libdcp
asset.cc
Go to the documentation of this file.
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 
40 #include "asset.h"
41 #include "compose.hpp"
42 #include "dcp_assert.h"
43 #include "exceptions.h"
44 #include "pkl.h"
45 #include "raw_convert.h"
46 #include "util.h"
47 #include "warnings.h"
48 LIBDCP_DISABLE_WARNINGS
49 #include <libxml++/libxml++.h>
50 LIBDCP_ENABLE_WARNINGS
51 #include <boost/algorithm/string.hpp>
52 
53 
54 using std::string;
55 using boost::function;
56 using std::shared_ptr;
57 using boost::optional;
58 using namespace boost::filesystem;
59 using namespace dcp;
60 
61 
62 Asset::Asset ()
63 {
64 
65 }
66 
67 
68 Asset::Asset (path file)
69  : _file (file)
70 {
71 
72 }
73 
74 
75 Asset::Asset (string id, path file)
76  : Object (id)
77  , _file (file)
78 {
79 
80 }
81 
82 
83 void
84 Asset::add_to_pkl (shared_ptr<PKL> pkl, path root) const
85 {
86  DCP_ASSERT (_file);
87 
88  auto path = relative_to_root (
89  canonical(root),
90  canonical(_file.get())
91  );
92 
93  if (!path) {
94  /* The path of this asset is not within our DCP, so we assume it's an external
95  (referenced) one.
96  */
97  return;
98  }
99 
100  pkl->add_asset (_id, _id, hash(), file_size(_file.get()), pkl_type(pkl->standard()));
101 }
102 
103 
104 void
105 Asset::write_to_assetmap (xmlpp::Node* node, path root) const
106 {
107  DCP_ASSERT (_file);
108  write_file_to_assetmap (node, root, _file.get(), _id);
109 }
110 
111 
112 void
113 Asset::write_file_to_assetmap (xmlpp::Node* node, path root, path file, string id)
114 {
115  auto path = relative_to_root (
116  canonical(root),
117  canonical(file)
118  );
119 
120  if (!path) {
121  /* The path of this asset is not within our DCP, so we assume it's an external
122  (referenced) one.
123  */
124  return;
125  }
126 
127  auto asset = node->add_child ("Asset");
128  asset->add_child("Id")->add_child_text("urn:uuid:" + id);
129  auto chunk_list = asset->add_child ("ChunkList");
130  auto chunk = chunk_list->add_child ("Chunk");
131 
132  chunk->add_child("Path")->add_child_text(path.get().generic_string());
133  chunk->add_child("VolumeIndex")->add_child_text("1");
134  chunk->add_child("Offset")->add_child_text("0");
135  chunk->add_child("Length")->add_child_text(raw_convert<string>(file_size(file)));
136 }
137 
138 
139 string
140 Asset::hash (function<void (float)> progress) const
141 {
142  DCP_ASSERT (_file);
143 
144  if (!_hash) {
145  _hash = make_digest (_file.get(), progress);
146  }
147 
148  return _hash.get();
149 }
150 
151 
152 bool
153 Asset::equals (std::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
154 {
155  if (_hash != other->_hash) {
156  note (NoteType::ERROR, "Asset: hashes differ");
157  return false;
158  }
159 
160  return true;
161 }
162 
163 
164 void
165 Asset::set_file (path file) const
166 {
167  _file = absolute (file);
168  _hash = boost::optional<string>();
169 }
170 
171 
172 void
173 Asset::set_hash (string hash)
174 {
175  _hash = hash;
176 }
Asset class.
virtual std::string pkl_type(Standard standard) const =0
boost::optional< boost::filesystem::path > file() const
Definition: asset.h:97
boost::optional< boost::filesystem::path > _file
Definition: asset.h:122
boost::optional< std::string > _hash
Definition: asset.h:133
Asset()
Definition: asset.cc:62
void set_file(boost::filesystem::path file) const
Definition: asset.cc:165
std::string hash(boost::function< void(float)> progress={}) const
Some part of a DCP that has a UUID.
Definition: object.h:63
DCP_ASSERT macro.
Exceptions thrown by libdcp.
Namespace for everything in libdcp.
Definition: array_data.h:50
std::string make_digest(boost::filesystem::path filename, boost::function< void(float)>)
Methods for conversion to/from string.
A class to describe what "equality" means for a particular test.
Definition: types.h:249
Utility methods and classes.