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