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
26 #include "exceptions.h"
27 #include "compose.hpp"
28 #include <libxml++/libxml++.h>
29 #include <boost/lexical_cast.hpp>
32 using boost::lexical_cast;
33 using boost::function;
34 using boost::optional;
37 /** Create an Asset with a randomly-generated ID */
43 /** Create an Asset from a given file. The ID will
44 * be extracted from the file.
45 * @param file File name.
47 Asset::Asset (boost::filesystem::path file)
53 /** Create an Asset with a specified ID.
54 * @param id ID to use.
56 Asset::Asset (string id)
63 Asset::write_to_pkl (xmlpp::Node* node, Standard standard) const
65 assert (!_file.empty ());
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));
76 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
78 assert (!_file.empty ());
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);
86 throw MiscError (String::compose ("Asset %1 is not within the directory %2", _file, root));
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)));
95 Asset::hash (function<void (float)> progress) const
97 assert (!_file.empty ());
100 _hash = make_digest (_file, progress);
107 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, function<void (NoteType, string)> note) const
109 if (_hash != other->_hash) {
110 note (ERROR, "Asset hashes differ");
118 Asset::set_file (boost::filesystem::path file) const
120 _file = boost::filesystem::absolute (file);