Remove debug code.
[dcpomatic.git] / src / lib / curl_uploader.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "curl_uploader.h"
21 #include "exceptions.h"
22 #include "config.h"
23 #include "cross.h"
24 #include "compose.hpp"
25
26 #include "i18n.h"
27
28 using std::string;
29 using std::cout;
30 using boost::function;
31
32 static size_t
33 read_callback (void* ptr, size_t size, size_t nmemb, void* object)
34 {
35         CurlUploader* u = reinterpret_cast<CurlUploader*> (object);
36         return u->read_callback (ptr, size, nmemb);
37 }
38
39 CurlUploader::CurlUploader (function<void (string)> set_status, function<void (float)> set_progress)
40         : Uploader (set_status, set_progress)
41         , _file (0)
42         , _transferred (0)
43         , _total_size (0)
44 {
45         _curl = curl_easy_init ();
46         if (!_curl) {
47                 throw NetworkError (_("Could not start transfer"));
48         }
49
50         curl_easy_setopt (_curl, CURLOPT_READFUNCTION, ::read_callback);
51         curl_easy_setopt (_curl, CURLOPT_READDATA, this);
52         curl_easy_setopt (_curl, CURLOPT_UPLOAD, 1L);
53         curl_easy_setopt (_curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
54         curl_easy_setopt (_curl, CURLOPT_READDATA, this);
55         curl_easy_setopt (_curl, CURLOPT_USERNAME, Config::instance()->tms_user().c_str ());
56         curl_easy_setopt (_curl, CURLOPT_PASSWORD, Config::instance()->tms_password().c_str ());
57 }
58
59 CurlUploader::~CurlUploader ()
60 {
61         if (_file) {
62                 fclose (_file);
63         }
64         curl_easy_cleanup (_curl);
65 }
66
67 void
68 CurlUploader::create_directory (boost::filesystem::path)
69 {
70         /* this is done by libcurl */
71 }
72
73 void
74 CurlUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
75 {
76         curl_easy_setopt (
77                 _curl, CURLOPT_URL,
78                 String::compose ("ftp://%1/%2/%3", Config::instance()->tms_ip(), Config::instance()->tms_path(), to.string ()).c_str ()
79                 );
80
81         _file = fopen_boost (from, "rb");
82         if (!_file) {
83                 throw NetworkError (String::compose (_("Could not open %1 to send"), from));
84         }
85         _transferred = &transferred;
86         _total_size = total_size;
87
88         CURLcode const r = curl_easy_perform (_curl);
89         if (r != CURLE_OK) {
90                 throw NetworkError (String::compose (_("Could not write to remote file (%1)"), curl_easy_strerror (r)));
91         }
92
93         fclose (_file);
94 }
95
96 size_t
97 CurlUploader::read_callback (void* ptr, size_t size, size_t nmemb)
98 {
99         size_t const r = fread (ptr, size, nmemb, _file);
100         *_transferred += size * nmemb;
101
102         if (_total_size > 0) {
103                 _set_progress ((double) *_transferred / _total_size);
104         }
105
106         return r;
107 }