Consider assets outside the DCP as referenced assets (i.e.
[libdcp.git] / src / asset.cc
1 /*
2     Copyright (C) 2014-2015 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 #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, Standard standard) const
62 {
63         DCP_ASSERT (!_file.empty ());
64
65         xmlpp::Node* asset = node->add_child ("Asset");
66         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
67         asset->add_child("AnnotationText")->add_child_text (_id);
68         asset->add_child("Hash")->add_child_text (hash ());
69         asset->add_child("Size")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
70         asset->add_child("Type")->add_child_text (pkl_type (standard));
71 }
72
73 void
74 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
75 {
76         DCP_ASSERT (!_file.empty ());
77
78         optional<boost::filesystem::path> path = relative_to_root (
79                 boost::filesystem::canonical (root),
80                 boost::filesystem::canonical (_file)
81                 );
82
83         if (!path) {
84                 /* The path of this asset is not within our DCP, so we assume it's an external
85                    (referenced) one.
86                 */
87                 return;
88         }
89
90         xmlpp::Node* asset = node->add_child ("Asset");
91         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
92         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
93         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
94
95         /* On Windows path.string() will contain back-slashes; replace these with
96            forward-slashes.  XXX: perhaps there is a nicer way to do this with boost.
97         */
98
99         string path_string = path.get().string ();
100         boost::replace_all (path_string, "\\", "/");
101
102         chunk->add_child("Path")->add_child_text (path_string);
103         chunk->add_child("VolumeIndex")->add_child_text ("1");
104         chunk->add_child("Offset")->add_child_text ("0");
105         chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
106 }
107
108 string
109 Asset::hash (function<void (float)> progress) const
110 {
111         DCP_ASSERT (!_file.empty ());
112
113         if (_hash.empty ()) {
114                 _hash = make_digest (_file, progress);
115         }
116
117         return _hash;
118 }
119
120 bool
121 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
122 {
123         if (_hash != other->_hash) {
124                 note (DCP_ERROR, "Asset: hashes differ");
125                 return false;
126         }
127
128         return true;
129 }
130
131 /** Set the file that holds this asset on disk.  Calling this function
132  *  clears this object's store of its hash, so you should call ::hash
133  *  after this.
134  *
135  *  @param file New file's path.
136  */
137 void
138 Asset::set_file (boost::filesystem::path file) const
139 {
140         _file = boost::filesystem::absolute (file);
141         _hash.clear ();
142 }