Missed update to private test repo version.
[dcpomatic.git] / src / lib / uploader.cc
1 /*
2     Copyright (C) 2015-2021 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 "uploader.h"
23 #include "dcpomatic_assert.h"
24 #include "compose.hpp"
25
26 #include "i18n.h"
27
28
29 using std::string;
30 using std::shared_ptr;
31 using std::function;
32
33
34 Uploader::Uploader (function<void (string)> set_status, function<void (float)> set_progress)
35         : _set_progress (set_progress)
36         , _set_status (set_status)
37 {
38         _set_status (_("connecting"));
39 }
40
41
42 boost::uintmax_t
43 Uploader::count_file_sizes (boost::filesystem::path directory) const
44 {
45         using namespace boost::filesystem;
46
47         boost::uintmax_t size = 0;
48
49         for (auto i: directory_iterator(directory)) {
50                 if (is_directory (i.path())) {
51                         size += count_file_sizes (i.path());
52                 } else {
53                         size += file_size (i);
54                 }
55         }
56
57         return size;
58 }
59
60
61 void
62 Uploader::upload (boost::filesystem::path directory)
63 {
64         boost::uintmax_t transferred = 0;
65         upload_directory (directory.parent_path(), directory, transferred, count_file_sizes(directory));
66 }
67
68
69 void
70 Uploader::upload_directory (boost::filesystem::path base, boost::filesystem::path directory, boost::uintmax_t& transferred, boost::uintmax_t total_size)
71 {
72         using namespace boost::filesystem;
73
74         create_directory (remove_prefix(base, directory));
75         for (auto i: directory_iterator(directory)) {
76                 if (is_directory(i.path())) {
77                         upload_directory (base, i.path(), transferred, total_size);
78                 } else {
79                         _set_status (String::compose(_("copying %1"), i.path().leaf()));
80                         upload_file (i.path(), remove_prefix (base, i.path()), transferred, total_size);
81                 }
82         }
83 }
84
85
86 boost::filesystem::path
87 Uploader::remove_prefix (boost::filesystem::path prefix, boost::filesystem::path target) const
88 {
89         using namespace boost::filesystem;
90
91         path result;
92
93         auto i = target.begin ();
94         for (auto j = prefix.begin (); j != prefix.end(); ++j) {
95                 DCPOMATIC_ASSERT (*i == *j);
96                 ++i;
97         }
98
99         for (; i != target.end(); ++i) {
100                 result /= *i;
101         }
102
103         return result;
104 }