forked from flipperdevices/qFlipper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremotefilefetcher.cpp
More file actions
92 lines (69 loc) · 2.8 KB
/
remotefilefetcher.cpp
File metadata and controls
92 lines (69 loc) · 2.8 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
#include "remotefilefetcher.h"
#include <QNetworkAccessManager>
#include <QCryptographicHash>
#include <QNetworkReply>
#include "debug.h"
using namespace Flipper;
RemoteFileFetcher::RemoteFileFetcher(QObject *parent):
QObject(parent),
m_manager(new QNetworkAccessManager(this))
{}
RemoteFileFetcher::RemoteFileFetcher(const QString &remoteUrl, QIODevice *outputFile, QObject *parent):
RemoteFileFetcher(parent)
{
fetch(remoteUrl, outputFile);
}
RemoteFileFetcher::RemoteFileFetcher(const Flipper::Updates::FileInfo &fileInfo, QIODevice *outputFile, QObject *parent):
RemoteFileFetcher(parent)
{
fetch(fileInfo, outputFile);
}
bool RemoteFileFetcher::fetch(const QString &remoteUrl, QIODevice *outputFile)
{
if(!outputFile->open(QIODevice::WriteOnly)) {
setError(BackendError::DiskError, QStringLiteral("Failed to open file for writing: %1.").arg(outputFile->errorString()));
return false;
}
auto *reply = m_manager->get(QNetworkRequest(remoteUrl));
if(reply->error() != QNetworkReply::NoError) {
setError(BackendError::InternetError, QStringLiteral("Network error: %1").arg(reply->errorString()));
reply->deleteLater();
return false;
}
const auto onReplyReadyRead = [=]() {
outputFile->write(reply->readAll());
};
connect(reply, &QNetworkReply::finished, this, [=]() {
// In case there was any leftover data
onReplyReadyRead();
outputFile->close();
reply->deleteLater();
if(reply->error() != QNetworkReply::NoError) {
setError(BackendError::InternetError, QStringLiteral("Network error: %1").arg(reply->errorString()));
} else if(!m_expectedChecksum.isEmpty()) {
if(!outputFile->open(QIODevice::ReadOnly)) {
setError(BackendError::DiskError, QStringLiteral("Failed to open file for reading: %1.").arg(outputFile->errorString()));
return;
}
QCryptographicHash hash(QCryptographicHash::Sha256);
hash.addData(outputFile);
if(hash.result().toHex() != m_expectedChecksum) {
setError(BackendError::UnknownError, QStringLiteral("File integrity check failed"));
}
outputFile->close();
}
emit finished();
});
connect(reply, &QNetworkReply::readyRead, this, onReplyReadyRead);
connect(reply, &QNetworkReply::downloadProgress, this, &RemoteFileFetcher::onDownloadProgress);
return true;
}
bool RemoteFileFetcher::fetch(const Flipper::Updates::FileInfo &fileInfo, QIODevice *outputFile)
{
m_expectedChecksum = fileInfo.sha256();
return fetch(fileInfo.url(), outputFile);
}
void RemoteFileFetcher::onDownloadProgress(qint64 received, qint64 total)
{
emit progressChanged(((double)received / (double)total) * 100.0);
}