Skip to content

Commit ef00836

Browse files
author
wangzhiyuan1
committed
fix download file bug
1 parent cf9bea6 commit ef00836

File tree

6 files changed

+32
-15
lines changed

6 files changed

+32
-15
lines changed

clipboard/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5)
22

33
project(clipboard VERSION 0.1 LANGUAGES CXX)
44

5-
set(CMAKE_OSX_ARCHITECTURES "x86_64")
5+
#set(CMAKE_OSX_ARCHITECTURES "x86_64")
66

77
set(CMAKE_INCLUDE_CURRENT_DIR ON)
88

clipboard/CMakeLists.txt.user

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 7.0.2, 2022-06-12T17:29:31. -->
3+
<!-- Written by QtCreator 7.0.2, 2022-06-12T21:20:32. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -92,7 +92,7 @@
9292
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">x86-darwin-generic-mach_o-64bit</value>
9393
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">x86-darwin-generic-mach_o-64bit</value>
9494
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{aa14ffd8-ab35-4922-a504-dc79e8941360}</value>
95-
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
95+
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
9696
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
9797
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
9898
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
@@ -306,7 +306,7 @@
306306
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
307307
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
308308
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
309-
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/Users/zhiyuan/Documents/project/clipboard_desktop_cpp/build-clipboard-x86_darwin_generic_mach_o_64bit-Release/clipboard.app/Contents/MacOS</value>
309+
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/Users/zhiyuan/Documents/project/clipboard_desktop_cpp/build-clipboard-x86_darwin_generic_mach_o_64bit-Debug/clipboard.app/Contents/MacOS</value>
310310
</valuemap>
311311
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
312312
</valuemap>

clipboard/downfilehelper.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ DownFileHelper::DownFileHelper(QApplication *app, QString url, QString fileName)
1111
this->fileName = fileName;
1212
this->url = url;
1313
this->downloadMgr = new QNetworkAccessManager(app);
14+
15+
const QString downloadsFolder = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
16+
auto downloadFileName = QString(downloadsFolder + QDir::separator() + this->fileName);
17+
this->currentFile = new QFile(downloadFileName);
18+
if(!currentFile->open(QIODevice::ReadWrite)){
19+
return;
20+
}
1421
this->downloadFile();
1522
}
1623

1724
DownFileHelper::~DownFileHelper() {
18-
delete &fileName;
19-
delete &url;
2025
delete downloadMgr;
26+
delete downloadReply;
27+
delete currentFile;
2128
}
2229

2330
void DownFileHelper::downloadFile() {
@@ -26,6 +33,7 @@ void DownFileHelper::downloadFile() {
2633
QNetworkReply *reply = downloadMgr->get(request);
2734
downloadReply = reply;
2835
QObject::connect(reply, SIGNAL(readyRead()), this, SLOT(readingReadyBytesToFile()));
36+
QObject::connect(downloadMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinish(QNetworkReply*)));
2937
}
3038

3139

@@ -34,15 +42,15 @@ void DownFileHelper::readingReadyBytesToFile() {
3442
if (!pReply) {
3543
return;
3644
}
45+
currentFile->skip(currentFile->size());
46+
currentFile->write(pReply->readAll());
47+
}
3748

38-
const QString downloadsFolder = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
39-
auto fileName = QString(downloadsFolder + QDir::separator() + this->fileName);
40-
auto avatorFile = QFile(fileName);
41-
if(!avatorFile.open(QIODevice::Append)){
42-
return;
49+
void DownFileHelper::downloadFinish(QNetworkReply * reply) {
50+
if (currentFile) {
51+
currentFile->close();
4352
}
44-
avatorFile.write(pReply->readAll());
45-
avatorFile.close();
53+
this->disconnect();
4654
}
4755

4856

clipboard/downfilehelper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <QString>
55
#include <QNetworkAccessManager>
66
#include <QApplication>
7+
#include <QtCore>
78

89
class DownFileHelper : public QObject
910
{
@@ -14,10 +15,13 @@ class DownFileHelper : public QObject
1415
~DownFileHelper();
1516
private slots:
1617
void readingReadyBytesToFile();
18+
void downloadFinish(QNetworkReply *reply);
1719
private:
1820
QString url, fileName;
1921
QNetworkAccessManager *downloadMgr;
2022
QNetworkReply *downloadReply;
23+
QFile *currentFile;
24+
2125
void downloadFile();
2226
};
2327

clipboard/mainwindow.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ void MainWindow::loginWeb()
9191
if (nullptr == serverUrlAndHost)
9292
return;
9393
serverPath = new QString(serverUrlAndHost);
94-
auto url = QUrl("http://" + *serverPath);
94+
QUrl url;
95+
if (this->token) {
96+
url = QUrl("http://" + *serverPath + "?token=" + *this->token);
97+
} else {
98+
url = QUrl("http://" + *serverPath);
99+
}
100+
95101
QNetworkRequest req;
96102
req.setUrl(url);
97103
req.setHeader(QNetworkRequest::UserAgentHeader, "Clipb0ard");

clipboard/mainwindow.ui

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
</rect>
6464
</property>
6565
</widget>
66-
<widget class="QStatusBar" name="statusbar"/>
6766
</widget>
6867
<resources/>
6968
<connections/>

0 commit comments

Comments
 (0)