2 Copyright (C) 2015-2021 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/>.
22 #include "curl_uploader.h"
23 #include "exceptions.h"
26 #include "compose.hpp"
38 read_callback (void* ptr, size_t size, size_t nmemb, void* object)
40 CurlUploader* u = reinterpret_cast<CurlUploader*> (object);
41 return u->read_callback (ptr, size, nmemb);
45 CurlUploader::CurlUploader (function<void (string)> set_status, function<void (float)> set_progress)
46 : Uploader (set_status, set_progress)
48 _curl = curl_easy_init ();
50 throw NetworkError (_("Could not start transfer"));
53 curl_easy_setopt (_curl, CURLOPT_READFUNCTION, ::read_callback);
54 curl_easy_setopt (_curl, CURLOPT_READDATA, this);
55 curl_easy_setopt (_curl, CURLOPT_UPLOAD, 1L);
56 curl_easy_setopt (_curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
57 curl_easy_setopt (_curl, CURLOPT_READDATA, this);
58 curl_easy_setopt (_curl, CURLOPT_USERNAME, Config::instance()->tms_user().c_str());
59 curl_easy_setopt (_curl, CURLOPT_PASSWORD, Config::instance()->tms_password().c_str());
63 CurlUploader::~CurlUploader ()
68 curl_easy_cleanup (_curl);
73 CurlUploader::create_directory (boost::filesystem::path)
75 /* this is done by libcurl */
80 CurlUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
84 /* Use generic_string so that we get forward-slashes in the path, even on Windows */
85 String::compose ("ftp://%1/%2/%3", Config::instance()->tms_ip(), Config::instance()->tms_path(), to.generic_string ()).c_str ()
88 _file = fopen_boost (from, "rb");
90 throw NetworkError (String::compose (_("Could not open %1 to send"), from));
92 _transferred = &transferred;
93 _total_size = total_size;
95 auto const r = curl_easy_perform (_curl);
97 throw NetworkError (String::compose (_("Could not write to remote file (%1)"), curl_easy_strerror (r)));
106 CurlUploader::read_callback (void* ptr, size_t size, size_t nmemb)
108 size_t const r = fread (ptr, size, nmemb, _file);
109 *_transferred += size * nmemb;
111 if (_total_size > 0) {
112 _set_progress ((double) *_transferred / _total_size);