Add a nice note for general MXF errors.
[libdcp.git] / src / asset.cc
1 /*
2     Copyright (C) 2014-2021 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
35 /** @file  src/asset.cc
36  *  @brief Asset class
37  */
38
39
40 #include "asset.h"
41 #include "asset_map.h"
42 #include "compose.hpp"
43 #include "dcp_assert.h"
44 #include "exceptions.h"
45 #include "pkl.h"
46 #include "raw_convert.h"
47 #include "util.h"
48 #include "warnings.h"
49 LIBDCP_DISABLE_WARNINGS
50 #include <libxml++/libxml++.h>
51 LIBDCP_ENABLE_WARNINGS
52 #include <boost/algorithm/string.hpp>
53
54
55 using std::string;
56 using boost::function;
57 using std::shared_ptr;
58 using boost::optional;
59 using namespace boost::filesystem;
60 using namespace dcp;
61
62
63 Asset::Asset ()
64 {
65
66 }
67
68
69 Asset::Asset (path file)
70         : _file (file)
71 {
72
73 }
74
75
76 Asset::Asset (string id, path file)
77         : Object (id)
78         , _file (file)
79 {
80
81 }
82
83
84 void
85 Asset::add_to_pkl (shared_ptr<PKL> pkl, path root) const
86 {
87         DCP_ASSERT (_file);
88
89         auto path = relative_to_root (
90                 canonical(root),
91                 canonical(_file.get())
92                 );
93
94         if (!path) {
95                 /* The path of this asset is not within our DCP, so we assume it's an external
96                    (referenced) one.
97                 */
98                 return;
99         }
100
101         pkl->add_asset(_id, _id, hash(), file_size(_file.get()), pkl_type(pkl->standard()), _file->filename().string());
102 }
103
104
105 void
106 Asset::add_to_assetmap (AssetMap& asset_map, path root) const
107 {
108         DCP_ASSERT (_file);
109         add_file_to_assetmap (asset_map, root, _file.get(), _id);
110 }
111
112
113 void
114 Asset::add_file_to_assetmap (AssetMap& asset_map, path root, path file, string id)
115 {
116         auto path = relative_to_root (
117                 canonical(root),
118                 canonical(file)
119                 );
120
121         if (!path) {
122                 /* The path of this asset is not within our DCP, so we assume it's an external
123                    (referenced) one.
124                 */
125                 return;
126         }
127
128         asset_map.add_asset(id, file, false);
129 }
130
131
132 string
133 Asset::hash (function<void (float)> progress) const
134 {
135         DCP_ASSERT (_file);
136
137         if (!_hash) {
138                 _hash = make_digest (_file.get(), progress);
139         }
140
141         return _hash.get();
142 }
143
144
145 bool
146 Asset::equals (std::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
147 {
148         if (_hash != other->_hash) {
149                 note (NoteType::ERROR, "Asset: hashes differ");
150                 return false;
151         }
152
153         return true;
154 }
155
156
157 void
158 Asset::set_file (path file) const
159 {
160         _file = absolute (file);
161         _hash = boost::optional<string>();
162 }
163
164
165 void
166 Asset::set_hash (string hash)
167 {
168         _hash = hash;
169 }