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 "dcpomatic_log.h"
24 #include "exceptions.h"
27 #include "compose.hpp"
28 #include "dcpomatic_assert.h"
40 read_callback (void* ptr, size_t size, size_t nmemb, void* object)
42 CurlUploader* u = reinterpret_cast<CurlUploader*> (object);
43 return u->read_callback (ptr, size, nmemb);
48 curl_debug_shim (CURL* curl, curl_infotype type, char* data, size_t size, void* userp)
50 return reinterpret_cast<CurlUploader*>(userp)->debug(curl, type, data, size);
54 CurlUploader::CurlUploader (function<void (string)> set_status, function<void (float)> set_progress)
55 : Uploader (set_status, set_progress)
57 _curl = curl_easy_init ();
59 throw NetworkError (_("Could not start transfer"));
62 curl_easy_setopt (_curl, CURLOPT_READFUNCTION, ::read_callback);
63 curl_easy_setopt (_curl, CURLOPT_READDATA, this);
64 curl_easy_setopt (_curl, CURLOPT_UPLOAD, 1L);
65 curl_easy_setopt (_curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
66 curl_easy_setopt (_curl, CURLOPT_READDATA, this);
67 curl_easy_setopt (_curl, CURLOPT_USERNAME, Config::instance()->tms_user().c_str());
68 curl_easy_setopt (_curl, CURLOPT_PASSWORD, Config::instance()->tms_password().c_str());
69 if (!Config::instance()->tms_passive()) {
70 curl_easy_setopt(_curl, CURLOPT_FTPPORT, "-");
72 curl_easy_setopt(_curl, CURLOPT_VERBOSE, 1L);
73 curl_easy_setopt(_curl, CURLOPT_DEBUGFUNCTION, curl_debug_shim);
74 curl_easy_setopt(_curl, CURLOPT_DEBUGDATA, this);
78 CurlUploader::~CurlUploader ()
80 curl_easy_cleanup (_curl);
85 CurlUploader::create_directory (boost::filesystem::path)
87 /* this is done by libcurl */
92 CurlUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
96 /* Use generic_string so that we get forward-slashes in the path, even on Windows */
97 String::compose ("ftp://%1/%2/%3", Config::instance()->tms_ip(), Config::instance()->tms_path(), to.generic_string ()).c_str ()
100 dcp::File file(from, "rb");
102 throw NetworkError (String::compose (_("Could not open %1 to send"), from));
105 _transferred = &transferred;
106 _total_size = total_size;
108 auto const r = curl_easy_perform (_curl);
110 throw NetworkError (String::compose (_("Could not write to remote file (%1)"), curl_easy_strerror (r)));
118 CurlUploader::read_callback (void* ptr, size_t size, size_t nmemb)
120 DCPOMATIC_ASSERT (_file);
121 size_t const r = fread(ptr, size, nmemb, _file);
122 *_transferred += size * nmemb;
124 if (_total_size > 0) {
125 _set_progress ((double) *_transferred / _total_size);
133 CurlUploader::debug(CURL *, curl_infotype type, char* data, size_t size)
135 if (type == CURLINFO_TEXT && size > 0) {
136 LOG_GENERAL("CurlUploader: %1", string(data, size - 1));