Make a generic base for uploaders and move the SCP code into a subclass of that.
[dcpomatic.git] / src / lib / uploader.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "uploader.h"
21 #include "dcpomatic_assert.h"
22
23 using std::string;
24 using boost::shared_ptr;
25 using boost::function;
26
27 Uploader::Uploader (function<void (string)> set_status, function<void (float)> set_progress)
28         : _set_status (set_status)
29         , _set_progress (set_progress)
30 {
31
32 }
33
34 boost::uintmax_t
35 Uploader::count_file_sizes (boost::filesystem::path directory) const
36 {
37         using namespace boost::filesystem;
38
39         boost::uintmax_t size = 0;
40
41         for (directory_iterator i = directory_iterator (directory); i != directory_iterator (); ++i) {
42                 if (is_directory (i->path ())) {
43                         size += count_file_sizes (i->path ());
44                 } else {
45                         size += file_size (*i);
46                 }
47         }
48
49         return size;
50 }
51
52 void
53 Uploader::upload (boost::filesystem::path directory)
54 {
55         boost::uintmax_t transferred = 0;
56         upload_directory (directory.parent_path (), directory, transferred, count_file_sizes (directory));
57 }
58
59 void
60 Uploader::upload_directory (boost::filesystem::path base, boost::filesystem::path directory, boost::uintmax_t& transferred, boost::uintmax_t total_size)
61 {
62         using namespace boost::filesystem;
63
64         create_directory (remove_prefix (base, directory));
65         for (directory_iterator i = directory_iterator (directory); i != directory_iterator (); ++i) {
66                 if (is_directory (i->path ())) {
67                         upload_directory (base, i->path (), transferred, total_size);
68                 } else {
69                         upload_file (i->path (), remove_prefix (base, i->path ()), transferred, total_size);
70                 }
71         }
72 }
73
74 boost::filesystem::path
75 Uploader::remove_prefix (boost::filesystem::path prefix, boost::filesystem::path target) const
76 {
77         using namespace boost::filesystem;
78
79         path result;
80
81         path::iterator i = target.begin ();
82         for (path::iterator j = prefix.begin (); j != prefix.end(); ++j) {
83                 DCPOMATIC_ASSERT (*i == *j);
84                 ++i;
85         }
86
87         for (; i != target.end(); ++i) {
88                 result /= *i;
89         }
90
91         return result;
92 }