WIP: stop using video directory and hard-linking (#2756).
[dcpomatic.git] / src / lib / remembered_asset.cc
1 /*
2     Copyright (C) 2024 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
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
30
31
32 using std::string;
33 using std::vector;
34
35
36 RememberedAsset::RememberedAsset(cxml::ConstNodePtr node)
37 {
38         _filename = node->string_child("Filename");
39         auto period_node = node->node_child("Period");
40         DCPOMATIC_ASSERT(period_node);
41
42         _period = {
43                 dcpomatic::DCPTime(period_node->number_child<int64_t>("From")),
44                 dcpomatic::DCPTime(period_node->number_child<int64_t>("To"))
45         };
46
47         _identifier = node->string_child("Identifier");
48 }
49
50
51 void
52 RememberedAsset::as_xml(xmlpp::Element* parent) const
53 {
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);
59 }
60
61
62 boost::optional<boost::filesystem::path>
63 find_asset(vector<RememberedAsset> const& assets, boost::filesystem::path directory, dcpomatic::DCPTimePeriod period, string identifier)
64 {
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;
68                 });
69                 if (iter != assets.end()) {
70                         return path.path();
71                 }
72         }
73
74         return {};
75 }
76
77
78 void
79 clean_up_asset_directory(boost::filesystem::path directory)
80 {
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.
83          */
84         boost::system::error_code ec;
85         dcp::filesystem::remove_all(directory, ec);
86 }
87
88
89 void
90 preserve_assets(boost::filesystem::path search, boost::filesystem::path assets_path)
91 {
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());
95                 }
96         }
97 }