summaryrefslogtreecommitdiff
path: root/src/lib/uploader.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2015-07-17 13:52:29 +0100
committerCarl Hetherington <cth@carlh.net>2015-07-17 15:40:55 +0100
commitb00389305af83dfd1e3b41ab3b108cb591f18c5d (patch)
tree3a369aa9607a8cab92236bf22e779a826edecf55 /src/lib/uploader.cc
parentc6b2624c1965740fa7b5d2230baf20156ff34151 (diff)
Make a generic base for uploaders and move the SCP code into a subclass of that.
Diffstat (limited to 'src/lib/uploader.cc')
-rw-r--r--src/lib/uploader.cc92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/lib/uploader.cc b/src/lib/uploader.cc
new file mode 100644
index 000000000..b6b23ed8e
--- /dev/null
+++ b/src/lib/uploader.cc
@@ -0,0 +1,92 @@
+/*
+ Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "uploader.h"
+#include "dcpomatic_assert.h"
+
+using std::string;
+using boost::shared_ptr;
+using boost::function;
+
+Uploader::Uploader (function<void (string)> set_status, function<void (float)> set_progress)
+ : _set_status (set_status)
+ , _set_progress (set_progress)
+{
+
+}
+
+boost::uintmax_t
+Uploader::count_file_sizes (boost::filesystem::path directory) const
+{
+ using namespace boost::filesystem;
+
+ boost::uintmax_t size = 0;
+
+ for (directory_iterator i = directory_iterator (directory); i != directory_iterator (); ++i) {
+ if (is_directory (i->path ())) {
+ size += count_file_sizes (i->path ());
+ } else {
+ size += file_size (*i);
+ }
+ }
+
+ return size;
+}
+
+void
+Uploader::upload (boost::filesystem::path directory)
+{
+ boost::uintmax_t transferred = 0;
+ upload_directory (directory.parent_path (), directory, transferred, count_file_sizes (directory));
+}
+
+void
+Uploader::upload_directory (boost::filesystem::path base, boost::filesystem::path directory, boost::uintmax_t& transferred, boost::uintmax_t total_size)
+{
+ using namespace boost::filesystem;
+
+ create_directory (remove_prefix (base, directory));
+ for (directory_iterator i = directory_iterator (directory); i != directory_iterator (); ++i) {
+ if (is_directory (i->path ())) {
+ upload_directory (base, i->path (), transferred, total_size);
+ } else {
+ upload_file (i->path (), remove_prefix (base, i->path ()), transferred, total_size);
+ }
+ }
+}
+
+boost::filesystem::path
+Uploader::remove_prefix (boost::filesystem::path prefix, boost::filesystem::path target) const
+{
+ using namespace boost::filesystem;
+
+ path result;
+
+ path::iterator i = target.begin ();
+ for (path::iterator j = prefix.begin (); j != prefix.end(); ++j) {
+ DCPOMATIC_ASSERT (*i == *j);
+ ++i;
+ }
+
+ for (; i != target.end(); ++i) {
+ result /= *i;
+ }
+
+ return result;
+}