2 Copyright (C) 2024 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic 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 DCP-o-matic 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "dcpomatic_assert.h"
23 #include "remembered_asset.h"
24 #include <dcp/filesystem.h>
25 #include <dcp/raw_convert.h>
26 #include <libcxml/cxml.h>
27 LIBDCP_DISABLE_WARNINGS
28 #include <libxml++/libxml++.h>
29 LIBDCP_ENABLE_WARNINGS
36 RememberedAsset::RememberedAsset(cxml::ConstNodePtr node)
38 _filename = node->string_child("Filename");
39 auto period_node = node->node_child("Period");
40 DCPOMATIC_ASSERT(period_node);
43 dcpomatic::DCPTime(period_node->number_child<int64_t>("From")),
44 dcpomatic::DCPTime(period_node->number_child<int64_t>("To"))
47 _identifier = node->string_child("Identifier");
52 RememberedAsset::as_xml(xmlpp::Element* parent) const
54 cxml::add_text_child(parent, "Filename", _filename.string());
55 auto period_node = parent->add_child("Period");
56 cxml::add_text_child(period_node, "From", dcp::raw_convert<string>(_period.from.get()));
57 cxml::add_text_child(period_node, "To", dcp::raw_convert<string>(_period.to.get()));
58 cxml::add_text_child(parent, "Identifier", _identifier);
62 boost::optional<boost::filesystem::path>
63 find_asset(vector<RememberedAsset> const& assets, boost::filesystem::path directory, dcpomatic::DCPTimePeriod period, string identifier)
65 for (auto path: dcp::filesystem::recursive_directory_iterator(directory)) {
66 auto iter = std::find_if(assets.begin(), assets.end(), [period, identifier, path](RememberedAsset const& asset) {
67 return asset.filename() == path.path().filename() && asset.period() == period && asset.identifier() == identifier;
69 if (iter != assets.end()) {
79 clean_up_asset_directory(boost::filesystem::path directory)
81 /* We could do something more advanced here (e.g. keep the last N assets) but for now
82 * let's just clean the whole thing out.
84 boost::system::error_code ec;
85 dcp::filesystem::remove_all(directory, ec);
90 preserve_assets(boost::filesystem::path search, boost::filesystem::path assets_path)
92 for (auto const& path: boost::filesystem::directory_iterator(search)) {
93 if (path.path().extension() == ".mxf") {
94 dcp::filesystem::rename(path.path(), assets_path / path.path().filename());