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