Add Asset::set_hash().
[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 /** @file  src/asset.cc
21  *  @brief Asset class.
22  */
23
24 #include "raw_convert.h"
25 #include "asset.h"
26 #include "util.h"
27 #include "exceptions.h"
28 #include "dcp_assert.h"
29 #include "compose.hpp"
30 #include <libxml++/libxml++.h>
31 #include <boost/algorithm/string.hpp>
32
33 using std::string;
34 using boost::function;
35 using boost::optional;
36 using namespace dcp;
37
38 /** Create an Asset with a randomly-generated ID */
39 Asset::Asset ()
40 {
41
42 }
43
44 /** Create an Asset from a given file.
45  *  @param file File name.
46  */
47 Asset::Asset (boost::filesystem::path file)
48         : _file (file)
49 {
50
51 }
52
53 Asset::Asset (string id, boost::filesystem::path file)
54         : Object (id)
55         , _file (file)
56 {
57
58 }
59
60 void
61 Asset::write_to_pkl (xmlpp::Node* node, boost::filesystem::path root, Standard standard) const
62 {
63         DCP_ASSERT (!_file.empty ());
64
65         optional<boost::filesystem::path> path = relative_to_root (
66                 boost::filesystem::canonical (root),
67                 boost::filesystem::canonical (_file)
68                 );
69
70         if (!path) {
71                 /* The path of this asset is not within our DCP, so we assume it's an external
72                    (referenced) one.
73                 */
74                 return;
75         }
76
77         xmlpp::Node* asset = node->add_child ("Asset");
78         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
79         asset->add_child("AnnotationText")->add_child_text (_id);
80         asset->add_child("Hash")->add_child_text (hash ());
81         asset->add_child("Size")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
82         asset->add_child("Type")->add_child_text (pkl_type (standard));
83 }
84
85 void
86 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
87 {
88         DCP_ASSERT (!_file.empty ());
89
90         optional<boost::filesystem::path> path = relative_to_root (
91                 boost::filesystem::canonical (root),
92                 boost::filesystem::canonical (_file)
93                 );
94
95         if (!path) {
96                 /* The path of this asset is not within our DCP, so we assume it's an external
97                    (referenced) one.
98                 */
99                 return;
100         }
101
102         xmlpp::Node* asset = node->add_child ("Asset");
103         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
104         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
105         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
106
107         chunk->add_child("Path")->add_child_text (path.get().generic_string());
108         chunk->add_child("VolumeIndex")->add_child_text ("1");
109         chunk->add_child("Offset")->add_child_text ("0");
110         chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
111 }
112
113 string
114 Asset::hash (function<void (float)> progress) const
115 {
116         DCP_ASSERT (!_file.empty ());
117
118         if (!_hash) {
119                 _hash = make_digest (_file, progress);
120         }
121
122         return _hash.get();
123 }
124
125 bool
126 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
127 {
128         if (_hash != other->_hash) {
129                 note (DCP_ERROR, "Asset: hashes differ");
130                 return false;
131         }
132
133         return true;
134 }
135
136 /** Set the file that holds this asset on disk.  Calling this function
137  *  clears this object's store of its hash, so you should call ::hash
138  *  after this.
139  *
140  *  @param file New file's path.
141  */
142 void
143 Asset::set_file (boost::filesystem::path file) const
144 {
145         _file = boost::filesystem::absolute (file);
146         _hash = optional<string> ();
147 }
148
149 void
150 Asset::set_hash (string hash)
151 {
152         _hash = hash;
153 }