2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
21 /** @file src/asset.cc
25 #include "raw_convert.h"
28 #include "exceptions.h"
29 #include "dcp_assert.h"
30 #include "compose.hpp"
31 #include <libxml++/libxml++.h>
32 #include <boost/algorithm/string.hpp>
35 using boost::function;
36 using boost::optional;
39 /** Create an Asset with a randomly-generated ID */
45 /** Create an Asset from a given file.
46 * @param file File name.
48 Asset::Asset (boost::filesystem::path file)
54 Asset::Asset (string id, boost::filesystem::path file)
62 Asset::write_to_pkl (xmlpp::Node* node, boost::filesystem::path root, Standard standard) const
64 DCP_ASSERT (!_file.empty ());
66 optional<boost::filesystem::path> path = relative_to_root (
67 boost::filesystem::canonical (root),
68 boost::filesystem::canonical (_file)
72 /* The path of this asset is not within our DCP, so we assume it's an external
78 xmlpp::Node* asset = node->add_child ("Asset");
79 asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
80 asset->add_child("AnnotationText")->add_child_text (_id);
81 asset->add_child("Hash")->add_child_text (hash ());
82 asset->add_child("Size")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
83 asset->add_child("Type")->add_child_text (pkl_type (standard));
87 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
89 DCP_ASSERT (!_file.empty ());
91 optional<boost::filesystem::path> path = relative_to_root (
92 boost::filesystem::canonical (root),
93 boost::filesystem::canonical (_file)
97 /* The path of this asset is not within our DCP, so we assume it's an external
103 xmlpp::Node* asset = node->add_child ("Asset");
104 asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
105 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
106 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
108 chunk->add_child("Path")->add_child_text (path.get().generic_string());
109 chunk->add_child("VolumeIndex")->add_child_text ("1");
110 chunk->add_child("Offset")->add_child_text ("0");
111 chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file)));
115 Asset::hash (function<void (float)> progress) const
117 DCP_ASSERT (!_file.empty ());
119 if (_hash.empty ()) {
120 _hash = make_digest (_file, progress);
127 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
129 if (_hash != other->_hash) {
130 note (DCP_ERROR, "Asset: hashes differ");
137 /** Set the file that holds this asset on disk. Calling this function
138 * clears this object's store of its hash, so you should call ::hash
141 * @param file New file's path.
144 Asset::set_file (boost::filesystem::path file) const
146 _file = boost::filesystem::absolute (file);