Merge.
[libdcp.git] / src / asset.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
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
32 using std::string;
33 using boost::function;
34 using boost::optional;
35 using namespace dcp;
36
37 /** Create an Asset with a randomly-generated ID */
38 Asset::Asset ()
39 {
40
41 }
42
43 /** Create an Asset from a given file.
44  *  @param file File name.
45  */
46 Asset::Asset (boost::filesystem::path file)
47         : _file (file)
48 {
49
50 }
51
52 Asset::Asset (string id, boost::filesystem::path file)
53         : Object (id)
54         , _file (file)
55 {
56
57 }
58
59 void
60 Asset::write_to_pkl (xmlpp::Node* node, Standard standard) const
61 {
62         DCP_ASSERT (!_file.empty ());
63
64         xmlpp::Node* asset = node->add_child ("Asset");
65         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
66         asset->add_child("AnnotationText")->add_child_text (_id);
67         asset->add_child("Hash")->add_child_text (hash ());
68         asset->add_child("Size")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
69         asset->add_child("Type")->add_child_text (pkl_type (standard));
70 }
71
72 void
73 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
74 {
75         DCP_ASSERT (!_file.empty ());
76
77         xmlpp::Node* asset = node->add_child ("Asset");
78         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
79         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
80         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
81
82         optional<boost::filesystem::path> path = relative_to_root (
83                 boost::filesystem::canonical (root),
84                 boost::filesystem::canonical (_file)
85                 );
86
87         if (!path) {
88                 throw MiscError (String::compose ("Asset %1 is not within the directory %2", _file, root));
89         }
90
91         chunk->add_child("Path")->add_child_text (path.get().string ());
92         chunk->add_child("VolumeIndex")->add_child_text ("1");
93         chunk->add_child("Offset")->add_child_text ("0");
94         chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
95 }
96
97 string
98 Asset::hash (function<void (float)> progress) const
99 {
100         DCP_ASSERT (!_file.empty ());
101
102         if (_hash.empty ()) {
103                 _hash = make_digest (_file, progress);
104         }
105
106         return _hash;
107 }
108
109 bool
110 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
111 {
112         if (_hash != other->_hash) {
113                 note (DCP_ERROR, "Asset: hashes differ");
114                 return false;
115         }
116
117         return true;
118 }
119
120 /** Set the file that holds this asset on disk.  Calling this function
121  *  clears this object's store of its hash, so you should call ::hash
122  *  after this.
123  *
124  *  @param file New file's path.
125  */
126 void
127 Asset::set_file (boost::filesystem::path file) const
128 {
129         _file = boost::filesystem::absolute (file);
130         _hash.clear ();
131 }
132