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 "exceptions.h"
24 #include "config.h"
25 #include "cross.h"
26 #include "compose.hpp"
27 #include "dcpomatic_assert.h"
28 #include <iostream>
29
30 #include "i18n.h"
31
32
33 using std::string;
34 using std::cout;
35 using std::function;
36
37
38 static size_t
39 read_callback (void* ptr, size_t size, size_t nmemb, void* object)
40 {
41         CurlUploader* u = reinterpret_cast<CurlUploader*> (object);
42         return u->read_callback (ptr, size, nmemb);
43 }
44
45
46 CurlUploader::CurlUploader (function<void (string)> set_status, function<void (float)> set_progress)
47         : Uploader (set_status, set_progress)
48 {
49         _curl = curl_easy_init ();
50         if (!_curl) {
51                 throw NetworkError (_("Could not start transfer"));
52         }
53
54         curl_easy_setopt (_curl, CURLOPT_READFUNCTION, ::read_callback);
55         curl_easy_setopt (_curl, CURLOPT_READDATA, this);
56         curl_easy_setopt (_curl, CURLOPT_UPLOAD, 1L);
57         curl_easy_setopt (_curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
58         curl_easy_setopt (_curl, CURLOPT_READDATA, this);
59         curl_easy_setopt (_curl, CURLOPT_USERNAME, Config::instance()->tms_user().c_str());
60         curl_easy_setopt (_curl, CURLOPT_PASSWORD, Config::instance()->tms_password().c_str());
61 }
62
63
64 CurlUploader::~CurlUploader ()
65 {
66         curl_easy_cleanup (_curl);
67 }
68
69
70 void
71 CurlUploader::create_directory (boost::filesystem::path)
72 {
73         /* this is done by libcurl */
74 }
75
76
77 void
78 CurlUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
79 {
80         curl_easy_setopt (
81                 _curl, CURLOPT_URL,
82                 /* Use generic_string so that we get forward-slashes in the path, even on Windows */
83                 String::compose ("ftp://%1/%2/%3", Config::instance()->tms_ip(), Config::instance()->tms_path(), to.generic_string ()).c_str ()
84                 );
85
86         dcp::File file(from, "rb");
87         if (!file) {
88                 throw NetworkError (String::compose (_("Could not open %1 to send"), from));
89         }
90         _file = file.get();
91         _transferred = &transferred;
92         _total_size = total_size;
93
94         auto const r = curl_easy_perform (_curl);
95         if (r != CURLE_OK) {
96                 throw NetworkError (String::compose (_("Could not write to remote file (%1)"), curl_easy_strerror (r)));
97         }
98
99         _file = nullptr;
100 }
101
102
103 size_t
104 CurlUploader::read_callback (void* ptr, size_t size, size_t nmemb)
105 {
106         DCPOMATIC_ASSERT (_file);
107         size_t const r = fread(ptr, size, nmemb, _file);
108         *_transferred += size * nmemb;
109
110         if (_total_size > 0) {
111                 _set_progress ((double) *_transferred / _total_size);
112         }
113
114         return r;
115 }