-
Notifications
You must be signed in to change notification settings - Fork 4
/
upload_manager.cpp
209 lines (170 loc) · 7 KB
/
upload_manager.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
#include "upload_manager.h"
#include "upload_list.h"
#include "window.h"
UploadManager::UploadManager(QObject * parent) : QObject(parent), network_manager(), json_document()
{
suffixes << "B" << "KB" << "MB" << "GB" << "TB" << "PB";
connect(UploadList::instance(), SIGNAL(changedList()), this, SLOT(updateUploadList()));
}
void UploadManager::toggleUploadState() {
if (UploadList::instance()->current_uploading == NULL)
startUploadSequence();
else {
network_reply->abort();
network_reply->deleteLater();
UploadList::instance()->fail_all_pending(tr("Aborted"));
failCurrentUpload(tr("Aborted"), false);
}
}
void UploadManager::updateUploadList() {
if (UploadList::instance()->current_uploading == NULL)
startUploadSequence();
}
void UploadManager::startUploadSequence() {
if (!assignFileForUpload())
return
UploadList::instance()->change_current_state(tr("uploading"), tr("Preparing..."));
current_progress = 0;
emit updatedCurrentUploadState();
QNetworkRequest request;
request.setUrl(QUrl("https://rghost.net/api/upload_url"));
setDefaultHeaders(&request);
network_reply = network_manager.get(request);
connect(network_reply, SIGNAL(finished()), this, SLOT(hostRequestFinished()));
}
bool UploadManager::assignFileForUpload() {
if (UploadList::instance()->new_current_uploading() == NULL)
return false;
if (!QFile::exists(UploadList::instance()->current_uploading->path())) {
failCurrentUpload(tr("Error: file does not exist"));
return false;
}
upload_file = new QFile(UploadList::instance()->current_uploading->path());
if ( !upload_file->open(QIODevice::ReadOnly) ) {
failCurrentUpload(tr("Unable to open the file %1: %2.").arg(UploadList::instance()->current_uploading->path()).arg(upload_file->errorString()));
return false;
}
return true;
}
void UploadManager::setDefaultHeaders(QNetworkRequest * request) {
request->setRawHeader("Accept-Language","en");
request->setRawHeader("Accept","application/json");
request->setRawHeader("User-Agent", USER_AGENT);
request->setRawHeader("Cookie", session.toUtf8());
if ( Window::isApiKeyEntered())
request->setRawHeader("X-API-Key", Window::instance().settings.value("api_key").toString().toUtf8());
}
void UploadManager::hostRequestFinished() {
if (!parseResponse())
return;
qint64 upload_limit = json_document["upload_limit"].toInt();
if ( upload_file->size() > upload_limit*1024*1024) {
upload_file->close();
upload_file = NULL;
failCurrentUpload(tr("Upload size limit (%1 MB)").arg(upload_limit));
return;
}
sendFile();
}
bool UploadManager::parseResponse() {
session = network_reply->rawHeader("Set-Cookie");
qDebug() << "received session:\n" << session;
QByteArray reply = network_reply->readAll();
json_document = QJsonDocument::fromJson(reply);
qDebug() << "received response:\n" << reply << "\n" << json_document;
QString message = json_document["error"].toString();
if (message == "" && network_reply->error() != QNetworkReply::NoError)
message = network_reply->errorString();
network_reply->deleteLater();
if (message != "") {
failCurrentUpload(tr("Upload failed: %1.").arg(message));
return false;
} else
return true;
}
void UploadManager::sendFile() {
QNetworkRequest request;
UploadList::instance()->change_current_state(tr("uploading"), tr("Uploading..."));
request.setUrl(QUrl(json_document["upload_url"].toString()));
setDefaultHeaders(&request);
request.setRawHeader("Content-type", QString("multipart/form-data; boundary=" + Payload::boundary).toUtf8());
request.setAttribute(QNetworkRequest::DoNotBufferUploadDataAttribute, true);
payload = new Payload(upload_file, json_document);
start_date = last_date = QDateTime::currentDateTime();
network_reply = network_manager.post(request, payload);
connect(network_reply, SIGNAL(finished()), this, SLOT(sendFileRequestFinished()));
connect(network_reply, SIGNAL(uploadProgress(qint64, qint64) ), this, SLOT(updateUploadProgress(qint64,qint64)));
}
void UploadManager::sendFileRequestFinished() {
if (!parseResponse())
return;
QString message = json_document["url"].toString();
if (message == "")
message = "Something went wrong, please contact [email protected]";
UploadList::instance()->change_current_state(tr("uploaded"), message);
current_progress = 1000;
emit updatedCurrentUploadState();
payload->deleteLater();
network_reply->deleteLater();
upload_file = NULL;
payload = NULL;
UploadList::instance()->stop_current_uploading();
UploadManager::startUploadSequence();
}
void UploadManager::failCurrentUpload(QString description, bool startNext) {
UploadList::instance()->change_current_state(tr("error"), description);
UploadList::instance()->stop_current_uploading();
if (startNext)
UploadManager::startUploadSequence();
}
void UploadManager::updateUploadProgress(qint64 bytesSent, qint64 totalBytes) {
if (bytesSent <= 0)
return;
qDebug() << "progress event: " << bytesSent << "/" << totalBytes;
recordSpeed(bytesSent);
current_progress = round(1000 * (float(bytesSent) / float(totalBytes)));
qint64 interval = start_date.secsTo(last_date);
if (interval == 0) {
emit updatedCurrentUploadState();
return;
}
float average_speed = float(bytesSent) / float(interval);
qint64 eta_seconds = round(float(totalBytes - bytesSent) / average_speed);
QString size_str, speed_str, time_str;
qint64 size = bytesSent, max_size = totalBytes;
int i = 0;
for(; i < suffixes.size(); ++i) {
if(max_size < 2048)
break;
size /= 1024;
max_size /= 1024;
}
int hours = round(eta_seconds / 3600);
int temp = round(eta_seconds % 3600);
int min = round(temp / 60);
int sec = round(temp % 60);
size_str = tr("%1 / %2 %3 ").arg(QString::number(size, 'g', 2)).arg(QString::number(max_size, 'g', 2)).arg(suffixes[i]);
speed_str = tr("%1 KBps ").arg(round(average_speed/1024));
if (hours > 0)
time_str = tr("%1h %2m").arg(hours).arg(min);
else if (min > 5)
time_str = tr("%1 m").arg(min);
else
time_str = tr("%1m %2s").arg(min).arg(sec);
current_progress_descr = size_str + speed_str + time_str;
emit updatedCurrentUploadState();
qDebug() << "recording progress:\n"<< current_progress << "\n" << current_progress_descr;
}
void UploadManager::recordSpeed(qint64 new_bytes_sent) {
QDateTime current_time = QDateTime::currentDateTime();
if(last_date != start_date) {
int interval = last_date.secsTo(current_time);
if(interval > 0) {
float avg = round(float(new_bytes_sent - bytes_sent)/interval);
if(maximum_speed < avg)
maximum_speed = avg;
}
}
last_date = current_time;
bytes_sent = new_bytes_sent;
}