1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
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.
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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
*/
#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 <iostream>
#include "i18n.h"
using std::string;
using std::cout;
using std::function;
static size_t
read_callback (void* ptr, size_t size, size_t nmemb, void* object)
{
CurlUploader* u = reinterpret_cast<CurlUploader*> (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<CurlUploader*>(userp)->debug(curl, type, data, size);
}
CurlUploader::CurlUploader (function<void (string)> set_status, function<void (float)> set_progress)
: Uploader (set_status, set_progress)
{
_curl = curl_easy_init ();
if (!_curl) {
throw NetworkError (_("Could not start transfer"));
}
curl_easy_setopt (_curl, CURLOPT_READFUNCTION, ::read_callback);
curl_easy_setopt (_curl, CURLOPT_READDATA, this);
curl_easy_setopt (_curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt (_curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
curl_easy_setopt (_curl, CURLOPT_READDATA, this);
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 ()
{
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,
/* 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 ()
);
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;
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)));
}
_file = nullptr;
}
size_t
CurlUploader::read_callback (void* ptr, size_t size, size_t nmemb)
{
DCPOMATIC_ASSERT (_file);
size_t const r = fread(ptr, size, nmemb, _file);
*_transferred += size * nmemb;
if (_total_size > 0) {
_set_progress ((double) *_transferred / _total_size);
}
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;
}
|