Repository navigation
Expand file tree
/
Copy pathfileuploader.cpp
More file actions
477 lines (431 loc) · 14.9 KB
/
Copy pathfileuploader.cpp
File metadata and controls
477 lines (431 loc) · 14.9 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright (C) 2026 zhutianhao <zhutianhao75@hotmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "fileuploader.h"
#include <QCloseEvent>
#include <QElapsedTimer>
#include <QFileInfo>
#include <QLabel>
#include <QLocale>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPointer>
#include <QPushButton>
#include <QSslError>
#include <QTimer>
#include <QUrl>
#include <QUrlQuery>
#include <QVBoxLayout>
#include "authmanager.h"
namespace {
QString formatSize(double bytes)
{
if (bytes < 0)
bytes = 0;
return QLocale().formattedDataSize(qint64(bytes), 1, QLocale::DataSizeSIFormat);
}
} // namespace
// ---------------------------------------------------------------- FileUploader
FileUploader::FileUploader(QObject *parent)
: QObject(parent)
{
m_nam = new QNetworkAccessManager(this);
}
void FileUploader::setAuthManager(AuthManager *mgr)
{
m_auth = mgr;
}
void FileUploader::setTarget(const Target &target)
{
m_target = target;
}
void FileUploader::addFiles(const QStringList &paths)
{
m_files.append(paths);
}
void FileUploader::start()
{
if (m_running)
return;
if (m_files.isEmpty()) {
emit finished(0, 0);
return;
}
m_running = true;
m_canceled = false;
m_finishedEmitted = false;
// 已缓存的授权头直接带上,省去一次 401 往返。
if (m_target.authHeader.isEmpty() && m_auth)
m_target.authHeader = m_auth->headerFor(m_target.ip, m_target.port);
m_ok = 0;
m_failed = 0;
m_index = -1;
m_bytesBefore = 0;
m_totalBytes = 0;
for (const QString &p : m_files)
m_totalBytes += QFileInfo(p).size();
m_clock.start();
m_lastMs = 0;
m_lastSent = 0;
m_speed = 0;
startNextFile();
}
void FileUploader::cancel()
{
if (!m_running)
return;
m_canceled = true;
m_file.close();
if (m_reply) {
QNetworkReply *r = m_reply;
m_reply = nullptr;
r->abort(); // 触发 finished,由 onChunkFinished 统一收尾
return;
}
finishAll();
}
void FileUploader::startNextFile()
{
if (m_canceled) {
finishAll();
return;
}
++m_index;
if (m_index >= m_files.count()) {
finishAll();
return;
}
const QString path = m_files.at(m_index);
m_curName = QFileInfo(path).fileName();
m_fileSize = QFileInfo(path).size();
m_offset = 0;
m_chunksSent = 0;
m_authTried = false;
// 覆盖与否按文件逐个询问:一次拖拽里的每个同名文件都需要用户单独确认。
m_overwrite = false;
m_overwriteAsked = false;
m_file.close();
m_file.setFileName(path);
if (!m_file.open(QIODevice::ReadOnly)) {
failCurrent(tr("无法读取文件"));
return;
}
emit progress(m_bytesBefore, m_totalBytes, m_speed, m_curName, m_index + 1,
m_files.count());
sendNextChunk();
}
void FileUploader::sendNextChunk()
{
if (m_canceled)
return;
// 已发过至少一块且写到末尾即完成;空文件也需先发一个空块以在接收端建文件。
if (m_offset >= m_fileSize && m_chunksSent > 0) {
finishCurrentFile();
return;
}
const qint64 want = qMin<qint64>(kChunkSize, m_fileSize - m_offset);
if (want > 0) {
m_chunk = m_file.read(want);
if (m_chunk.size() != want) {
failCurrent(tr("读取文件失败"));
return;
}
} else {
m_chunk.clear();
}
postChunk();
}
void FileUploader::postChunk()
{
if (m_canceled)
return;
QNetworkRequest req = buildRequest(m_curName, m_offset, m_fileSize);
if (!m_target.authHeader.isEmpty())
req.setRawHeader(QByteArrayLiteral("Authorization"), m_target.authHeader.toUtf8());
QNetworkReply *reply = m_nam->post(req, m_chunk);
m_reply = reply;
QPointer<FileUploader> guard(this);
// 自签证书(无授权模式下的 HTTPS 共享)不阻塞传输。
connect(reply, &QNetworkReply::sslErrors, reply,
[reply](const QList<QSslError> &) { reply->ignoreSslErrors(); });
connect(reply, &QNetworkReply::uploadProgress, this, [this, guard](qint64 sent, qint64) {
if (!guard)
return;
const qint64 done = m_bytesBefore + m_offset + sent;
const qint64 now = m_clock.elapsed();
const qint64 dt = now - m_lastMs;
if (dt >= 400) {
m_speed = double(done - m_lastSent) * 1000.0 / double(dt);
m_lastSent = done;
m_lastMs = now;
}
emit progress(done, m_totalBytes, m_speed, m_curName, m_index + 1, m_files.count());
});
connect(reply, &QNetworkReply::finished, this, [this, guard, reply]() {
if (!guard)
return;
onChunkFinished(reply);
});
}
void FileUploader::onChunkFinished(QNetworkReply *reply)
{
if (m_reply == reply)
m_reply = nullptr;
reply->deleteLater();
if (m_canceled) {
finishAll();
return;
}
const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// 收到 401:握手一次拿到授权头后重发当前块,不必重传整个文件。
if (status == 401) {
if (!m_authTried && m_auth) {
m_authTried = true;
QPointer<FileUploader> guard(this);
m_auth->ensureHeader(m_target.ip, m_target.port, m_target.secure,
[this, guard](const QString &header) {
if (!guard)
return;
if (m_canceled) {
finishAll();
return;
}
if (header.isEmpty()) {
failCurrent(tr("授权被拒绝"));
return;
}
m_target.authHeader = header;
postChunk(); // 重发当前块,不重新读文件
});
return;
}
failCurrent(tr("授权失败"));
return;
}
// 目标已存在:服务端默认不覆盖,先问用户,答复前暂停当前文件的传输。
if (status == 409 && !m_overwriteAsked) {
m_overwriteAsked = true;
emit overwriteRequested(m_curName);
return;
}
if (reply->error() != QNetworkReply::NoError || status < 200 || status >= 300) {
failCurrent(status == 409 ? tr("服务端不允许覆盖同名文件")
: reply->errorString());
return;
}
m_offset += m_chunk.size();
++m_chunksSent;
sendNextChunk();
}
void FileUploader::replyOverwrite(bool overwrite)
{
if (!m_running || m_canceled) {
finishAll();
return;
}
if (!overwrite) {
failCurrent(tr("目标已存在,已跳过"));
return;
}
// 带上 overwrite=1 重发当前块——m_chunk / m_offset 均未改变,无需重读文件。
m_overwrite = true;
postChunk();
}
void FileUploader::failCurrent(const QString &error)
{
m_file.close();
m_bytesBefore += m_fileSize; // 失败文件也计入进度,避免总进度回退
++m_failed;
emit fileFailed(m_curName, error);
// 排队进入下一个文件,避免大量连续失败时递归过深。
QMetaObject::invokeMethod(this, &FileUploader::startNextFile, Qt::QueuedConnection);
}
void FileUploader::finishCurrentFile()
{
m_file.close();
m_bytesBefore += m_fileSize;
++m_ok;
emit progress(m_bytesBefore, m_totalBytes, m_speed, m_curName, m_index + 1,
m_files.count());
startNextFile();
}
void FileUploader::finishAll()
{
m_file.close();
m_running = false;
if (m_finishedEmitted)
return;
m_finishedEmitted = true;
emit finished(m_ok, m_failed);
}
QNetworkRequest FileUploader::buildRequest(const QString &name, qint64 offset,
qint64 total) const
{
QUrl url;
url.setScheme(m_target.secure ? QStringLiteral("https") : QStringLiteral("http"));
url.setHost(m_target.ip);
url.setPort(m_target.port);
url.setPath(QStringLiteral("/upload")
+ (m_target.destRel.isEmpty() ? QStringLiteral("/") : m_target.destRel));
QUrlQuery q;
q.addQueryItem(QStringLiteral("name"), name);
q.addQueryItem(QStringLiteral("offset"), QString::number(offset));
q.addQueryItem(QStringLiteral("total"), QString::number(total));
// 服务端对已存在的同名文件默认拒绝(409),只有用户明确同意覆盖时才带上该标记。
if (m_overwrite)
q.addQueryItem(QStringLiteral("overwrite"), QStringLiteral("1"));
url.setQuery(q);
QNetworkRequest req(url);
req.setHeader(QNetworkRequest::ContentTypeHeader,
QByteArrayLiteral("application/octet-stream"));
return req;
}
// -------------------------------------------------------- UploadProgressDialog
UploadProgressDialog::UploadProgressDialog(QWidget *parent)
: Dtk::Widget::DDialog(parent)
{
setAttribute(Qt::WA_DeleteOnClose);
setTitle(tr("正在上传"));
setOnButtonClickedClose(false);
m_uploader = new FileUploader(this);
auto *content = new QWidget;
auto *vbox = new QVBoxLayout(content);
vbox->setContentsMargins(0, 0, 0, 0);
vbox->setSpacing(8);
m_fileLabel = new QLabel(tr("准备上传…"));
m_fileLabel->setWordWrap(true);
m_bar = new Dtk::Widget::DProgressBar;
m_bar->setRange(0, 100);
m_bar->setValue(0);
m_bar->setTextVisible(false);
m_bar->setFixedWidth(420);
m_statLabel = new QLabel(tr("0%"));
vbox->addWidget(m_fileLabel);
vbox->addWidget(m_bar);
vbox->addWidget(m_statLabel);
addContent(content);
addButton(tr("取消"));
connect(m_uploader, &FileUploader::progress, this, &UploadProgressDialog::updateUi);
connect(m_uploader, &FileUploader::finished, this, &UploadProgressDialog::onUploadFinished);
connect(m_uploader, &FileUploader::overwriteRequested,
this, &UploadProgressDialog::onOverwriteRequested);
connect(this, &DDialog::buttonClicked, this, [this](int, const QString &) {
// 上传中:取消;已完成(按钮已换成“关闭”):由 setOnButtonClickedClose 关闭。
if (!m_done)
m_uploader->cancel();
});
}
UploadProgressDialog::~UploadProgressDialog()
{
m_closing = true;
// 析构期间不再回调界面,仅中止在途请求。
disconnect(m_uploader, nullptr, this, nullptr);
if (m_uploader->isRunning())
m_uploader->cancel();
}
void UploadProgressDialog::upload(const QString &machineName, const QString &ip, quint16 port,
bool secure, const QString &destRel,
const QStringList &files, AuthManager *auth)
{
setTitle(tr("正在上传到 %1").arg(machineName));
FileUploader::Target t;
t.ip = ip;
t.port = port;
t.secure = secure;
t.destRel = destRel;
m_uploader->setAuthManager(auth);
m_uploader->setTarget(t);
m_uploader->addFiles(files);
m_uploader->start();
show();
raise();
activateWindow();
}
void UploadProgressDialog::onOverwriteRequested(const QString &name)
{
auto *dlg = new Dtk::Widget::DDialog(this);
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->setTitle(tr("文件已存在"));
dlg->setMessage(tr("对方的共享目录中已有同名文件:\n%1\n是否覆盖?").arg(name));
dlg->addButton(tr("跳过"), false, Dtk::Widget::DDialog::ButtonNormal);
dlg->addButton(tr("覆盖"), true, Dtk::Widget::DDialog::ButtonWarning);
// 第二个按钮(索引 1)为「覆盖」;finished 也会在点了按钮后触发,
// 用它标记已经答复过,避免重复调用 replyOverwrite。
dlg->setProperty("decided", false);
connect(dlg, &Dtk::Widget::DDialog::buttonClicked, this, [this, dlg](int idx) {
dlg->setProperty("decided", true);
m_uploader->replyOverwrite(idx == 1);
dlg->close();
});
connect(dlg, &Dtk::Widget::DDialog::finished, this, [this, dlg]() {
// 直接关闭(未点按钮)视为跳过。
if (!dlg->property("decided").toBool() && m_uploader->isRunning())
m_uploader->replyOverwrite(false);
});
dlg->exec();
}
void UploadProgressDialog::updateUi(qint64 sent, qint64 total, double speed,
const QString &name, int fileIndex, int fileCount)
{
if (m_closing)
return;
const int percent = total > 0 ? int(qMin<qint64>(100, sent * 100 / total)) : 0;
m_bar->setValue(percent);
m_fileLabel->setText(tr("正在上传:%1(第 %2/%3 个)")
.arg(name, QString::number(fileIndex), QString::number(fileCount)));
m_statLabel->setText(tr("%1% %2 / %3%4")
.arg(QString::number(percent), formatSize(sent), formatSize(total),
speed > 0 ? tr(" %1/s").arg(formatSize(speed)) : QString()));
}
void UploadProgressDialog::onUploadFinished(int ok, int fail)
{
m_done = true;
clearButtons();
addButton(tr("关闭"));
setOnButtonClickedClose(true);
if (m_closing) {
close();
return;
}
if (fail == 0) {
setTitle(tr("上传完成"));
m_fileLabel->setText(tr("已上传 %n 个文件", "", ok));
m_bar->setValue(100);
m_statLabel->setText(formatSize(m_uploader->totalBytes()));
// 全部成功时短暂停留后自动关闭,减少一次点击。
QTimer::singleShot(900, this, [this]() {
if (!m_closing)
close();
});
} else {
setTitle(tr("上传结束"));
m_fileLabel->setText(tr("成功 %1 个,失败 %2 个")
.arg(QString::number(ok), QString::number(fail)));
}
emit finished(ok, fail);
}
void UploadProgressDialog::closeEvent(QCloseEvent *event)
{
if (!m_done && m_uploader->isRunning()) {
// 上传中关闭:先中止传输,等收尾完成后再真正关闭。
m_closing = true;
m_uploader->cancel();
event->ignore();
return;
}
DDialog::closeEvent(event);
}