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