2019-12-08 10:45:55 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "uploadDiveShare.h"
|
|
|
|
#include <QDebug>
|
|
|
|
#include "core/membuffer.h"
|
|
|
|
#include "core/settings/qPrefCloudStorage.h"
|
|
|
|
#include "core/qthelper.h"
|
|
|
|
#include "core/cloudstorage.h"
|
|
|
|
#include "core/save-html.h"
|
|
|
|
#include "core/errorhelper.h"
|
|
|
|
|
|
|
|
|
|
|
|
uploadDiveShare *uploadDiveShare::instance()
|
|
|
|
{
|
|
|
|
static uploadDiveShare *self = new uploadDiveShare;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uploadDiveShare::uploadDiveShare():
|
|
|
|
reply(NULL)
|
|
|
|
{
|
|
|
|
timeout.setSingleShot(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void uploadDiveShare::doUpload(bool selected, const QString &uid, bool noPublic)
|
|
|
|
{
|
2019-12-10 13:33:54 +00:00
|
|
|
//generate json
|
|
|
|
struct membuffer buf = {};
|
|
|
|
export_list(&buf, NULL, selected, false);
|
|
|
|
QByteArray json_data(buf.buffer, buf.len);
|
|
|
|
free_buffer(&buf);
|
|
|
|
|
|
|
|
//Request to server
|
|
|
|
QNetworkRequest request;
|
|
|
|
if (noPublic)
|
|
|
|
request.setUrl(QUrl("http://dive-share.appspot.com/upload?private=true"));
|
|
|
|
else
|
|
|
|
request.setUrl(QUrl("http://dive-share.appspot.com/upload"));
|
|
|
|
request.setRawHeader("User-Agent", getUserAgent().toUtf8());
|
|
|
|
if (uid.length() != 0)
|
|
|
|
request.setRawHeader("X-UID", uid.toUtf8());
|
|
|
|
|
|
|
|
// Execute async.
|
|
|
|
reply = manager()->put(request, json_data);
|
|
|
|
|
|
|
|
// connect signals from upload process
|
2019-12-12 11:39:29 +00:00
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(uploadFinishedSlot()));
|
2019-12-10 13:33:54 +00:00
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
|
2019-12-12 11:39:29 +00:00
|
|
|
SLOT(uploadErrorSlot(QNetworkReply::NetworkError)));
|
2019-12-10 13:33:54 +00:00
|
|
|
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this,
|
2019-12-12 11:39:29 +00:00
|
|
|
SLOT(updateProgressSlot(qint64, qint64)));
|
|
|
|
connect(&timeout, SIGNAL(timeout()), this, SLOT(uploadTimeoutSlot()));
|
2019-12-10 13:33:54 +00:00
|
|
|
|
|
|
|
timeout.start(30000); // 30s
|
2019-12-08 10:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-12 11:39:29 +00:00
|
|
|
void uploadDiveShare::updateProgressSlot(qint64 current, qint64 total)
|
2019-12-08 10:45:55 +00:00
|
|
|
{
|
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
if (total <= 0 || current <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Calculate percentage
|
|
|
|
// And signal whoever wants to know
|
|
|
|
qreal percentage = (float)current / (float)total;
|
|
|
|
emit uploadProgress(percentage, 1.0);
|
|
|
|
|
|
|
|
// reset the timer: 30 seconds after we last got any data
|
|
|
|
timeout.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-12 11:39:29 +00:00
|
|
|
void uploadDiveShare::uploadFinishedSlot()
|
2019-12-08 10:45:55 +00:00
|
|
|
{
|
2019-12-12 11:57:56 +00:00
|
|
|
QByteArray html = reply->readAll();
|
2019-12-08 10:45:55 +00:00
|
|
|
reply->deleteLater();
|
|
|
|
timeout.stop();
|
|
|
|
if (reply->error() != 0) {
|
2019-12-12 11:57:56 +00:00
|
|
|
emit uploadFinish(false, reply->errorString(), html);
|
2019-12-08 10:45:55 +00:00
|
|
|
} else {
|
2019-12-12 11:57:56 +00:00
|
|
|
emit uploadFinish(true, tr("Upload successful"), html);
|
2019-12-08 10:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-12 11:39:29 +00:00
|
|
|
void uploadDiveShare::uploadTimeoutSlot()
|
2019-12-08 10:45:55 +00:00
|
|
|
{
|
|
|
|
timeout.stop();
|
|
|
|
if (reply) {
|
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
|
|
|
}
|
2019-12-10 13:33:54 +00:00
|
|
|
QString err(tr("dive-share.com not responding"));
|
2019-12-08 10:45:55 +00:00
|
|
|
report_error(err.toUtf8());
|
2019-12-10 14:45:51 +00:00
|
|
|
emit uploadFinish(false, err, QByteArray());
|
2019-12-08 10:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-12 11:39:29 +00:00
|
|
|
void uploadDiveShare::uploadErrorSlot(QNetworkReply::NetworkError error)
|
2019-12-08 10:45:55 +00:00
|
|
|
{
|
|
|
|
timeout.stop();
|
|
|
|
if (reply) {
|
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
|
|
|
}
|
|
|
|
QString err(tr("network error %1").arg(error));
|
|
|
|
report_error(err.toUtf8());
|
2019-12-10 14:45:51 +00:00
|
|
|
emit uploadFinish(false, err, QByteArray());
|
2019-12-08 10:45:55 +00:00
|
|
|
}
|