Merge master.
[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 "compose.hpp"
29 #include <libxml++/libxml++.h>
30
31 using std::string;
32 using boost::function;
33 using boost::optional;
34 using namespace dcp;
35
36 /** Create an Asset with a randomly-generated ID */
37 Asset::Asset ()
38 {
39
40 }
41
42 /** Create an Asset from a given file.  The ID will
43  *  be extracted from the file.
44  *  @param file File name.
45  */
46 Asset::Asset (boost::filesystem::path file)
47         : _file (file)
48 {
49
50 }
51
52 /** Create an Asset with a specified ID.
53  *  @param id ID to use.
54  */
55 Asset::Asset (string id)
56         : Object (id)
57 {
58
59 }
60
61 void
62 Asset::write_to_pkl (xmlpp::Node* node, Standard standard) const
63 {
64         assert (!_file.empty ());
65         
66         xmlpp::Node* asset = node->add_child ("Asset");
67         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
68         asset->add_child("AnnotationText")->add_child_text (_id);
69         asset->add_child("Hash")->add_child_text (hash ());
70         asset->add_child("Size")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
71         asset->add_child("Type")->add_child_text (pkl_type (standard));
72 }
73
74 void
75 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
76 {
77         assert (!_file.empty ());
78
79         xmlpp::Node* asset = node->add_child ("Asset");
80         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
81         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
82         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
83         optional<boost::filesystem::path> path = relative_to_root (root, _file);
84         if (!path) {
85                 throw MiscError (String::compose ("Asset %1 is not within the directory %2", _file, root));
86         }
87         chunk->add_child("Path")->add_child_text (path.get().string ());
88         chunk->add_child("VolumeIndex")->add_child_text ("1");
89         chunk->add_child("Offset")->add_child_text ("0");
90         chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
91 }
92
93 string
94 Asset::hash (function<void (float)> progress) const
95 {
96         assert (!_file.empty ());
97                 
98         if (_hash.empty ()) {
99                 _hash = make_digest (_file, progress);
100         }
101
102         return _hash;
103 }
104
105 bool
106 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, function<void (NoteType, string)> note) const
107 {
108         if (_hash != other->_hash) {
109                 note (ERROR, "Asset hashes differ");
110                 return false;
111         }
112
113         return true;
114 }
115
116 void
117 Asset::set_file (boost::filesystem::path file) const
118 {
119         _file = boost::filesystem::absolute (file);
120         _hash.clear ();
121 }
122