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