5b64b6ccfe73ffe72bd1049a1c3e227d6d19ddff
[libdcp.git] / src / asset.cc
1 /*
2     Copyright (C) 2014-2018 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     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/asset.cc
35  *  @brief Asset class.
36  */
37
38 #include "raw_convert.h"
39 #include "asset.h"
40 #include "util.h"
41 #include "exceptions.h"
42 #include "dcp_assert.h"
43 #include "compose.hpp"
44 #include "pkl.h"
45 #include <libxml++/libxml++.h>
46 #include <boost/algorithm/string.hpp>
47
48 using std::string;
49 using boost::function;
50 using boost::shared_ptr;
51 using boost::optional;
52 using namespace dcp;
53
54 /** Create an Asset with a randomly-generated ID */
55 Asset::Asset ()
56 {
57
58 }
59
60 /** Create an Asset from a given file.
61  *  @param file File name.
62  */
63 Asset::Asset (boost::filesystem::path file)
64         : _file (file)
65 {
66
67 }
68
69 /** Create an Asset from a given file with a known ID.
70  *  @param file File name.
71  *  @param id ID.
72  */
73 Asset::Asset (string id, boost::filesystem::path file)
74         : Object (id)
75         , _file (file)
76 {
77
78 }
79
80 void
81 Asset::add_to_pkl (shared_ptr<PKL> pkl, boost::filesystem::path root) const
82 {
83         DCP_ASSERT (_file);
84
85         optional<boost::filesystem::path> path = relative_to_root (
86                 boost::filesystem::canonical (root),
87                 boost::filesystem::canonical (_file.get())
88                 );
89
90         if (!path) {
91                 /* The path of this asset is not within our DCP, so we assume it's an external
92                    (referenced) one.
93                 */
94                 return;
95         }
96
97         pkl->add_asset (_id, _id, hash(), boost::filesystem::file_size (_file.get()), pkl_type (pkl->standard()));
98 }
99
100 void
101 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
102 {
103         DCP_ASSERT (_file);
104         write_file_to_assetmap (node, root, _file.get(), _id);
105 }
106
107 void
108 Asset::write_file_to_assetmap (xmlpp::Node* node, boost::filesystem::path root, boost::filesystem::path file, string id)
109 {
110         optional<boost::filesystem::path> path = relative_to_root (
111                 boost::filesystem::canonical (root),
112                 boost::filesystem::canonical (file)
113                 );
114
115         if (!path) {
116                 /* The path of this asset is not within our DCP, so we assume it's an external
117                    (referenced) one.
118                 */
119                 return;
120         }
121
122         xmlpp::Node* asset = node->add_child ("Asset");
123         asset->add_child("Id")->add_child_text ("urn:uuid:" + id);
124         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
125         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
126
127         chunk->add_child("Path")->add_child_text (path.get().generic_string());
128         chunk->add_child("VolumeIndex")->add_child_text ("1");
129         chunk->add_child("Offset")->add_child_text ("0");
130         chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (file)));
131 }
132
133 string
134 Asset::hash (function<void (float)> progress) const
135 {
136         DCP_ASSERT (_file);
137
138         if (!_hash) {
139                 _hash = make_digest (_file.get(), progress);
140         }
141
142         return _hash.get();
143 }
144
145 bool
146 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
147 {
148         if (_hash != other->_hash) {
149                 note (DCP_ERROR, "Asset: hashes differ");
150                 return false;
151         }
152
153         return true;
154 }
155
156 /** Set the file that holds this asset on disk.  Calling this function
157  *  clears this object's store of its hash, so you should call ::hash
158  *  after this.
159  *
160  *  @param file New file's path.
161  */
162 void
163 Asset::set_file (boost::filesystem::path file) const
164 {
165         _file = boost::filesystem::absolute (file);
166         _hash = optional<string> ();
167 }
168
169 void
170 Asset::set_hash (string hash)
171 {
172         _hash = hash;
173 }