-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpdownload.cpp
192 lines (153 loc) · 3.87 KB
/
httpdownload.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
#include <memory>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QString>
#include <QTime>
#include <QUrl>
#include <QVariant>
#include "httpdownload.hpp"
namespace vfg {
namespace net {
HttpDownload::HttpDownload(const QNetworkRequest& request, const QDir& cachePath, QObject *parent) :
QObject(parent),
request(request),
outFile(cachePath.absoluteFilePath(request.url().fileName()))
{
if(!cachePath.exists()) {
cachePath.mkpath(cachePath.path());
}
else if(outFile.exists()) {
const QFileInfo info(outFile);
for(int i = 1; ; ++i) {
const QString newFileName = QString("%1-%2.%3").arg(info.baseName())
.arg(QString::number(i)).arg(info.completeSuffix());
const QString newPath = cachePath.absoluteFilePath(newFileName);
if(!QFile::exists(newPath)) {
outFile.setFileName(newPath);
break;
}
}
}
}
HttpDownload::~HttpDownload()
{
outFile.remove();
}
void HttpDownload::start(QNetworkAccessManager* netMan)
{
received = total = dlDuration = downloaded = speed = 0;
outFile.open(QIODevice::WriteOnly);
reply.reset(netMan->get(request));
connect(reply.get(), &QNetworkReply::downloadProgress,
this, &HttpDownload::updateProgress);
connect(reply.get(), &QNetworkReply::finished,
this, &HttpDownload::downloadFinished);
timer.start();
speedTimer.start();
status = Status::Running;
}
double HttpDownload::percentCompleted() const
{
return static_cast<double>(received) / total * 100;
}
bool HttpDownload::sizeKnown() const
{
return total >= 0;
}
int HttpDownload::bytesDownloaded() const
{
return received;
}
int HttpDownload::bytesTotal() const
{
return total;
}
bool HttpDownload::isFinished() const
{
return reply->isFinished();
}
int HttpDownload::duration() const
{
return dlDuration;
}
QString HttpDownload::fileName() const
{
const QFileInfo info(outFile);
return info.fileName();
}
QString HttpDownload::path() const
{
return outFile.fileName();
}
void HttpDownload::abort()
{
if(reply->isFinished()) {
return;
}
reply->abort();
status = Status::Aborted;
outFile.close();
emit updated();
}
HttpDownload::Status HttpDownload::getStatus() const
{
return status;
}
QUrl HttpDownload::url() const
{
return request.url();
}
double HttpDownload::downloadSpeed() const
{
return speed;
}
bool HttpDownload::hasError() const
{
return reply->error() != QNetworkReply::NoError;
}
QString HttpDownload::errorString() const
{
return reply->errorString();
}
int HttpDownload::statusCode() const
{
return reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}
QString HttpDownload::reason() const
{
return reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
}
void HttpDownload::retry()
{
start(reply->manager());
}
void HttpDownload::downloadFinished()
{
dlDuration = timer.elapsed();
// Prevent aborted requests from being marked as finished
if(status == Status::Running) {
status = Status::Finished;
}
}
void HttpDownload::updateProgress(const qint64 bytesReceived, const qint64 bytesTotal)
{
received = bytesReceived;
total = bytesTotal;
dlDuration = timer.elapsed();
// Calculate download speed in the last second
if(speedTimer.elapsed() > 1000) {
downloaded = received - downloaded;
speed = static_cast<double>(downloaded) / (speedTimer.elapsed() / 1000.0);
downloaded = received;
speedTimer.restart();
}
// reply->readAll() fails to return complete data
outFile.write(reply->read(reply->bytesAvailable()));
emit updated();
}
} // namespace net
} // namespace vfg