Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F139943
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/kazv-io-manager.cpp b/src/kazv-io-manager.cpp
index e010937..f1c99ad 100644
--- a/src/kazv-io-manager.cpp
+++ b/src/kazv-io-manager.cpp
@@ -1,174 +1,176 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2022-2023 nannanko <nannanko@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "kazv-io-manager.hpp"
#include "kazv-io-job.hpp"
#include "matrix-room.hpp"
#include "upload-job-model.hpp"
#include "qt-rand-adapter.hpp"
#include <api/csapi/content-repo.hpp>
#include <file-desc.hpp>
#include <QObject>
#include <QIODevice>
#include <QFile>
#include <QSaveFile>
#include <QPointer>
#include <QSharedPointer>
#include <QUrl>
#include <QMap>
#include <QMultiMap>
#include <QTemporaryDir>
#include <string>
static const QString ROOMLESS = QStringLiteral("not-a-room");
struct KazvIOManagerPrivate
{
/**
* Used to store various temporary files.
* If the user does not specify a cache directory,
* this directory is also used as a cache directory.
*/
QTemporaryDir tmpDir = QTemporaryDir{};
/**
* Stores files that need to be previewed (e.g. in a timeline)
* and are not downloaded by the user, such as images, audio, video covers, and videos.
*/
QDir cacheDir{tmpDir.path()};
Kazv::RandomInterface randomGenerator = QtRandAdapter{};
- QMap<QString, QSharedPointer<KazvIODownloadJob>> downloadJobs;
+ QMap<QString, QSharedPointer<KazvIODownloadJob>> downloadJobs; // 用了 parent 不能用 QsharedPointer
QMap<QString, QSharedPointer<UploadJobModel>> uploadJobs;
};
KazvIOManager::KazvIOManager(QObject *parent)
: QObject(parent)
, m_d(new KazvIOManagerPrivate)
{
}
KazvIOManager::~KazvIOManager() = default;
QUrl KazvIOManager::cacheFile(const QUrl &fileUrl, const QString &id, const QString &hash,
const QString &key, const QString &iv)
{
const QString filePath = m_d->cacheDir.filePath(id);
QDir dir(m_d->cacheDir.path());
if (!dir.exists(id) && this->getDownloadJob(id) == nullptr) {
auto job = this->startNewDownloadJob(fileUrl, QUrl::fromLocalFile(filePath), id, hash, key, iv);
connect(job, &KazvIOBaseJob::result, this,
[this, id](auto ec) {
if (ec == KazvIOBaseJob::NoError) {
m_d->downloadJobs.remove(id);
}
});
}
return QUrl::fromLocalFile(filePath);
}
KazvIOBaseJob *KazvIOManager::startNewDownloadJob(const QUrl &serverUrl, const QUrl &localFileUrl,
const QString &jobId, const QString &hash, const QString &key, const QString &iv)
{
auto downloadJob = QSharedPointer<KazvIODownloadJob>{};
if (key == "") {
- downloadJob.reset(new KazvIODownloadJob{localFileUrl.toLocalFile(), serverUrl, hash, std::nullopt, this});
+ downloadJob.reset(new KazvIODownloadJob{localFileUrl.toLocalFile(), serverUrl, hash, std::nullopt});
} else {
auto aes = Kazv::AES256CTRDesc{key.toStdString(), iv.toStdString()};
- downloadJob.reset(new KazvIODownloadJob{localFileUrl.toLocalFile(), serverUrl, hash, aes, this});
+ downloadJob.reset(new KazvIODownloadJob{localFileUrl.toLocalFile(), serverUrl, hash, aes});
}
m_d->downloadJobs[jobId] = downloadJob;
return downloadJob.data();
}
KazvIOBaseJob *KazvIOManager::startNewUploadJob(const QUrl &serverUrl, const QUrl &localFileUrl,
const QString &token, const QString &roomId, MatrixRoomList *roomList, const bool encrypted)
{
auto uploadJob = QPointer<KazvIOUploadJob>();
if (encrypted) {
auto aes = Kazv::AES256CTRDesc::fromRandom(
m_d->randomGenerator.generateRange<Kazv::RandomData>(Kazv::AES256CTRDesc::randomSize));
uploadJob = new KazvIOUploadJob(localFileUrl.toLocalFile(), serverUrl,
- roomList, roomId, token, aes, this);
+ roomList, roomId, token, aes);
} else {
uploadJob = new KazvIOUploadJob(localFileUrl.toLocalFile(), serverUrl,
- roomList, roomId, token, std::nullopt, this);
+ roomList, roomId, token, std::nullopt);
}
if (!m_d->uploadJobs.contains(roomId) || m_d->uploadJobs[roomId].isNull()) {
- m_d->uploadJobs[roomId].reset(new UploadJobModel(this));
+ m_d->uploadJobs[roomId].reset(new UploadJobModel());
}
m_d->uploadJobs[roomId]->addJob(uploadJob);
return uploadJob.data();
}
KazvIOBaseJob *KazvIOManager::startNewRoomlessUploadJob(const QUrl &serverUrl, const QUrl &localFileUrl, const QString &token)
{
return startNewUploadJob(serverUrl, localFileUrl, token, ROOMLESS, 0, false);
}
KazvIOBaseJob *KazvIOManager::getDownloadJob(const QString &jobId)
{
auto targetJob = m_d->downloadJobs.find(jobId);
if (targetJob == m_d->downloadJobs.end()) {
return nullptr;
}
+ QQmlEngine::setObjectOwnership(targetJob.value().data(), QQmlEngine::CppOwnership);
return targetJob.value().data();
}
void KazvIOManager::deleteDownloadJob(const QString &jobId)
{
m_d->downloadJobs.remove(jobId);
}
UploadJobModel *KazvIOManager::getUploadJobs(const QString &roomId)
{
if (!m_d->uploadJobs.contains(roomId) || m_d->uploadJobs[roomId].isNull()) {
- m_d->uploadJobs[roomId].reset(new UploadJobModel(this));
+ m_d->uploadJobs[roomId].reset(new UploadJobModel());
}
+ QQmlEngine::setObjectOwnership(m_d->uploadJobs[roomId].data(), QQmlEngine::CppOwnership);
return m_d->uploadJobs[roomId].data();
}
void KazvIOManager::deleteUploadJob(const QString &roomId, KazvIOBaseJob *job)
{
m_d->uploadJobs[roomId]->removeJob(QPointer<KazvIOUploadJob>(qobject_cast<KazvIOUploadJob*>(job)));
}
void KazvIOManager::deleteRoomlessUploadJob(KazvIOBaseJob *job)
{
deleteUploadJob(ROOMLESS, job);
}
void KazvIOManager::deleteModelIfEmpty(const QString &roomId)
{
if (!m_d->uploadJobs.contains(roomId)) {
return;
}
if (m_d->uploadJobs[roomId]->rowCount() == 0) {
m_d->uploadJobs.remove(roomId);
}
}
QString KazvIOManager::cacheDirectory() const
{
return m_d->cacheDir.path();
}
void KazvIOManager::setCacheDirectory(QString cacheDirectory)
{
if (cacheDirectory == "") {
m_d->cacheDir = QDir{m_d->tmpDir.path()};
} else {
auto cacheDir = QDir{cacheDirectory};
if (!cacheDir.mkpath(cacheDirectory)) {
return;
}
m_d->cacheDir = cacheDir;
}
m_d->downloadJobs.clear();
Q_EMIT cacheDirectoryChanged();
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Jan 19, 8:35 AM (1 h, 22 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55031
Default Alt Text
(6 KB)
Attached To
Mode
rK kazv
Attached
Detach File
Event Timeline
Log In to Comment