-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpclient.cpp
1033 lines (881 loc) · 33.5 KB
/
ftpclient.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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ftpclient.h"
#include <QtConcurrent/QtConcurrent>
#include <QJsonDocument>
#include <QJsonArray>
#include <QFileDialog>
#include <QDesktopServices>
#include <QDebug>
#include <atomic>
#include <QGuiApplication>
#include <QJsonObject>
#include "allfileslistmodel.h"
#include "selectpathlistmodel.h"
#include "uploadlistmodel.h"
#include "downloadlistmodel.h"
#include "qaesencryption.h"
static QString BASE_URL = "";
static QString USER_PWD = "";
static std::atomic<int> flagCancelUpload;
static std::atomic<int> flagCancelDownload;
static std::string strBufferData = "";
static QList<QString> uploadFileList;
static QList<QString> downloadFileList;
static QString donwloadPath = "";
static std::atomic<double> uploadTotalSize;
static std::atomic<double> uploadSize;
static std::atomic<double> lastUploadSize;
static std::atomic<double> downloadTotalSize;
static std::atomic<double> downloadSize;
static std::atomic<double> lastDownloadSize;
static qint64 diskUsage = 0;
const QString CRYPTO_KEY = "!@#678TgaE10~%^*";
#define ValueMax_B pow(1024,1)
#define ValueMax_KB pow(1024,2)
#define ValueMax_MB pow(1024,3)
#define ValueMax_G pow(1024,4)
FtpClient::FtpClient(QObject *parent)
: QObject{parent}
{
curl_global_init(CURL_GLOBAL_ALL);
m_strCurrentPath = "";
m_strForwardPath = "";
flagCancelUpload.store(0);
flagCancelDownload.store(0);
m_timerUpload.setInterval(1000);
m_timerDownload.setInterval(1000);
connect(&m_timerUpload, &QTimer::timeout, this, &FtpClient::slotUploadTimeout);
connect(&m_timerDownload, &QTimer::timeout, this, &FtpClient::slotDownloadTimeout);
connect(this, &FtpClient::signaUploadComplete, this, &FtpClient::slotUploadComplete);
connect(this, &FtpClient::signaDownloadComplete, this, &FtpClient::slotDownloadComplete);
QSettings setting("HKEY_CURRENT_USER\\SOFTWARE\\SRC",
QSettings::NativeFormat);
QString userName = decryptoData(setting.value("userName").toString());
QString userPass = decryptoData(setting.value("userToken").toString());
USER_PWD = userName + ":" + userPass;
m_strUserName = userName.replace(userName.mid(3, 4),"****");
donwloadPath = setting.value("downLoadPath", "C:\\YouBoxDownload").toString();
QString strApplicationDir = QGuiApplication::applicationDirPath();
QDir dirs(strApplicationDir);
dirs.cdUp();
QString cfgPath = dirs.path() + "/server-config.ini";
QSettings settings(cfgPath, QSettings::IniFormat);
QString srvHost = settings.value("server-config/disk_srv_pub").toString();
QString srvPort = settings.value("server-config/disk_srv_port").toString();
BASE_URL = "ftp://" + srvHost + ":" + srvPort + "/";
qDebug() << BASE_URL;
m_hashFileTypeIcon.insert("EXE", FilesType{"../res/icon_exe_big",u8"应用程序"});
m_hashFileTypeIcon.insert("ZIP", FilesType{"../res/icon_zip_big.png",u8"ZIP压缩文件"});
m_hashFileTypeIcon.insert("RAR", FilesType{"../res/icon_zip_big.png",u8"RAR压缩文件"});
m_hashFileTypeIcon.insert("TXT", FilesType{"../res/icon_txt_big.png",u8"文本文件"});
m_hashFileTypeIcon.insert("XLS", FilesType{"../res/icon_xls_big.png",u8"Excel文档"});
m_hashFileTypeIcon.insert("XLSX",FilesType{"../res/icon_xls_big.png",u8"Excel文档"});
m_hashFileTypeIcon.insert("MP3", FilesType{"../res/icon_video_big.png",u8"MP3音频文件"});
m_hashFileTypeIcon.insert("MP4", FilesType{"../res/icon_video_big.png",u8"MP4视频文件"});
m_hashFileTypeIcon.insert("AVI", FilesType{"../res/icon_video_big.png",u8"AVI视频文件"});
m_hashFileTypeIcon.insert("MKV", FilesType{"../res/icon_video_big.png",u8"MKV视频文件"});
m_hashFileTypeIcon.insert("RMVB",FilesType{"../res/icon_video_big.png",u8"RMVB视频文件"});
m_hashFileTypeIcon.insert("PPT", FilesType{"../res/icon_ppt_big.png",u8"PPT文档"});
m_hashFileTypeIcon.insert("DOC", FilesType{"../res/icon_word_big.png",u8"DOC文档"});
m_hashFileTypeIcon.insert("PDF", FilesType{"../res/icon_pdf_big.png",u8"PDF文件"});
m_hashFileTypeIcon.insert("PNG", FilesType{"../res/icon_image_big.png",u8"PNG图片文件"});
m_hashFileTypeIcon.insert("JPG", FilesType{"../res/icon_image_big.png",u8"JPG图片文件"});
m_hashFileTypeIcon.insert("JPEG", FilesType{"../res/icon_image_big.png",u8"JPEG图片文件"});
m_hashFileTypeIcon.insert("ISO", FilesType{"../res/icon_iso_big.png",u8"镜像文件"});
m_hashFileTypeIcon.insert("DLL", FilesType{"../res/icon_dll_big.png",u8"应用程序扩展"});
}
FtpClient::~FtpClient()
{
curl_global_cleanup();
}
QString FtpClient::formatSize(qint64 size)
{
QString result = "0";
double value = 0.00;
if(size > 0 && size < ValueMax_B)
{
result = QString("%1").arg(size)+QString(" B");
}
else if((size >= ValueMax_B) && (size < ValueMax_KB))
{
value = size/ValueMax_B;
result = QString::number(value,'f',1)+QString(" KB");
}
else if((size >= ValueMax_KB) && (size < ValueMax_MB))
{
value = size/ValueMax_KB;
result = QString::number(value,'f',1)+QString(" MB");
}
else if((size >= ValueMax_MB) && (size < ValueMax_G))
{
value = size/ValueMax_MB;
result = QString::number(value,'f',1)+QString(" GB");
}
return result;
}
size_t FtpClient::OnWriteCallBack(char* buffer, size_t size,
size_t nitems, void* outstream)
{
if (nullptr == buffer || nullptr == outstream)
return size * nitems;
std::string* pBuf = reinterpret_cast<std::string*>(outstream);
pBuf->append(buffer, size * nitems);
return size * nitems;
}
//解析目录信息
void FtpClient::ParseDirInfo(std::string str)
{
QRegExp dosPattern(QLatin1String("^(\\d\\d-\\d\\d-\\d\\d\\d?\\d?\\ \\ \\d\\d:\\d\\d[AP]M)\\s+"
"(<DIR>|\\d+)\\s+(\\S.*)$"));
QString bufferStr = QString::fromLatin1(str.data());
QStringList strList = bufferStr.split("\r\n");
QJsonArray jsonArrDir, jsonArrFiles;
for (int i = 0; i < strList.size(); ++i)
{
QUrlInfo info;
if (dosPattern.indexIn(strList.at(i).trimmed()) != 0)
{
continue;
}
if(!parseDosDir(dosPattern.capturedTexts(), &info))
{
continue;
}
QJsonObject jsonItem;
jsonItem.insert("fileTime", info.lastModified().toLocalTime().toString("yyyy-MM-dd hh:mm:ss"));
jsonItem.insert("isFolder", info.isDir());
jsonItem.insert("fileName", QString::fromLocal8Bit(info.name().toLatin1()));
if (info.isDir())
{
jsonItem.insert("fileDesc", u8"文件夹");
jsonItem.insert("fileIcon", "../res/icon_folder_big.png");
jsonItem.insert("fileSize", 0);
jsonItem.insert("fileSizeFormat", "");
jsonItem.insert("fileTransfer", "");
jsonArrDir.push_back(jsonItem);
} else {
QString fileType = info.name().mid(info.name().lastIndexOf(".") + 1, info.name().size()).toUpper();
auto iter = m_hashFileTypeIcon.find(fileType);
if(iter != m_hashFileTypeIcon.end())
{
jsonItem.insert("fileDesc", iter.value().typeDesc);
jsonItem.insert("fileIcon", iter.value().typeIcon);
} else {
jsonItem.insert("fileDesc", u8"未知文件");
jsonItem.insert("fileIcon", "../res/icon_unknow_big.png");
}
jsonItem.insert("fileSize", info.size());
jsonItem.insert("fileSizeFormat", formatSize(info.size()));
jsonItem.insert("fileTransfer", formatSize(info.size()));
jsonArrFiles.push_back(jsonItem);
}
}
for (auto iter : jsonArrFiles)
{
jsonArrDir.append(iter);
}
emit updateFileInfo(jsonArrDir);
}
bool FtpClient::parseDosDir(const QStringList &tokens, QUrlInfo *info)
{
// DOS style, 3 + 1 entries
// 01-16-02 11:14AM <DIR> epsgroup
// 06-05-03 03:19PM 1973 readme.txt
if (tokens.size() != 4)
return false;
QString name = tokens.at(3);
info->setName(name);
info->setSymLink(name.toLower().endsWith(QLatin1String(".lnk")));
if (tokens.at(2) == QLatin1String("<DIR>")) {
info->setFile(false);
info->setDir(true);
} else {
info->setFile(true);
info->setDir(false);
info->setSize(tokens.at(2).toLongLong());
}
// Note: We cannot use QFileInfo; permissions are for the server-side
// machine, and QFileInfo's behavior depends on the local platform.
int permissions = QUrlInfo::ReadOwner | QUrlInfo::WriteOwner
| QUrlInfo::ReadGroup | QUrlInfo::WriteGroup
| QUrlInfo::ReadOther | QUrlInfo::WriteOther;
QString ext;
int extIndex = name.lastIndexOf(QLatin1Char('.'));
if (extIndex != -1)
ext = name.mid(extIndex + 1);
if (ext == QLatin1String("exe") || ext == QLatin1String("bat") || ext == QLatin1String("com"))
permissions |= QUrlInfo::ExeOwner | QUrlInfo::ExeGroup | QUrlInfo::ExeOther;
info->setPermissions(permissions);
info->setReadable(true);
info->setWritable(info->isFile());
QDateTime dateTime;
#ifndef QT_NO_DATESTRING
dateTime = QLocale::c().toDateTime(tokens.at(1), QLatin1String("MM-dd-yy hh:mmAP"));
if (dateTime.date().year() < 1971) {
dateTime.setDate(QDate(dateTime.date().year() + 100,
dateTime.date().month(),
dateTime.date().day()));
}
#endif
info->setLastModified(dateTime);
return true;
}
//获取目录列表
void FtpClient::getDirList(bool rootDir, bool isBack, bool refresh, const QVariant& dirPath)
{
emit updateUserName(m_strUserName);
if (!refresh)
{
//根目录
if (rootDir)
{
if (isBack)
{
SelectPathListModel::pSelectPathListModel->delFolder();
} else {
QJsonObject item;
item.insert("dirName", u8"我的文件");
item.insert("showArrow", true);
SelectPathListModel::pSelectPathListModel->addFolder(item);
}
} else {
if (m_strCurrentPath.isEmpty())
{
m_strCurrentPath.append(dirPath.toString());
} else {
m_strCurrentPath.append("/");
m_strCurrentPath.append(dirPath.toString());
}
if (isBack)
{
SelectPathListModel::pSelectPathListModel->delFolder();
} else {
QJsonObject item;
item.insert("dirName", dirPath.toString());
item.insert("showArrow", true);
SelectPathListModel::pSelectPathListModel->addFolder(item);
}
}
} else {
if (m_strCurrentPath.isEmpty())
{
m_strCurrentPath.append(dirPath.toString());
} else {
m_strCurrentPath.append("/");
m_strCurrentPath.append(dirPath.toString());
}
}
connect(this, &FtpClient::updateFileInfo,
AllFilesListModel::pAllFileListModel,
&AllFilesListModel::updateDirList, Qt::UniqueConnection);
getDir(m_strCurrentPath);
//refreshUsege();
}
//获取目录信息
void FtpClient::getDir(const QString& dirPath)
{
std::string strBuffer = "";
QtConcurrent::run([=](){
std::string strCmd = "CWD ";
strCmd.append(dirPath.toLocal8Bit());
struct curl_slist* headerlist = NULL;
headerlist = curl_slist_append(headerlist, strCmd.data());
CURL* pCurl = curl_easy_init();
curl_easy_setopt(pCurl, CURLOPT_URL, BASE_URL.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_USERPWD, USER_PWD.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_QUOTE, headerlist);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnWriteCallBack);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &strBuffer);
curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 5L);
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 0);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPIDLE, 1000L);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPINTVL, 60L);
CURLcode curlCode = curl_easy_perform(pCurl);
curl_slist_free_all(headerlist);
if (curlCode != CURLE_OK)
{
emit showMessage(true, u8"个人云盘连接失败,请联系技术支持");
qDebug() <<"dirlist error: "<< curl_easy_strerror(curlCode);
} else {
ParseDirInfo(strBuffer);
}
curl_easy_cleanup(pCurl);
});
}
//返回上层目录
void FtpClient::backDir()
{
if (m_strCurrentPath.isEmpty())
{
return;
}
int pos = m_strCurrentPath.lastIndexOf("/");
if (pos == -1)
{
m_strCurrentPath = "";
getDirList(true, true, false, m_strCurrentPath);
} else {
QString strBackPath = m_strCurrentPath.mid(0, pos);
m_strCurrentPath = "";
getDirList(false, true, false, strBackPath);
}
}
//前进目录
void FtpClient::forwardDir()
{
}
//跳转到目标目录
void FtpClient::gotoDir(const QVariant& dirPath)
{
QString dirName = dirPath.toString();
if (dirName == u8"我的文件")
{
m_strCurrentPath = "";
getDir("");
SelectPathListModel::pSelectPathListModel->delFolder(dirName);
} else {
if (m_strCurrentPath.right(dirName.length()).compare(dirName) != 0)
{
QString dstPath = m_strCurrentPath.mid(0,m_strCurrentPath.lastIndexOf(dirName)
+ dirName.length());
m_strCurrentPath = dstPath;
getDir(dstPath);
SelectPathListModel::pSelectPathListModel->delFolder(dirName);
}
}
}
//刷新当前目录
void FtpClient::refreshDir()
{
QString currPath = m_strCurrentPath;
m_strCurrentPath.clear();
getDirList(false, false, true, currPath);
}
//新建文件夹
void FtpClient::createDir(const QVariant& dirName)
{
QString strDirs = m_strCurrentPath;
if (!m_strCurrentPath.isEmpty())
{
strDirs.append("/");
}
strDirs.append(dirName.toString());
QtConcurrent::run([=](){
std::string strBuffer = "";
struct curl_slist* headerlist = NULL;
std::string strCmd = "MKD ";
strCmd.append(strDirs.toLocal8Bit());
headerlist = curl_slist_append(headerlist, strCmd.data());
CURL* pCurl = curl_easy_init();
curl_easy_setopt(pCurl, CURLOPT_URL, BASE_URL.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_USERPWD, USER_PWD.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_QUOTE, headerlist);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnWriteCallBack);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &strBuffer);
curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 5L);
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 0);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPIDLE, 1000L);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPINTVL, 60L);
CURLcode curlCode = curl_easy_perform(pCurl);
if (curlCode != CURLE_OK)
{
qDebug() << curl_easy_strerror(curlCode);
emit showMessage(true, u8"新建文件夹失败");
}
curl_slist_free_all(headerlist);
curl_easy_cleanup(pCurl);
getDir(m_strCurrentPath);
});
}
//删除文件或目录
void FtpClient::deleteDirFiles(CURL* pCurl, const QString& name,
bool isFolder, std::string* strBuf)
{
std::string strCmd;
if(isFolder)
{
strCmd.append("RMD ");
strCmd.append(name.toLocal8Bit());
} else {
strCmd.append("DELE ");
strCmd.append(name.toLocal8Bit());
}
strBuf->clear();
struct curl_slist* headerlist = NULL;
headerlist = curl_slist_append(headerlist, strCmd.data());
curl_easy_setopt(pCurl, CURLOPT_QUOTE, headerlist);
CURLcode curlCode = curl_easy_perform(pCurl);
curl_slist_free_all(headerlist);
if (curlCode != CURLE_OK)
{
emit showMessage(true, u8"不支持删除非空文件夹");
qDebug() << "DELETE FILE : "<< curl_easy_strerror(curlCode) << curlCode;
}
}
//删除选中文件
void FtpClient::deleteSelectItem()
{
QHash<int, QJsonObject> selectItems;
AllFilesListModel::pAllFileListModel->getselectItems(selectItems);
if (selectItems.empty())
{
emit showMessage(true, u8"请选择需要删除的文件");
return;
}
QtConcurrent::run([=](){
std::string strBuffer = "";
CURL* pCurl = curl_easy_init();
curl_easy_setopt(pCurl, CURLOPT_URL, BASE_URL.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_USERPWD, USER_PWD.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 2L);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnWriteCallBack);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &strBuffer);
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 0);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPIDLE, 1000L);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPINTVL, 60L);
CURLcode curlCode = curl_easy_perform(pCurl);
if (curlCode != CURLE_OK)
{
curl_easy_cleanup(pCurl);
emit showMessage(true, u8"连接个人云盘失败");
return;
}
std::string strCmd;
for (auto iter = selectItems.begin();
iter != selectItems.end(); ++iter)
{
QJsonObject item = iter.value();
bool isFolder = item.value("isFolder").toBool();
QString fileName = item.value("fileName").toString();
QString pathName = m_strCurrentPath;
if(!m_strCurrentPath.isEmpty())
{
pathName.append("/");
}
pathName.append(fileName);
deleteDirFiles(pCurl, pathName, isFolder, &strBuffer);
}
curl_easy_cleanup(pCurl);
getDir(m_strCurrentPath);
});
}
//文件下载
void FtpClient::downloadFile()
{
if (AllFilesListModel::pAllFileListModel == nullptr ||
DownloadListModel::pDownloadListModel == nullptr)
{
return;
}
QHash<int, QJsonObject> selectItems;
AllFilesListModel::pAllFileListModel->getselectItems(selectItems);
if (selectItems.empty())
{
emit showMessage(true, u8"请选择需要下载的文件");
return;
}
for (auto iter : selectItems)
{
iter.insert("localPath", donwloadPath);
if (!iter.value("isFolder").toBool())
{
downloadFileList.push_back(iter.value("fileName").toString());
DownloadListModel::pDownloadListModel->addDownloadItem(iter);
}
}
if (!m_timerDownload.isActive() &&
!downloadFileList.empty())
{
connect(this, &FtpClient::removeDownItem,
DownloadListModel::pDownloadListModel,
&DownloadListModel::removeDownItem, Qt::UniqueConnection);
connect(this, &FtpClient::updateDownloadProgress,
DownloadListModel::pDownloadListModel,
&DownloadListModel::updateProgress, Qt::UniqueConnection);
emit showMessage(false, QString(u8"添加%1个下载任务成功").arg(downloadFileList.size()));
m_timerDownload.start();
downloadFileImp();
}
return;
}
void FtpClient::downloadFileImp()
{
lastDownloadSize.store(0);
flagCancelDownload.store(0);
QString fileName = downloadFileList.takeFirst();
QtConcurrent::run([=](){
QString strPath = QDir::fromNativeSeparators(donwloadPath);
QDir dir(strPath);
if(!dir.exists())
{
dir.mkpath(strPath);
}
QString filePath = strPath + "/" + fileName;
QFile oFile(filePath);
oFile.open(QIODevice::ReadWrite);
if(!oFile.isOpen())
{
emit removeDownItem(0, false, true);
emit signaDownloadComplete();
return;
}
CURL* pCurl = curl_easy_init();
std::string remoteUrl = BASE_URL.toStdString();
QByteArray currPath = m_strCurrentPath.toLocal8Bit();
if (!m_strCurrentPath.isEmpty())
{
remoteUrl.append(curl_easy_escape(pCurl, currPath.data(),
currPath.size()));
remoteUrl.append("/");
}
QByteArray fileNameByte = fileName.toLocal8Bit();
remoteUrl.append(curl_easy_escape(pCurl, fileNameByte.data(),
fileNameByte.size()));
curl_easy_setopt(pCurl, CURLOPT_URL, remoteUrl.data());
curl_easy_setopt(pCurl, CURLOPT_USERPWD, USER_PWD.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnDownloadCallBack);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &oFile);
curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(pCurl, CURLOPT_PROGRESSFUNCTION, OnDownloadProgress);
//当请求在 10 秒内每一秒的传输速率都不足 10 字节时,则判定为超时
curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_LIMIT, 10);
curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_TIME, 10);
curl_easy_setopt(pCurl, CURLOPT_NOPROXY, "*");
CURLcode curlCode = curl_easy_perform(pCurl);
oFile.close();
curl_easy_cleanup(pCurl);
if (curlCode == CURLE_OK)
{
emit removeDownItem(0, true, false);
} else if(curlCode == CURLE_ABORTED_BY_CALLBACK) {
emit removeDownItem(0, false, true);
} else {
qDebug() << "download error: " <<
curl_easy_strerror(curlCode);
emit removeDownItem(0, false, false);
}
emit signaDownloadComplete();
});
}
//下载刷新界面进度
void FtpClient::slotDownloadTimeout()
{
QString sizeDown = formatSize(downloadSize.load());
QString sizeTotal = formatSize(downloadTotalSize.load());
qint64 speedVal = (qint64)downloadSize.load() - (qint64)lastDownloadSize.load();
emit updateDownloadProgress(downloadSize.load(),
downloadTotalSize.load(), formatSize(speedVal) + "/s",
sizeDown + "/" + sizeTotal);
lastDownloadSize.store(downloadSize.load());
}
//下载文件回调
size_t FtpClient::OnDownloadCallBack(char* buffer, size_t size,
size_t nitems, void* outstream)
{
QFile* pFile = reinterpret_cast<QFile*>(outstream);
if (pFile == NULL)
{
return 0;
}
return pFile->write(buffer, size * nitems);
}
//下载进度
int FtpClient::OnDownloadProgress(void* clientp, double dltotal, double dlnow,
double ultotal, double ulnow)
{
Q_UNUSED(ultotal);
Q_UNUSED(ulnow);
Q_UNUSED(clientp);
downloadSize.store(dlnow);
downloadTotalSize.store(dltotal);
return flagCancelDownload.load();
}
void FtpClient::slotDownloadComplete()
{
if (downloadFileList.empty())
{
m_timerDownload.stop();
return;
}
downloadFileImp();
}
//文件上传
void FtpClient::uploadFile(const QVariantList& fileList)
{
if (UploadListModel::pUploadListModel == nullptr)
{
return;
}
QJsonArray jsonFileList;
for(auto iter : fileList)
{
QJsonObject jsonItem;
QString filePath = iter.toString().mid(8);
QFileInfo info(filePath);
if (info.isDir() || !info.exists())
{
emit showMessage(true, u8"不支持文件夹上传");
continue;
}
QString fileName = filePath.mid(filePath.lastIndexOf("/") + 1, filePath.length());
QString fileType = filePath.mid(filePath.lastIndexOf(".") + 1, filePath.length()).toUpper();
QString localPath = filePath.mid(0, filePath.lastIndexOf("/"));
auto itFind = m_hashFileTypeIcon.find(fileType);
if (itFind != m_hashFileTypeIcon.end())
{
jsonItem.insert("fileIcon" , itFind.value().typeIcon);
} else {
jsonItem.insert("fileIcon" , "../res/icon_unknow_big.png");
}
jsonItem.insert("fileSize" , info.size());
jsonItem.insert("fileSizeFormat" , formatSize(info.size()));
jsonItem.insert("fileTransfer" , formatSize(info.size()));
jsonItem.insert("fileName" , fileName);
jsonItem.insert("uploadSpeed" ,"0 B/s");
jsonItem.insert("progressVal" , 0);
jsonItem.insert("localPath" , filePath.mid(0, filePath.lastIndexOf("/")));
UploadListModel::pUploadListModel->addUploadList(jsonItem);
uploadFileList.push_back(filePath);
}
if (uploadFileList.empty())
{
return;
}
emit showMessage(false, QString(u8"添加%1个上传任务成功").arg(uploadFileList.size()));
connect(this, &FtpClient::removeItem,
UploadListModel::pUploadListModel,
&UploadListModel::removeItem, Qt::UniqueConnection);
connect(this, &FtpClient::updateProgress,
UploadListModel::pUploadListModel,
&UploadListModel::updateProgress, Qt::UniqueConnection);
if (!m_timerUpload.isActive())
{
m_timerUpload.start();
uploadFileToServer();
}
return;
}
void FtpClient::uploadFileToServer()
{
lastUploadSize.store(0);
flagCancelUpload.store(0);
QString filePath = uploadFileList.takeFirst();
QString fileName = filePath.mid(filePath.lastIndexOf("/") + 1,
filePath.length());
QtConcurrent::run([=](){
QFile oFile(filePath);
oFile.open(QIODevice::ReadOnly);
if (!oFile.isOpen())
{
emit removeItem(0, false, true);
emit signaUploadComplete();
return;
}
CURL* pCurl = curl_easy_init();
std::string remoteUrl = BASE_URL.toStdString();
if (!m_strCurrentPath.isEmpty())
{
remoteUrl.append(curl_easy_escape(pCurl,
m_strCurrentPath.toLocal8Bit().data(),
m_strCurrentPath.toLocal8Bit().size()));
remoteUrl.append("/");
}
remoteUrl.append(curl_easy_escape(pCurl,
fileName.toLocal8Bit().data(),
fileName.toLocal8Bit().size()));
curl_off_t fileSize = static_cast<curl_off_t>(oFile.size());
curl_easy_setopt(pCurl, CURLOPT_URL, remoteUrl.data());
curl_easy_setopt(pCurl, CURLOPT_USERPWD, USER_PWD.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_INFILESIZE_LARGE, fileSize);
curl_easy_setopt(pCurl, CURLOPT_FTP_CREATE_MISSING_DIRS, (long)CURLFTP_CREATE_DIR_RETRY);
curl_easy_setopt(pCurl, CURLOPT_READFUNCTION, OnUploadCallBack);
curl_easy_setopt(pCurl, CURLOPT_READDATA, &oFile);
curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(pCurl, CURLOPT_PROGRESSFUNCTION, OnUploadProgress);
curl_easy_setopt(pCurl, CURLOPT_UPLOAD, 1L);
//当请求在 10 秒内每一秒的传输速率都不足 10 字节时,则判定为超时
curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_LIMIT, 10);
curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_TIME, 10);
curl_easy_setopt(pCurl, CURLOPT_NOPROXY, "*");
CURLcode curlCode = curl_easy_perform(pCurl);
oFile.close();
curl_easy_cleanup(pCurl);
if (curlCode == CURLE_OK)
{
emit removeItem(0, true, false);
} else if (curlCode == CURLE_ABORTED_BY_CALLBACK){
emit removeItem(0, false, true);
} else {
qDebug() << "upload error: " <<
curl_easy_strerror(curlCode);
emit removeItem(0, false, false);
}
emit signaUploadComplete();
});
}
//上传进度
size_t FtpClient::OnUploadCallBack(char* buffer, size_t size,
size_t nitems, void* instream)
{
QFile* pFile = reinterpret_cast<QFile*>(instream);
if (pFile == NULL)
{
return 0;
}
return pFile->read(buffer, size * nitems);
}
//上传回调
int FtpClient::OnUploadProgress(void* clientp, double dltotal, double dlnow,
double ultotal, double ulnow)
{
Q_UNUSED(dltotal);
Q_UNUSED(dlnow);
Q_UNUSED(clientp);
uploadTotalSize.store(ultotal);
uploadSize.store(ulnow);
return flagCancelUpload.load();
}
void FtpClient::slotUploadComplete()
{
if (uploadFileList.empty())
{
m_timerUpload.stop();
getDir(m_strCurrentPath);
return;
}
uploadFileToServer();
}
//上传刷新界面进度
void FtpClient::slotUploadTimeout()
{
QString sizeDown = formatSize(uploadSize.load());
QString sizeTotal = formatSize(uploadTotalSize.load());
qint64 speedVal = (qint64)uploadSize.load() - (qint64)lastUploadSize.load();
emit updateProgress(uploadSize.load(),
uploadTotalSize.load(), formatSize(speedVal) + "/s",
sizeDown + "/" + sizeTotal);
lastUploadSize.store(uploadSize.load());
}
//打开文件所在路径
void FtpClient::openLocalPath(const QVariant& path)
{
QString strUrl = "file:///";
strUrl.append(path.toString());
QDesktopServices::openUrl(QUrl(strUrl));
}
//界面取消上传
void FtpClient::removeUploadItem(int index)
{
if (index == 0)
{
flagCancelUpload.store(1);
} else {
uploadFileList.removeAt(index - 1);
emit removeItem(index, false, true);
}
}
//界面取消下载
void FtpClient::removeDownloadItem(int index)
{
if (index == 0)
{
flagCancelDownload.store(1);
} else {
downloadFileList.removeAt(index - 1);
emit removeDownItem(index, false, true);
}
}
void FtpClient::refreshUsege()
{
QtConcurrent::run([=](){
CURL* pCurl = curl_easy_init();
curl_easy_setopt(pCurl, CURLOPT_URL, BASE_URL.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_USERPWD, USER_PWD.toStdString().data());
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnWriteCallBack);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &strBufferData);
curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 5L);
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 0);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPIDLE, 1000L);
curl_easy_setopt(pCurl, CURLOPT_TCP_KEEPINTVL, 60L);
CURLcode curlCode = curl_easy_perform(pCurl);
if(curlCode == CURLE_OK)
{
requestDirInfo(pCurl, "", "");
}
curl_easy_cleanup(pCurl);
});
}
void FtpClient::requestDirInfo(CURL* pCurl, const QString& dirName, const QString& currPath)
{
strBufferData.clear();
std::string strCmd = "CWD ";
if (!currPath.isEmpty())
{
strCmd.append(currPath.toLocal8Bit());
strCmd.append("/");
}
strCmd.append(dirName.toLocal8Bit());
qDebug() << "currPath: " << currPath;
qDebug() << "strCmd: " << QString::fromStdString(strCmd);
struct curl_slist* headerlist = NULL;
headerlist = curl_slist_append(headerlist, strCmd.data());
curl_easy_setopt(pCurl, CURLOPT_QUOTE, headerlist);
CURLcode curlCode = curl_easy_perform(pCurl);
curl_slist_free_all(headerlist);
if (curlCode == CURLE_OK)
{
parseDirList(pCurl, strBufferData, currPath);
} else {
qDebug() <<"dirlist error: "<< curl_easy_strerror(curlCode);
}
}
void FtpClient::parseDirList(CURL* pCurl, const std::string& strData, const QString& currPath)
{
QRegExp dosPattern(QLatin1String("^(\\d\\d-\\d\\d-\\d\\d\\d?\\d?\\ \\ \\d\\d:\\d\\d[AP]M)\\s+"
"(<DIR>|\\d+)\\s+(\\S.*)$"));
QString bufferStr = QString::fromLatin1(strData.data());
QStringList strList = bufferStr.split("\r\n");
for (int i = 0; i < strList.size(); ++i)
{
QUrlInfo info;
if (dosPattern.indexIn(strList.at(i).trimmed()) != 0)
{
continue;
}
if(!parseDosDir(dosPattern.capturedTexts(), &info))
{
continue;
}
if (info.isDir())
{