Supporters update.
[dcpomatic.git] / src / lib / curl_uploader.cc
1 /*
2     Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "curl_uploader.h"
23 #include "dcpomatic_log.h"
24 #include "exceptions.h"
25 #include "config.h"
26 #include "cross.h"
27 #include "compose.hpp"
28 #include "dcpomatic_assert.h"
29 #include <iostream>
30
31 #include "i18n.h"
32
33
34 using std::string;
35 using std::cout;
36 using std::function;
37
38
39 static size_t
40 read_callback (void* ptr, size_t size, size_t nmemb, void* object)
41 {
42         CurlUploader* u = reinterpret_cast<CurlUploader*> (object);
43         return u->read_callback (ptr, size, nmemb);
44 }
45
46
47 static int
48 curl_debug_shim (CURL* curl, curl_infotype type, char* data, size_t size, void* userp)
49 {
50         return reinterpret_cast<CurlUploader*>(userp)->debug(curl, type, data, size);
51 }
52
53
54 CurlUploader::CurlUploader (function<void (string)> set_status, function<void (float)> set_progress)
55         : Uploader (set_status, set_progress)
56 {
57         _curl = curl_easy_init ();
58         if (!_curl) {
59                 throw NetworkError (_("Could not start transfer"));
60         }
61
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, "-");
71         }
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);
75 }
76
77
78 CurlUploader::~CurlUploader ()
79 {
80         curl_easy_cleanup (_curl);
81 }
82
83
84 void
85 CurlUploader::create_directory (boost::filesystem::path)
86 {
87         /* this is done by libcurl */
88 }
89
90
91 void
92 CurlUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
93 {
94         curl_easy_setopt (
95                 _curl, CURLOPT_URL,
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 ()
98                 );
99
100         dcp::File file(from, "rb");
101         if (!file) {
102                 throw NetworkError (String::compose (_("Could not open %1 to send"), from));
103         }
104         _file = file.get();
105         _transferred = &transferred;
106         _total_size = total_size;
107
108         auto const r = curl_easy_perform (_curl);
109         if (r != CURLE_OK) {
110                 throw NetworkError (String::compose (_("Could not write to remote file (%1)"), curl_easy_strerror (r)));
111         }
112
113         _file = nullptr;
114 }
115
116
117 size_t
118 CurlUploader::read_callback (void* ptr, size_t size, size_t nmemb)
119 {
120         DCPOMATIC_ASSERT (_file);
121         size_t const r = fread(ptr, size, nmemb, _file);
122         *_transferred += size * nmemb;
123
124         if (_total_size > 0) {
125                 _set_progress ((double) *_transferred / _total_size);
126         }
127
128         return r;
129 }
130
131
132 int
133 CurlUploader::debug(CURL *, curl_infotype type, char* data, size_t size)
134 {
135         if (type == CURLINFO_TEXT && size > 0) {
136                 LOG_GENERAL("CurlUploader: %1", string(data, size - 1));
137         }
138         return 0;
139 }
140