forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.cpp
237 lines (206 loc) · 5.62 KB
/
upload.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (C) 2014-2018 Daniel Nicoletti <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "upload_p.h"
#include "common.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTemporaryFile>
using namespace Cutelyst;
QString Upload::filename() const
{
Q_D(const Upload);
return d->filename;
}
QString Upload::contentType() const
{
Q_D(const Upload);
return d->headers.contentType();
}
Headers Upload::headers() const
{
Q_D(const Upload);
return d->headers;
}
bool Upload::save(const QString &newName)
{
Q_D(Upload);
bool error = false;
QString fileTemplate = QStringLiteral("%1/qt_temp.XXXXXX");
QFile out(fileTemplate.arg(QFileInfo(newName).path()));
if (!out.open(QIODevice::ReadWrite)) {
error = true;
}
if (error) {
out.close();
setErrorString(QLatin1String("Failed to open file for saving: ") + out.errorString());
qCWarning(CUTELYST_UPLOAD) << errorString();
} else {
qint64 posOrig = d->pos;
seek(0);
char block[4096];
qint64 totalRead = 0;
while (!atEnd()) {
qint64 in = read(block, sizeof(block));
if (in <= 0)
break;
totalRead += in;
if (in != out.write(block, in)) {
setErrorString(QLatin1String("Failure to write block"));
qCWarning(CUTELYST_UPLOAD) << errorString();
error = true;
break;
}
}
if (error) {
out.remove();
}
if (!error && !out.rename(newName)) {
error = true;
setErrorString(QStringLiteral("Cannot create %1 for output").arg(newName));
qCWarning(CUTELYST_UPLOAD) << errorString();
}
if (error) {
out.remove();
}
seek(posOrig);
}
return !error;
}
QTemporaryFile *Upload::createTemporaryFile(const QString &templateName)
{
#ifndef QT_NO_TEMPORARYFILE
Q_D(Upload);
QTemporaryFile *ret;
if (templateName.isEmpty()) {
ret = new QTemporaryFile(this);
} else {
ret = new QTemporaryFile(templateName, this);
}
if (ret->open()) {
bool error = false;
qint64 posOrig = d->pos;
seek(0);
char block[4096];
qint64 totalRead = 0;
while (!atEnd()) {
qint64 in = read(block, sizeof(block));
if (in <= 0)
break;
totalRead += in;
if (in != ret->write(block, in)) {
setErrorString(QLatin1String("Failure to write block"));
qCWarning(CUTELYST_UPLOAD) << errorString();
error = true;
break;
}
}
if (error) {
ret->remove();
}
ret->seek(0);
seek(posOrig);
return ret;
} else {
qCWarning(CUTELYST_UPLOAD) << "Failed to open temporary file.";
}
delete ret;
#else
Q_UNUSED(templateName)
#endif
return nullptr;
}
qint64 Upload::pos() const
{
Q_D(const Upload);
return d->pos;
}
qint64 Upload::size() const
{
Q_D(const Upload);
return d->endOffset - d->startOffset;
}
bool Upload::seek(qint64 pos)
{
Q_D(Upload);
if (pos <= size()) {
QIODevice::seek(pos);
d->pos = pos;
return true;
}
return false;
}
Upload::Upload(UploadPrivate *prv) :
d_ptr(prv)
{
Q_D(Upload);
open(prv->device->openMode());
const QString disposition = prv->headers.contentDisposition();
int start = disposition.indexOf(QLatin1String("name=\""));
if (start != -1) {
start += 6;
int end = disposition.indexOf(QLatin1Char('"'), start);
if (end != -1) {
d->name = disposition.mid(start, end - start);
}
}
start = disposition.indexOf(QLatin1String("filename=\""));
if (start != -1) {
start += 10;
int end = disposition.indexOf(QLatin1Char('"'), start);
if (end != -1) {
d->filename = disposition.mid(start, end - start);
}
}
}
Upload::~Upload()
{
delete d_ptr;
}
QString Upload::name() const
{
Q_D(const Upload);
return d->name;
}
qint64 Upload::readData(char *data, qint64 maxlen)
{
Q_D(Upload);
qint64 posOrig = d->device->pos();
d->device->seek(d->startOffset + d->pos);
qint64 len = d->device->read(data,
qMin(size() - d->pos, maxlen));
d->device->seek(posOrig);
d->pos += len;
return len;
}
qint64 Upload::readLineData(char *data, qint64 maxlen)
{
Q_D(Upload);
qint64 posOrig = d->device->pos();
d->device->seek(d->startOffset + d->pos);
qint64 len = d->device->readLine(data,
qMin(size() - d->pos, maxlen));
d->device->seek(posOrig);
d->pos += len;
return len;
}
qint64 Upload::writeData(const char *data, qint64 maxSize)
{
return -1;
}
#include "moc_upload.cpp"