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