2013-09-10 18:01:46 +01:00
|
|
|
/*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
* SPDX-FileCopyrightText: 2015 Aleix Pol i Gonzalez <aleixpol@kde.org>
|
2013-09-10 18:01:46 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2013-09-10 18:01:46 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filetransferjob.h"
|
2015-05-04 23:41:39 +01:00
|
|
|
#include "daemon.h"
|
2014-09-21 23:59:34 +01:00
|
|
|
#include <core_debug.h>
|
2013-09-10 18:01:46 +01:00
|
|
|
|
2014-09-21 23:59:34 +01:00
|
|
|
#include <QDebug>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QFileInfo>
|
2023-04-10 09:29:08 +01:00
|
|
|
#include <QNetworkAccessManager>
|
2023-07-16 15:20:34 +01:00
|
|
|
#include <qalgorithms.h>
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2023-05-25 01:12:29 +01:00
|
|
|
#include <KFileUtils>
|
2023-07-16 15:20:34 +01:00
|
|
|
#include <KLocalizedString>
|
2013-09-20 14:54:30 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
FileTransferJob::FileTransferJob(const NetworkPacket *np, const QUrl &destination)
|
2015-01-21 06:22:14 +00:00
|
|
|
: KJob()
|
2019-06-02 15:02:21 +01:00
|
|
|
, m_origin(np->payload())
|
2019-05-05 14:45:50 +01:00
|
|
|
, m_reply(nullptr)
|
2017-09-03 20:39:44 +01:00
|
|
|
, m_from(QStringLiteral("KDE Connect"))
|
|
|
|
, m_destination(destination)
|
|
|
|
, m_speedBytes(0)
|
|
|
|
, m_written(0)
|
2019-06-02 15:02:21 +01:00
|
|
|
, m_size(np->payloadSize())
|
|
|
|
, m_np(np)
|
2023-05-25 01:12:29 +01:00
|
|
|
, m_autoRename(false)
|
2013-09-10 18:01:46 +01:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
Q_ASSERT(m_origin);
|
2022-09-10 22:23:52 +01:00
|
|
|
// Disabled this assert: QBluetoothSocket doesn't report "->isReadable() == true" until it's connected
|
|
|
|
// Q_ASSERT(m_origin->isReadable());
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_destination.scheme().isEmpty()) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "Destination QUrl" << m_destination << "lacks a scheme. Setting its scheme to 'file'.";
|
|
|
|
m_destination.setScheme(QStringLiteral("file"));
|
2015-01-23 16:10:37 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 21:34:25 +00:00
|
|
|
setCapabilities(Killable);
|
2019-06-02 15:02:21 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "FileTransferJob Downloading payload to" << destination << "size:" << m_size;
|
2013-09-26 23:01:59 +01:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:01:46 +01:00
|
|
|
void FileTransferJob::start()
|
|
|
|
{
|
2014-03-03 21:33:22 +00:00
|
|
|
QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection);
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCDebug(KDECONNECT_CORE) << "FileTransferJob start";
|
2014-03-03 21:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::doStart()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_destination.isLocalFile() && QFile::exists(m_destination.toLocalFile())) {
|
2023-05-25 01:12:29 +01:00
|
|
|
if (m_autoRename) {
|
|
|
|
QFileInfo fileInfo(m_destination.toLocalFile());
|
|
|
|
QString path = fileInfo.path();
|
|
|
|
QString fileName = fileInfo.fileName();
|
|
|
|
m_destination.setPath(path + QStringLiteral("/") + KFileUtils::suggestName(QUrl(path), fileName), QUrl::DecodedMode);
|
|
|
|
} else {
|
|
|
|
setError(2);
|
|
|
|
setErrorText(i18n("Filename already present"));
|
|
|
|
emitResult();
|
|
|
|
return;
|
|
|
|
}
|
2014-03-03 21:33:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_origin->bytesAvailable())
|
2016-07-05 11:56:27 +01:00
|
|
|
startTransfer();
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_origin.data(), &QIODevice::readyRead, this, &FileTransferJob::startTransfer);
|
2014-03-03 21:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::startTransfer()
|
|
|
|
{
|
2017-08-01 23:35:24 +01:00
|
|
|
// Don't put each ready read
|
2017-09-03 21:00:06 +01:00
|
|
|
if (m_reply)
|
2017-08-01 23:35:24 +01:00
|
|
|
return;
|
|
|
|
|
2014-03-03 20:12:06 +00:00
|
|
|
setProcessedAmount(Bytes, 0);
|
2014-03-03 21:33:22 +00:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QNetworkRequest req(m_destination);
|
2017-09-03 21:00:06 +01:00
|
|
|
if (m_size >= 0) {
|
|
|
|
setTotalAmount(Bytes, m_size);
|
|
|
|
req.setHeader(QNetworkRequest::ContentLengthHeader, m_size);
|
2017-08-02 00:48:37 +01:00
|
|
|
}
|
2017-09-03 20:39:44 +01:00
|
|
|
m_reply = Daemon::instance()->networkAccessManager()->put(req, m_origin.data());
|
2015-09-11 16:03:11 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_reply, &QNetworkReply::uploadProgress, this, [this](qint64 bytesSent, qint64 /*bytesTotal*/) {
|
2017-09-03 21:00:06 +01:00
|
|
|
if (!m_timer.isValid())
|
|
|
|
m_timer.start();
|
2015-05-04 23:41:39 +01:00
|
|
|
setProcessedAmount(Bytes, bytesSent);
|
2016-09-10 21:52:03 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
const auto elapsed = m_timer.elapsed();
|
2016-09-10 21:52:03 +01:00
|
|
|
if (elapsed > 0) {
|
2017-08-01 22:18:15 +01:00
|
|
|
emitSpeed((1000 * bytesSent) / elapsed);
|
2016-09-10 21:52:03 +01:00
|
|
|
}
|
2020-05-09 15:46:52 +01:00
|
|
|
|
2018-11-09 12:58:28 +00:00
|
|
|
m_written = bytesSent;
|
2015-05-04 23:41:39 +01:00
|
|
|
});
|
2020-05-09 15:54:22 +01:00
|
|
|
connect(m_reply, &QNetworkReply::errorOccurred, this, &FileTransferJob::transferFailed);
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_reply, &QNetworkReply::finished, this, &FileTransferJob::transferFinished);
|
2013-09-10 18:01:46 +01:00
|
|
|
}
|
|
|
|
|
2016-06-22 11:42:51 +01:00
|
|
|
void FileTransferJob::transferFailed(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Couldn't transfer the file successfully" << error << m_reply->errorString();
|
2016-06-22 11:42:51 +01:00
|
|
|
setError(error);
|
2017-09-03 20:39:44 +01:00
|
|
|
setErrorText(i18n("Received incomplete file: %1", m_reply->errorString()));
|
2016-06-22 11:42:51 +01:00
|
|
|
emitResult();
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
m_reply->close();
|
2016-06-22 11:42:51 +01:00
|
|
|
}
|
|
|
|
|
2015-05-04 23:41:39 +01:00
|
|
|
void FileTransferJob::transferFinished()
|
2013-09-10 18:01:46 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// TODO: MD5-check the file
|
2018-11-09 12:58:28 +00:00
|
|
|
if (m_size == m_written) {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Finished transfer" << m_destination;
|
|
|
|
emitResult();
|
|
|
|
} else {
|
2022-09-10 22:23:52 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Received incomplete file (" << m_written << "/" << m_size << "bytes ), deleting";
|
2020-05-09 15:46:52 +01:00
|
|
|
|
2019-05-02 17:49:50 +01:00
|
|
|
deleteDestinationFile();
|
|
|
|
|
2018-11-09 12:58:28 +00:00
|
|
|
setError(3);
|
|
|
|
setErrorText(i18n("Received incomplete file from: %1", m_from));
|
|
|
|
emitResult();
|
|
|
|
}
|
2013-09-10 18:01:46 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 17:49:50 +01:00
|
|
|
void FileTransferJob::deleteDestinationFile()
|
|
|
|
{
|
|
|
|
if (m_destination.isLocalFile() && QFile::exists(m_destination.toLocalFile())) {
|
|
|
|
QFile::remove(m_destination.toLocalFile());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 21:34:03 +00:00
|
|
|
bool FileTransferJob::doKill()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_reply) {
|
|
|
|
m_reply->close();
|
2014-03-03 21:34:03 +00:00
|
|
|
}
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_origin) {
|
|
|
|
m_origin->close();
|
2014-03-03 21:34:03 +00:00
|
|
|
}
|
2019-05-02 17:49:50 +01:00
|
|
|
|
|
|
|
deleteDestinationFile();
|
|
|
|
|
2014-03-03 21:34:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_filetransferjob.cpp"
|