Small bits of pre-release tidying.
[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 "raw_convert.h"
41 #include "asset.h"
42 #include "util.h"
43 #include "exceptions.h"
44 #include "dcp_assert.h"
45 #include "compose.hpp"
46 #include "pkl.h"
47 #include <libxml++/libxml++.h>
48 #include <boost/algorithm/string.hpp>
49
50
51 using std::string;
52 using boost::function;
53 using std::shared_ptr;
54 using boost::optional;
55 using namespace boost::filesystem;
56 using namespace dcp;
57
58
59 Asset::Asset ()
60 {
61
62 }
63
64
65 Asset::Asset (path file)
66         : _file (file)
67 {
68
69 }
70
71
72 Asset::Asset (string id, path file)
73         : Object (id)
74         , _file (file)
75 {
76
77 }
78
79
80 void
81 Asset::add_to_pkl (shared_ptr<PKL> pkl, path root) const
82 {
83         DCP_ASSERT (_file);
84
85         auto path = relative_to_root (
86                 canonical(root),
87                 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(), file_size(_file.get()), pkl_type(pkl->standard()));
98 }
99
100
101 void
102 Asset::write_to_assetmap (xmlpp::Node* node, path root) const
103 {
104         DCP_ASSERT (_file);
105         write_file_to_assetmap (node, root, _file.get(), _id);
106 }
107
108
109 void
110 Asset::write_file_to_assetmap (xmlpp::Node* node, path root, path file, string id)
111 {
112         auto path = relative_to_root (
113                 canonical(root),
114                 canonical(file)
115                 );
116
117         if (!path) {
118                 /* The path of this asset is not within our DCP, so we assume it's an external
119                    (referenced) one.
120                 */
121                 return;
122         }
123
124         auto asset = node->add_child ("Asset");
125         asset->add_child("Id")->add_child_text("urn:uuid:" + id);
126         auto chunk_list = asset->add_child ("ChunkList");
127         auto chunk = chunk_list->add_child ("Chunk");
128
129         chunk->add_child("Path")->add_child_text(path.get().generic_string());
130         chunk->add_child("VolumeIndex")->add_child_text("1");
131         chunk->add_child("Offset")->add_child_text("0");
132         chunk->add_child("Length")->add_child_text(raw_convert<string>(file_size(file)));
133 }
134
135
136 string
137 Asset::hash (function<void (float)> progress) const
138 {
139         DCP_ASSERT (_file);
140
141         if (!_hash) {
142                 _hash = make_digest (_file.get(), progress);
143         }
144
145         return _hash.get();
146 }
147
148
149 bool
150 Asset::equals (std::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
151 {
152         if (_hash != other->_hash) {
153                 note (NoteType::ERROR, "Asset: hashes differ");
154                 return false;
155         }
156
157         return true;
158 }
159
160
161 void
162 Asset::set_file (path file) const
163 {
164         _file = absolute (file);
165         _hash = {};
166 }
167
168
169 void
170 Asset::set_hash (string hash)
171 {
172         _hash = hash;
173 }