X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fcurl_uploader.cc;h=389a5d6de50b45859401feb829713f6e793a6ccd;hb=3ffd0163026be24e5373e0674c3301ed37546e44;hp=d996fde5365c86c50ff9741c1391989ebd363b42;hpb=1858190cff2f960f3d1f0a5cc02c69da86088f5b;p=dcpomatic.git diff --git a/src/lib/curl_uploader.cc b/src/lib/curl_uploader.cc index d996fde53..389a5d6de 100644 --- a/src/lib/curl_uploader.cc +++ b/src/lib/curl_uploader.cc @@ -1,34 +1,40 @@ /* - Copyright (C) 2015 Carl Hetherington + Copyright (C) 2015-2021 Carl Hetherington - This program is free software; you can redistribute it and/or modify + This file is part of DCP-o-matic. + + DCP-o-matic 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, + DCP-o-matic 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. + along with DCP-o-matic. If not, see . */ + #include "curl_uploader.h" +#include "dcpomatic_log.h" #include "exceptions.h" #include "config.h" #include "cross.h" #include "compose.hpp" +#include "dcpomatic_assert.h" #include #include "i18n.h" + using std::string; using std::cout; -using boost::function; +using std::function; + static size_t read_callback (void* ptr, size_t size, size_t nmemb, void* object) @@ -37,11 +43,16 @@ read_callback (void* ptr, size_t size, size_t nmemb, void* object) return u->read_callback (ptr, size, nmemb); } + +static int +curl_debug_shim (CURL* curl, curl_infotype type, char* data, size_t size, void* userp) +{ + return reinterpret_cast(userp)->debug(curl, type, data, size); +} + + CurlUploader::CurlUploader (function set_status, function set_progress) : Uploader (set_status, set_progress) - , _file (0) - , _transferred (0) - , _total_size (0) { _curl = curl_easy_init (); if (!_curl) { @@ -53,51 +64,61 @@ CurlUploader::CurlUploader (function set_status, functiontms_user().c_str ()); - curl_easy_setopt (_curl, CURLOPT_PASSWORD, Config::instance()->tms_password().c_str ()); + curl_easy_setopt (_curl, CURLOPT_USERNAME, Config::instance()->tms_user().c_str()); + curl_easy_setopt (_curl, CURLOPT_PASSWORD, Config::instance()->tms_password().c_str()); + if (!Config::instance()->tms_passive()) { + curl_easy_setopt(_curl, CURLOPT_FTPPORT, "-"); + } + curl_easy_setopt(_curl, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(_curl, CURLOPT_DEBUGFUNCTION, curl_debug_shim); + curl_easy_setopt(_curl, CURLOPT_DEBUGDATA, this); } + CurlUploader::~CurlUploader () { - if (_file) { - fclose (_file); - } curl_easy_cleanup (_curl); } + void CurlUploader::create_directory (boost::filesystem::path) { /* this is done by libcurl */ } + void CurlUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size) { curl_easy_setopt ( _curl, CURLOPT_URL, - String::compose ("ftp://%1/%2/%3", Config::instance()->tms_ip(), Config::instance()->tms_path(), to.string ()).c_str () + /* Use generic_string so that we get forward-slashes in the path, even on Windows */ + String::compose ("ftp://%1/%2/%3", Config::instance()->tms_ip(), Config::instance()->tms_path(), to.generic_string ()).c_str () ); - _file = fopen_boost (from, "rb"); - if (!_file) { + dcp::File file(from, "rb"); + if (!file) { throw NetworkError (String::compose (_("Could not open %1 to send"), from)); } + _file = file.get(); _transferred = &transferred; _total_size = total_size; - CURLcode const r = curl_easy_perform (_curl); + auto const r = curl_easy_perform (_curl); if (r != CURLE_OK) { throw NetworkError (String::compose (_("Could not write to remote file (%1)"), curl_easy_strerror (r))); } - fclose (_file); + _file = nullptr; } + size_t CurlUploader::read_callback (void* ptr, size_t size, size_t nmemb) { - size_t const r = fread (ptr, size, nmemb, _file); + DCPOMATIC_ASSERT (_file); + size_t const r = fread(ptr, size, nmemb, _file); *_transferred += size * nmemb; if (_total_size > 0) { @@ -106,3 +127,14 @@ CurlUploader::read_callback (void* ptr, size_t size, size_t nmemb) return r; } + + +int +CurlUploader::debug(CURL *, curl_infotype type, char* data, size_t size) +{ + if (type == CURLINFO_TEXT && size > 0) { + LOG_GENERAL("CurlUploader: %1", string(data, size - 1)); + } + return 0; +} +