2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "curl_uploader.h"
22 #include "exceptions.h"
25 #include "compose.hpp"
32 using boost::function;
35 read_callback (void* ptr, size_t size, size_t nmemb, void* object)
37 CurlUploader* u = reinterpret_cast<CurlUploader*> (object);
38 return u->read_callback (ptr, size, nmemb);
41 CurlUploader::CurlUploader (function<void (string)> set_status, function<void (float)> set_progress)
42 : Uploader (set_status, set_progress)
47 _curl = curl_easy_init ();
49 throw NetworkError (_("Could not start transfer"));
52 curl_easy_setopt (_curl, CURLOPT_READFUNCTION, ::read_callback);
53 curl_easy_setopt (_curl, CURLOPT_READDATA, this);
54 curl_easy_setopt (_curl, CURLOPT_UPLOAD, 1L);
55 curl_easy_setopt (_curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
56 curl_easy_setopt (_curl, CURLOPT_READDATA, this);
57 curl_easy_setopt (_curl, CURLOPT_USERNAME, Config::instance()->tms_user().c_str ());
58 curl_easy_setopt (_curl, CURLOPT_PASSWORD, Config::instance()->tms_password().c_str ());
61 CurlUploader::~CurlUploader ()
66 curl_easy_cleanup (_curl);
70 CurlUploader::create_directory (boost::filesystem::path)
72 /* this is done by libcurl */
76 CurlUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
80 /* Use generic_string so that we get forward-slashes in the path, even on Windows */
81 String::compose ("ftp://%1/%2/%3", Config::instance()->tms_ip(), Config::instance()->tms_path(), to.generic_string ()).c_str ()
84 _file = fopen_boost (from, "rb");
86 throw NetworkError (String::compose (_("Could not open %1 to send"), from));
88 _transferred = &transferred;
89 _total_size = total_size;
91 CURLcode const r = curl_easy_perform (_curl);
93 throw NetworkError (String::compose (_("Could not write to remote file (%1)"), curl_easy_strerror (r)));
101 CurlUploader::read_callback (void* ptr, size_t size, size_t nmemb)
103 size_t const r = fread (ptr, size, nmemb, _file);
104 *_transferred += size * nmemb;
106 if (_total_size > 0) {
107 _set_progress ((double) *_transferred / _total_size);