2019-09-17 15:16:09 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
|
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
#include <QtCore/QDir>
|
|
|
|
|
2019-09-17 15:16:09 +00:00
|
|
|
#include "networkaccess.h"
|
|
|
|
|
|
|
|
Core::NetworkAccess::NetworkAccess(QObject* parent):
|
|
|
|
QObject(parent),
|
|
|
|
running(false),
|
2019-09-18 13:27:47 +00:00
|
|
|
manager(0),
|
2021-04-18 12:49:20 +00:00
|
|
|
storage("fileURLStorage"),
|
2019-09-23 12:09:15 +00:00
|
|
|
downloads(),
|
|
|
|
uploads()
|
2019-09-17 15:16:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::NetworkAccess::~NetworkAccess()
|
|
|
|
{
|
2019-09-18 13:27:47 +00:00
|
|
|
stop();
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
void Core::NetworkAccess::downladFile(const QString& url)
|
2019-09-17 15:16:09 +00:00
|
|
|
{
|
2019-09-23 12:09:15 +00:00
|
|
|
std::map<QString, Transfer*>::iterator itr = downloads.find(url);
|
2019-09-17 15:16:09 +00:00
|
|
|
if (itr != downloads.end()) {
|
2021-04-18 12:49:20 +00:00
|
|
|
qDebug() << "NetworkAccess received a request to download a file" << url << ", but the file is currently downloading, skipping";
|
2019-09-17 15:16:09 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2021-04-18 12:49:20 +00:00
|
|
|
std::pair<QString, std::list<Shared::MessageInfo>> p = storage.getPath(url);
|
|
|
|
if (p.first.size() > 0) {
|
|
|
|
QFileInfo info(p.first);
|
|
|
|
if (info.exists() && info.isFile()) {
|
|
|
|
emit downloadFileComplete(p.second, p.first);
|
|
|
|
} else {
|
|
|
|
startDownload(p.second, url);
|
|
|
|
}
|
2019-09-17 15:16:09 +00:00
|
|
|
} else {
|
2021-04-18 12:49:20 +00:00
|
|
|
startDownload(p.second, url);
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
2019-11-17 10:24:12 +00:00
|
|
|
} catch (const Archive::NotFound& e) {
|
2021-04-18 12:49:20 +00:00
|
|
|
qDebug() << "NetworkAccess received a request to download a file" << url << ", but there is now record of which message uses that file, downloading anyway";
|
|
|
|
storage.addFile(url);
|
|
|
|
startDownload(std::list<Shared::MessageInfo>(), url);
|
2019-11-17 10:24:12 +00:00
|
|
|
} catch (const Archive::Unknown& e) {
|
2019-09-17 15:16:09 +00:00
|
|
|
qDebug() << "Error requesting file path:" << e.what();
|
2021-04-18 12:49:20 +00:00
|
|
|
emit loadFileError(std::list<Shared::MessageInfo>(), QString("Database error: ") + e.what(), false);
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::NetworkAccess::start()
|
|
|
|
{
|
|
|
|
if (!running) {
|
2019-09-18 13:27:47 +00:00
|
|
|
manager = new QNetworkAccessManager();
|
2021-04-18 12:49:20 +00:00
|
|
|
storage.open();
|
2019-09-17 15:16:09 +00:00
|
|
|
running = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::NetworkAccess::stop()
|
|
|
|
{
|
|
|
|
if (running) {
|
2021-04-18 12:49:20 +00:00
|
|
|
storage.close();
|
2019-09-18 13:27:47 +00:00
|
|
|
manager->deleteLater();
|
|
|
|
manager = 0;
|
2019-09-17 15:16:09 +00:00
|
|
|
running = false;
|
2019-09-18 13:27:47 +00:00
|
|
|
|
2019-09-23 12:09:15 +00:00
|
|
|
for (std::map<QString, Transfer*>::const_iterator itr = downloads.begin(), end = downloads.end(); itr != end; ++itr) {
|
2019-09-18 13:27:47 +00:00
|
|
|
itr->second->success = false;
|
|
|
|
itr->second->reply->abort(); //assuming it's gonna call onRequestFinished slot
|
|
|
|
}
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::NetworkAccess::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
|
|
|
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
|
|
|
QString url = rpl->url().toString();
|
2019-09-23 12:09:15 +00:00
|
|
|
std::map<QString, Transfer*>::const_iterator itr = downloads.find(url);
|
2019-09-17 15:16:09 +00:00
|
|
|
if (itr == downloads.end()) {
|
2019-11-09 14:04:27 +00:00
|
|
|
qDebug() << "an error downloading" << url << ": the request had some progress but seems like no one is waiting for it, skipping";
|
2019-09-17 15:16:09 +00:00
|
|
|
} else {
|
2019-09-23 12:09:15 +00:00
|
|
|
Transfer* dwn = itr->second;
|
2019-09-17 22:05:32 +00:00
|
|
|
qreal received = bytesReceived;
|
|
|
|
qreal total = bytesTotal;
|
|
|
|
qreal progress = received/total;
|
|
|
|
dwn->progress = progress;
|
2021-04-18 12:49:20 +00:00
|
|
|
emit loadFileProgress(dwn->messages, progress, false);
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 12:09:15 +00:00
|
|
|
void Core::NetworkAccess::onDownloadError(QNetworkReply::NetworkError code)
|
2019-09-17 15:16:09 +00:00
|
|
|
{
|
|
|
|
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
|
|
|
QString url = rpl->url().toString();
|
2019-09-23 12:09:15 +00:00
|
|
|
std::map<QString, Transfer*>::const_iterator itr = downloads.find(url);
|
2019-09-17 15:16:09 +00:00
|
|
|
if (itr == downloads.end()) {
|
2019-11-09 14:04:27 +00:00
|
|
|
qDebug() << "an error downloading" << url << ": the request is reporting an error but seems like no one is waiting for it, skipping";
|
2019-09-17 15:16:09 +00:00
|
|
|
} else {
|
2019-09-23 12:09:15 +00:00
|
|
|
QString errorText = getErrorText(code);
|
2019-09-18 13:27:47 +00:00
|
|
|
if (errorText.size() > 0) {
|
|
|
|
itr->second->success = false;
|
2019-09-23 12:09:15 +00:00
|
|
|
Transfer* dwn = itr->second;
|
2021-04-18 12:49:20 +00:00
|
|
|
emit loadFileError(dwn->messages, errorText, false);
|
2019-09-18 13:27:47 +00:00
|
|
|
}
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 12:09:15 +00:00
|
|
|
QString Core::NetworkAccess::getErrorText(QNetworkReply::NetworkError code)
|
|
|
|
{
|
|
|
|
QString errorText("");
|
|
|
|
switch (code) {
|
|
|
|
case QNetworkReply::NoError:
|
|
|
|
//this never is supposed to happen
|
|
|
|
break;
|
|
|
|
|
|
|
|
// network layer errors [relating to the destination server] (1-99):
|
|
|
|
case QNetworkReply::ConnectionRefusedError:
|
|
|
|
errorText = "Connection refused";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::RemoteHostClosedError:
|
|
|
|
errorText = "Remote server closed the connection";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::HostNotFoundError:
|
|
|
|
errorText = "Remote host is not found";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::TimeoutError:
|
|
|
|
errorText = "Connection was closed because it timed out";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::OperationCanceledError:
|
|
|
|
//this means I closed it myself by abort() or close(), don't think I need to notify here
|
|
|
|
break;
|
|
|
|
case QNetworkReply::SslHandshakeFailedError:
|
|
|
|
errorText = "Security error"; //TODO need to handle sslErrors signal to get a better description here
|
|
|
|
break;
|
|
|
|
case QNetworkReply::TemporaryNetworkFailureError:
|
|
|
|
//this means the connection is lost by opened route, but it's going to be resumed, not sure I need to notify
|
|
|
|
break;
|
|
|
|
case QNetworkReply::NetworkSessionFailedError:
|
|
|
|
errorText = "Outgoing connection problem";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::BackgroundRequestNotAllowedError:
|
|
|
|
errorText = "Background request is not allowed";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::TooManyRedirectsError:
|
|
|
|
errorText = "The request was redirected too many times";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::InsecureRedirectError:
|
|
|
|
errorText = "The request was redirected to insecure connection";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::UnknownNetworkError:
|
|
|
|
errorText = "Unknown network error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
// proxy errors (101-199):
|
|
|
|
case QNetworkReply::ProxyConnectionRefusedError:
|
|
|
|
errorText = "The connection to the proxy server was refused";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ProxyConnectionClosedError:
|
|
|
|
errorText = "Proxy server closed the connection";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ProxyNotFoundError:
|
|
|
|
errorText = "Proxy host was not found";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ProxyTimeoutError:
|
|
|
|
errorText = "Connection to the proxy server was closed because it timed out";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ProxyAuthenticationRequiredError:
|
|
|
|
errorText = "Couldn't connect to proxy server, authentication is required";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::UnknownProxyError:
|
|
|
|
errorText = "Unknown proxy error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
// content errors (201-299):
|
|
|
|
case QNetworkReply::ContentAccessDenied:
|
|
|
|
errorText = "The access to file is denied";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ContentOperationNotPermittedError:
|
|
|
|
errorText = "The operation over requesting file is not permitted";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ContentNotFoundError:
|
|
|
|
errorText = "The file was not found";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::AuthenticationRequiredError:
|
|
|
|
errorText = "Couldn't access the file, authentication is required";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ContentReSendError:
|
|
|
|
errorText = "Sending error, one more attempt will probably solve this problem";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ContentConflictError:
|
|
|
|
errorText = "The request could not be completed due to a conflict with the current state of the resource";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ContentGoneError:
|
|
|
|
errorText = "The requested resource is no longer available at the server";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::UnknownContentError:
|
|
|
|
errorText = "Unknown content error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
// protocol errors
|
|
|
|
case QNetworkReply::ProtocolUnknownError:
|
|
|
|
errorText = "Unknown protocol error";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ProtocolInvalidOperationError:
|
|
|
|
errorText = "Requested operation is not permitted in this protocol";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ProtocolFailure:
|
|
|
|
errorText = "Low level protocol error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Server side errors (401-499)
|
|
|
|
case QNetworkReply::InternalServerError:
|
|
|
|
errorText = "Internal server error";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::OperationNotImplementedError:
|
|
|
|
errorText = "Server doesn't support requested operation";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::ServiceUnavailableError:
|
|
|
|
errorText = "The server is not available for this operation right now";
|
|
|
|
break;
|
|
|
|
case QNetworkReply::UnknownServerError:
|
|
|
|
errorText = "Unknown server error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return errorText;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Core::NetworkAccess::onDownloadFinished()
|
2019-09-17 15:16:09 +00:00
|
|
|
{
|
|
|
|
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
|
|
|
QString url = rpl->url().toString();
|
2019-09-23 12:09:15 +00:00
|
|
|
std::map<QString, Transfer*>::const_iterator itr = downloads.find(url);
|
2019-09-17 15:16:09 +00:00
|
|
|
if (itr == downloads.end()) {
|
2021-04-18 12:49:20 +00:00
|
|
|
qDebug() << "an error downloading" << url << ": the request is done but there is no record of it being downloaded, ignoring";
|
2019-09-17 15:16:09 +00:00
|
|
|
} else {
|
2019-09-23 12:09:15 +00:00
|
|
|
Transfer* dwn = itr->second;
|
2019-09-17 15:16:09 +00:00
|
|
|
if (dwn->success) {
|
|
|
|
qDebug() << "download success for" << url;
|
|
|
|
QStringList hops = url.split("/");
|
|
|
|
QString fileName = hops.back();
|
2021-04-18 12:49:20 +00:00
|
|
|
QString jid;
|
|
|
|
if (dwn->messages.size() > 0) {
|
|
|
|
jid = dwn->messages.front().jid;
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
2021-04-18 12:49:20 +00:00
|
|
|
QString path = prepareDirectory(jid);
|
|
|
|
if (path.size() > 0) {
|
|
|
|
path = checkFileName(fileName, path);
|
|
|
|
|
|
|
|
QFile file(path);
|
|
|
|
if (file.open(QIODevice::WriteOnly)) {
|
|
|
|
file.write(dwn->reply->readAll());
|
|
|
|
file.close();
|
|
|
|
storage.setPath(url, path);
|
|
|
|
qDebug() << "file" << path << "was successfully downloaded";
|
|
|
|
} else {
|
|
|
|
qDebug() << "couldn't save file" << path;
|
|
|
|
path = QString();
|
|
|
|
}
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
if (path.size() > 0) {
|
|
|
|
emit downloadFileComplete(dwn->messages, path);
|
2019-09-17 15:16:09 +00:00
|
|
|
} else {
|
2021-04-18 12:49:20 +00:00
|
|
|
//TODO do I need to handle the failure here or it's already being handled in error?
|
|
|
|
//emit loadFileError(dwn->messages, path, false);
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dwn->reply->deleteLater();
|
|
|
|
delete dwn;
|
|
|
|
downloads.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
void Core::NetworkAccess::startDownload(const std::list<Shared::MessageInfo>& msgs, const QString& url)
|
2019-09-17 15:16:09 +00:00
|
|
|
{
|
2021-04-18 12:49:20 +00:00
|
|
|
Transfer* dwn = new Transfer({msgs, 0, 0, true, "", url, 0});
|
2019-09-17 15:16:09 +00:00
|
|
|
QNetworkRequest req(url);
|
2019-09-18 13:27:47 +00:00
|
|
|
dwn->reply = manager->get(req);
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(dwn->reply, &QNetworkReply::downloadProgress, this, &NetworkAccess::onDownloadProgress);
|
2020-08-04 14:52:16 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
2020-07-29 20:26:56 +00:00
|
|
|
connect(dwn->reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::errorOccurred), this, &NetworkAccess::onDownloadError);
|
2020-08-04 14:52:16 +00:00
|
|
|
#else
|
|
|
|
connect(dwn->reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::error), this, &NetworkAccess::onDownloadError);
|
|
|
|
#endif
|
2019-11-09 09:54:30 +00:00
|
|
|
connect(dwn->reply, &QNetworkReply::finished, this, &NetworkAccess::onDownloadFinished);
|
2019-09-17 15:16:09 +00:00
|
|
|
downloads.insert(std::make_pair(url, dwn));
|
2021-04-18 12:49:20 +00:00
|
|
|
emit loadFileProgress(dwn->messages, 0, false);
|
2019-09-17 15:16:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 12:09:15 +00:00
|
|
|
void Core::NetworkAccess::onUploadError(QNetworkReply::NetworkError code)
|
|
|
|
{
|
|
|
|
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
|
|
|
QString url = rpl->url().toString();
|
|
|
|
std::map<QString, Transfer*>::const_iterator itr = uploads.find(url);
|
|
|
|
if (itr == uploads.end()) {
|
2021-04-18 12:49:20 +00:00
|
|
|
qDebug() << "an error uploading" << url << ": the request is reporting an error but there is no record of it being uploading, ignoring";
|
2019-09-23 12:09:15 +00:00
|
|
|
} else {
|
|
|
|
QString errorText = getErrorText(code);
|
|
|
|
if (errorText.size() > 0) {
|
|
|
|
itr->second->success = false;
|
|
|
|
Transfer* upl = itr->second;
|
2021-04-18 12:49:20 +00:00
|
|
|
emit loadFileError(upl->messages, errorText, true);
|
2019-09-23 12:09:15 +00:00
|
|
|
}
|
2021-04-18 12:49:20 +00:00
|
|
|
|
|
|
|
//TODO deletion?
|
2019-09-23 12:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::NetworkAccess::onUploadFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
|
|
|
QString url = rpl->url().toString();
|
|
|
|
std::map<QString, Transfer*>::const_iterator itr = uploads.find(url);
|
|
|
|
if (itr == downloads.end()) {
|
2021-04-18 12:49:20 +00:00
|
|
|
qDebug() << "an error uploading" << url << ": the request is done there is no record of it being uploading, ignoring";
|
2019-09-23 12:09:15 +00:00
|
|
|
} else {
|
|
|
|
Transfer* upl = itr->second;
|
|
|
|
if (upl->success) {
|
|
|
|
qDebug() << "upload success for" << url;
|
2021-04-18 12:49:20 +00:00
|
|
|
|
|
|
|
storage.addFile(upl->messages, upl->url, upl->path);
|
|
|
|
emit uploadFileComplete(upl->messages, upl->url);
|
2019-09-23 12:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
upl->reply->deleteLater();
|
|
|
|
upl->file->close();
|
|
|
|
upl->file->deleteLater();
|
|
|
|
delete upl;
|
|
|
|
uploads.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::NetworkAccess::onUploadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
|
|
|
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
|
|
|
QString url = rpl->url().toString();
|
|
|
|
std::map<QString, Transfer*>::const_iterator itr = uploads.find(url);
|
|
|
|
if (itr == uploads.end()) {
|
2019-11-09 14:04:27 +00:00
|
|
|
qDebug() << "an error downloading" << url << ": the request had some progress but seems like no one is waiting for it, skipping";
|
2019-09-23 12:09:15 +00:00
|
|
|
} else {
|
|
|
|
Transfer* upl = itr->second;
|
|
|
|
qreal received = bytesReceived;
|
|
|
|
qreal total = bytesTotal;
|
|
|
|
qreal progress = received/total;
|
|
|
|
upl->progress = progress;
|
2021-04-18 12:49:20 +00:00
|
|
|
emit loadFileProgress(upl->messages, progress, true);
|
2019-09-23 12:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
QString Core::NetworkAccess::getFileRemoteUrl(const QString& path)
|
|
|
|
{
|
|
|
|
QString p;
|
|
|
|
|
|
|
|
try {
|
|
|
|
QString p = storage.getUrl(path);
|
|
|
|
} catch (const Archive::NotFound& err) {
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::NetworkAccess::uploadFile(const Shared::MessageInfo& info, const QString& path, const QUrl& put, const QUrl& get, const QMap<QString, QString> headers)
|
2019-09-23 12:09:15 +00:00
|
|
|
{
|
|
|
|
QFile* file = new QFile(path);
|
2021-04-18 12:49:20 +00:00
|
|
|
Transfer* upl = new Transfer({{info}, 0, 0, true, path, get.toString(), file});
|
|
|
|
QNetworkRequest req(put);
|
|
|
|
for (QMap<QString, QString>::const_iterator itr = headers.begin(), end = headers.end(); itr != end; itr++) {
|
|
|
|
req.setRawHeader(itr.key().toUtf8(), itr.value().toUtf8());
|
|
|
|
}
|
2019-09-23 12:09:15 +00:00
|
|
|
if (file->open(QIODevice::ReadOnly)) {
|
|
|
|
upl->reply = manager->put(req, file);
|
|
|
|
|
2019-11-09 14:04:27 +00:00
|
|
|
connect(upl->reply, &QNetworkReply::uploadProgress, this, &NetworkAccess::onUploadProgress);
|
2020-08-04 14:52:16 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
2020-07-29 20:26:56 +00:00
|
|
|
connect(upl->reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::errorOccurred), this, &NetworkAccess::onUploadError);
|
2020-08-04 14:52:16 +00:00
|
|
|
#else
|
|
|
|
connect(upl->reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::error), this, &NetworkAccess::onUploadError);
|
|
|
|
#endif
|
2019-11-09 14:04:27 +00:00
|
|
|
connect(upl->reply, &QNetworkReply::finished, this, &NetworkAccess::onUploadFinished);
|
2021-04-18 12:49:20 +00:00
|
|
|
uploads.insert(std::make_pair(put.toString(), upl));
|
|
|
|
emit loadFileProgress(upl->messages, 0, true);
|
2019-09-23 12:09:15 +00:00
|
|
|
} else {
|
|
|
|
qDebug() << "couldn't upload file" << path;
|
2021-04-18 12:49:20 +00:00
|
|
|
emit loadFileError(upl->messages, "Error opening file", true);
|
2019-09-23 12:09:15 +00:00
|
|
|
delete file;
|
2021-04-18 12:49:20 +00:00
|
|
|
delete upl;
|
2019-09-23 12:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
void Core::NetworkAccess::registerFile(const QString& url, const QString& account, const QString& jid, const QString& id)
|
2019-09-23 12:09:15 +00:00
|
|
|
{
|
2021-04-18 12:49:20 +00:00
|
|
|
storage.addFile(url, account, jid, id);
|
|
|
|
std::map<QString, Transfer*>::iterator itr = downloads.find(url);
|
|
|
|
if (itr != downloads.end()) {
|
|
|
|
itr->second->messages.emplace_back(account, jid, id); //TODO notification is going to happen the next tick, is that okay?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::NetworkAccess::registerFile(const QString& url, const QString& path, const QString& account, const QString& jid, const QString& id)
|
|
|
|
{
|
|
|
|
storage.addFile(url, path, account, jid, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Core::NetworkAccess::checkAndAddToUploading(const QString& acc, const QString& jid, const QString id, const QString path)
|
|
|
|
{
|
|
|
|
for (const std::pair<const QString, Transfer*>& pair : uploads) {
|
|
|
|
Transfer* info = pair.second;
|
|
|
|
if (pair.second->path == path) {
|
|
|
|
std::list<Shared::MessageInfo>& messages = info->messages;
|
|
|
|
bool dup = false;
|
|
|
|
for (const Shared::MessageInfo& info : messages) {
|
|
|
|
if (info.account == acc && info.jid == jid && info.messageId == id) {
|
|
|
|
dup = true;
|
|
|
|
break;
|
2019-11-11 15:19:54 +00:00
|
|
|
}
|
2019-09-23 12:09:15 +00:00
|
|
|
}
|
2021-04-18 12:49:20 +00:00
|
|
|
if (!dup) {
|
|
|
|
info->messages.emplace_back(acc, jid, id); //TODO notification is going to happen the next tick, is that okay?
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-23 12:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-18 12:49:20 +00:00
|
|
|
|
|
|
|
return false;
|
2019-09-23 12:09:15 +00:00
|
|
|
}
|
2019-11-09 14:04:27 +00:00
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
QString Core::NetworkAccess::prepareDirectory(const QString& jid)
|
2019-11-09 14:04:27 +00:00
|
|
|
{
|
2021-04-18 12:49:20 +00:00
|
|
|
QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
|
|
|
path += "/" + QApplication::applicationName();
|
|
|
|
if (jid.size() > 0) {
|
|
|
|
path += "/" + jid;
|
|
|
|
}
|
|
|
|
QDir location(path);
|
|
|
|
|
|
|
|
if (!location.exists()) {
|
|
|
|
bool res = location.mkpath(path);
|
|
|
|
if (!res) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
2019-11-09 14:04:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
QString Core::NetworkAccess::checkFileName(const QString& name, const QString& path)
|
2019-11-09 14:04:27 +00:00
|
|
|
{
|
2021-04-18 12:49:20 +00:00
|
|
|
QStringList parts = name.split(".");
|
|
|
|
QString suffix("");
|
|
|
|
QStringList::const_iterator sItr = parts.begin();
|
|
|
|
QString realName = *sItr;
|
|
|
|
++sItr;
|
|
|
|
for (QStringList::const_iterator sEnd = parts.end(); sItr != sEnd; ++sItr) {
|
|
|
|
suffix += "." + (*sItr);
|
|
|
|
}
|
|
|
|
QString postfix("");
|
2021-04-19 21:49:24 +00:00
|
|
|
QFileInfo proposedName(path + "/" + realName + suffix);
|
2021-04-18 12:49:20 +00:00
|
|
|
int counter = 0;
|
|
|
|
while (proposedName.exists()) {
|
|
|
|
QString count = QString("(") + std::to_string(++counter).c_str() + ")";
|
2021-04-19 21:49:24 +00:00
|
|
|
proposedName = QFileInfo(path + "/" + realName + count + suffix);
|
2021-04-18 12:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return proposedName.absoluteFilePath();
|
2019-11-09 14:04:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
QString Core::NetworkAccess::addMessageAndCheckForPath(const QString& url, const QString& account, const QString& jid, const QString& id)
|
2019-11-09 14:04:27 +00:00
|
|
|
{
|
2021-04-18 12:49:20 +00:00
|
|
|
return storage.addMessageAndCheckForPath(url, account, jid, id);
|
2019-11-09 14:04:27 +00:00
|
|
|
}
|