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