2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/asset.cc
24 #include "raw_convert.h"
27 #include "exceptions.h"
28 #include "compose.hpp"
29 #include <libxml++/libxml++.h>
32 using boost::function;
33 using boost::optional;
36 /** Create an Asset with a randomly-generated ID */
42 /** Create an Asset from a given file.
43 * @param file File name.
45 Asset::Asset (boost::filesystem::path file)
51 /** Create an Asset with a specified ID.
52 * @param id ID to use.
54 Asset::Asset (string id)
61 Asset::write_to_pkl (xmlpp::Node* node, Standard standard) const
63 assert (!_file.empty ());
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));
74 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
76 assert (!_file.empty ());
78 xmlpp::Node* asset = node->add_child ("Asset");
79 asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
80 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
81 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
82 optional<boost::filesystem::path> path = relative_to_root (root, _file);
84 throw MiscError (String::compose ("Asset %1 is not within the directory %2", _file, root));
86 chunk->add_child("Path")->add_child_text (path.get().string ());
87 chunk->add_child("VolumeIndex")->add_child_text ("1");
88 chunk->add_child("Offset")->add_child_text ("0");
89 chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
93 Asset::hash (function<void (float)> progress) const
95 assert (!_file.empty ());
98 _hash = make_digest (_file, progress);
105 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, function<void (NoteType, string)> note) const
107 if (_hash != other->_hash) {
108 note (DCP_ERROR, "Asset hashes differ");
116 Asset::set_file (boost::filesystem::path file) const
118 _file = boost::filesystem::absolute (file);