Various tinkerings.
[libdcp.git] / src / asset.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/asset.cc
21  *  @brief Asset class.
22  */
23
24 #include "asset.h"
25 #include "util.h"
26 #include <libxml++/libxml++.h>
27 #include <boost/lexical_cast.hpp>
28
29 using std::string;
30 using boost::lexical_cast;
31 using boost::function;
32 using namespace dcp;
33
34 /** Create an Asset with a randomly-generated ID */
35 Asset::Asset ()
36 {
37
38 }
39
40 /** Create an Asset from a given file.  The ID will
41  *  be extracted from the file.
42  *  @param file File name.
43  */
44 Asset::Asset (boost::filesystem::path file)
45         : _file (file)
46 {
47
48 }
49
50 /** Create an Asset with a specified ID.
51  *  @param id ID to use.
52  */
53 Asset::Asset (string id)
54         : Object (id)
55 {
56
57 }
58
59 void
60 Asset::write_to_pkl (xmlpp::Node* node) const
61 {
62         assert (!_file.empty ());
63         
64         xmlpp::Node* asset = node->add_child ("Asset");
65         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
66         asset->add_child("AnnotationText")->add_child_text (_id);
67         asset->add_child("Hash")->add_child_text (hash ());
68         asset->add_child("Size")->add_child_text (lexical_cast<string> (boost::filesystem::file_size (_file)));
69         asset->add_child("Type")->add_child_text (pkl_type ());
70 }
71
72 void
73 Asset::write_to_assetmap (xmlpp::Node* node) const
74 {
75         assert (!_file.empty ());
76
77         xmlpp::Node* asset = node->add_child ("Asset");
78         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
79         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
80         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
81         chunk->add_child("Path")->add_child_text (_file.string ());
82         chunk->add_child("VolumeIndex")->add_child_text ("1");
83         chunk->add_child("Offset")->add_child_text ("0");
84         chunk->add_child("Length")->add_child_text (lexical_cast<string> (boost::filesystem::file_size (_file)));
85 }
86
87 string
88 Asset::hash () const
89 {
90         assert (!_file.empty ());
91                 
92         if (!_hash.empty ()) {
93                 _hash = make_digest (_file, 0);
94         }
95
96         return _hash;
97 }
98
99 bool
100 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, function<void (NoteType, string)> note) const
101 {
102         if (_hash != other->_hash) {
103                 note (ERROR, "Asset hashes differ");
104                 return false;
105         }
106
107         return true;
108 }