1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
| #include "ftpsfilemanager.h"
#include <QIODevice>
#include <QSharedPointer>
#include <QUrl>
#include <QDebug>
// bytes
#define FTPS_CHUNK_SIZE 1024
FtpsFilemanager::FtpsFilemanager(const QUrl &url)
{
m_url = url;
curl_global_init(CURL_GLOBAL_DEFAULT);
m_curl = curl_easy_init();
curl_easy_setopt(m_curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(m_curl, CURLOPT_USERNAME, url.userName().toStdString().c_str() );
curl_easy_setopt(m_curl, CURLOPT_PASSWORD, url.password().toStdString().c_str() );
curl_easy_setopt(m_curl, CURLOPT_PORT , url.port() );
curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(m_curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1 );
m_tmpFile = QSharedPointer<QIODevice>(new QTemporaryFile());
m_tmpFileData = m_tmpFile.data();
if(!m_tmpFileData->open(QIODevice::ReadWrite))
{
m_error = "can't open temp file";
}
}
FtpsFilemanager::~FtpsFilemanager()
{
curl_easy_cleanup(m_curl);
curl_global_cleanup();
}
bool FtpsFilemanager::mkpath(const QString &path)
{
return true;
}
bool FtpsFilemanager::putfile(const QString &filename, const QByteArray &data)
{
/* enable uploading */
curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1);
/* specify target */
qDebug() << "Upload to " + m_url.toString() + "/" + filename;
curl_easy_setopt(m_curl, CURLOPT_URL, QString(m_url.toString() + "/" + filename).toStdString().c_str());
curl_easy_setopt(m_curl, CURLOPT_READFUNCTION,reader);
curl_easy_setopt(m_curl, CURLOPT_READDATA,&data);
m_code = curl_easy_perform(m_curl);
if(m_code != CURLE_OK)
{
m_error = "upload error";
return false;
}
return true;
}
bool FtpsFilemanager::putfile(const QString &filename, const QString &data)
{
return true;
}
QString FtpsFilemanager::destUrl()
{
return true;
}
QSharedPointer<QIODevice> FtpsFilemanager::getFile(const QString &path)
{
/* disable uploading */
curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 0);
/* specify target */
qDebug() << "Download file " + m_url.toString() + "/" + path;
curl_easy_setopt(m_curl, CURLOPT_URL, QString(m_url.toString() + "/" + path).toStdString().c_str());
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION,writer);
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA,m_tmpFileData);
m_code = curl_easy_perform(m_curl);
if(m_code != CURLE_OK)
{
m_error = "upload error";
return QSharedPointer<QIODevice>( new QTemporaryFile());
}
return m_tmpFile;
}
int FtpsFilemanager::writer(char *data, size_t size, size_t nmemb, QIODevice *buffer)
{
qDebug() << "DATA:" << data;
int result = 0;
if (buffer != NULL)
{
buffer->write(data);
result = size * nmemb;
}
return result;
}
size_t FtpsFilemanager::reader(char *data, size_t size, size_t nmemb, QByteArray *src)
{
// qDebug() << "get:" << data << " AND " << *src;
int result = 0;
if(src!=NULL)
{
QByteArray chunk = src->mid(0,FTPS_CHUNK_SIZE);
qDebug() << chunk;
data = chunk.data();
qDebug() << "input" << data;
result = chunk.size();
src->remove(0,FTPS_CHUNK_SIZE);
}
return result;
} |