82ef44feccad02ecfc5da0e5534d2bc3472e304a
[libdcp.git] / src / asset.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/asset.cc
22  *  @brief Asset class.
23  */
24
25 #include "raw_convert.h"
26 #include "asset.h"
27 #include "util.h"
28 #include "exceptions.h"
29 #include "dcp_assert.h"
30 #include "compose.hpp"
31 #include <libxml++/libxml++.h>
32 #include <boost/algorithm/string.hpp>
33
34 using std::string;
35 using boost::function;
36 using boost::optional;
37 using namespace dcp;
38
39 /** Create an Asset with a randomly-generated ID */
40 Asset::Asset ()
41 {
42
43 }
44
45 /** Create an Asset from a given file.
46  *  @param file File name.
47  */
48 Asset::Asset (boost::filesystem::path file)
49         : _file (file)
50 {
51
52 }
53
54 Asset::Asset (string id, boost::filesystem::path file)
55         : Object (id)
56         , _file (file)
57 {
58
59 }
60
61 void
62 Asset::write_to_pkl (xmlpp::Node* node, boost::filesystem::path root, Standard standard) const
63 {
64         DCP_ASSERT (!_file.empty ());
65
66         optional<boost::filesystem::path> path = relative_to_root (
67                 boost::filesystem::canonical (root),
68                 boost::filesystem::canonical (_file)
69                 );
70
71         if (!path) {
72                 /* The path of this asset is not within our DCP, so we assume it's an external
73                    (referenced) one.
74                 */
75                 return;
76         }
77
78         xmlpp::Node* asset = node->add_child ("Asset");
79         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
80         asset->add_child("AnnotationText")->add_child_text (_id);
81         asset->add_child("Hash")->add_child_text (hash ());
82         asset->add_child("Size")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
83         asset->add_child("Type")->add_child_text (pkl_type (standard));
84 }
85
86 void
87 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
88 {
89         DCP_ASSERT (!_file.empty ());
90
91         optional<boost::filesystem::path> path = relative_to_root (
92                 boost::filesystem::canonical (root),
93                 boost::filesystem::canonical (_file)
94                 );
95
96         if (!path) {
97                 /* The path of this asset is not within our DCP, so we assume it's an external
98                    (referenced) one.
99                 */
100                 return;
101         }
102
103         xmlpp::Node* asset = node->add_child ("Asset");
104         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
105         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
106         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
107
108         chunk->add_child("Path")->add_child_text (path.get().generic_string());
109         chunk->add_child("VolumeIndex")->add_child_text ("1");
110         chunk->add_child("Offset")->add_child_text ("0");
111         chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
112 }
113
114 string
115 Asset::hash (function<void (float)> progress) const
116 {
117         DCP_ASSERT (!_file.empty ());
118
119         if (_hash.empty ()) {
120                 _hash = make_digest (_file, progress);
121         }
122
123         return _hash;
124 }
125
126 bool
127 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
128 {
129         if (_hash != other->_hash) {
130                 note (DCP_ERROR, "Asset: hashes differ");
131                 return false;
132         }
133
134         return true;
135 }
136
137 /** Set the file that holds this asset on disk.  Calling this function
138  *  clears this object's store of its hash, so you should call ::hash
139  *  after this.
140  *
141  *  @param file New file's path.
142  */
143 void
144 Asset::set_file (boost::filesystem::path file) const
145 {
146         _file = boost::filesystem::absolute (file);
147         _hash.clear ();
148 }