Page MenuHomePhorge

FileHandler.qml
No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None

FileHandler.qml

/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2023 nannanko <nannanko@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import moe.kazv.mxc.kazv 0.0 as MK
import QtQml 2.15
import QtQuick 2.15
/**
* Used to handle files described in event, such as caching(if autoCache is true) and download.
* It is either used for caching or downloading. These two operations cannot be performed in the same FileHandler.
*/
QtObject {
id: fileHandler
required property var eventContent
/**
* If autoCache is true, the file is automatically cached(thumbnail is downloaded if exists).
*/
required property bool autoCache
required property var kazvIOManager
required property var matrixSdk
property var kazvIOJob
property real percent: fileHandler.kazvIOJob ? fileHandler.kazvIOJob.progress : 0.0 // from 0 to 1
property var result
property bool success
/**
* This attribute is only used after result is set (that is, after signal resultChanged is emitted),
* and only when success is true, this attribute has a correct and valid value.
*/
property url cachedFile
property var fileInfo: "info" in eventContent ? eventContent.info : {}
property var encryptedFile: "file" in eventContent ? eventContent.file : {}
property var encryptedFileMxcUri: "url" in encryptedFile ? encryptedFile.url : ""
property var encryptedFileSha256: "hashes" in encryptedFile && "sha256" in encryptedFile.hashes ? encryptedFile.hashes.sha256 : ""
// property var encryptedFileAlg: "key" in encryptedFile && "alg" in encryptedFile.key ? encryptedFile.key.alg : ""
property var encryptedFileKey: "key" in encryptedFile && "k" in encryptedFile.key ? encryptedFile.key.k : ""
property var encryptedFileIv: "iv" in encryptedFile ? encryptedFile.iv : ""
property var encryptedThumbnail: "thumbnail_file" in fileInfo ? fileInfo.thumbnail_file : {}
property var encryptedThumbnailMxcUri: "url" in encryptedThumbnail ? enceyptedThumbnail.url : ""
property var encryptedThumbnailSha256: "hashes" in encryptedThumbnail && "sha256" in enceyptedThumbnail.hashes ? enceyptedThumbnail.hashes.sha256 : ""
// property var encryptedFileAlg: "key" in encryptedThumbnail && "alg" in encryptedThumbnail.key ? encryptedThumbnail.key.alg : ""
property var encryptedThumbnailKey: "key" in encryptedThumbnail && "k" in encryptedThumbnail.key ? encryptedThumbnail.key.k : ""
property var encryptedThumbnailIv: "iv" in encryptedThumbnail ? encryptedThumbnail.iv : ""
property var unencryptedFileMxcUri: "url" in eventContent ? eventContent.url : ""
property var unencryptedThumbnailMxcUri: "thumbnail_url" in fileInfo ? fileInfo.thumbnail_url : ""
/**
* Emit when start download, not emit when start cache.
* Can be used to indicate progress bar visible
*/
signal startDownload
onEventContentChanged: {
if (!fileHandler.autoCache) {
return
}
let mxcUri = ""
let sha256 = "", key = "", iv = ""
// Check if there is an encrypted thumbnail, unencrypted thumbnail , encrypted file or unencrypted file
if (encryptedThumbnailMxcUri &&
encryptedThumbnailSha256 &&
encryptedThumbnailKey &&
encryptedThumbnailIv) {
mxcUri = encryptedThumbnailMxcUri
sha256 = encryptedThumbnailSha256
key = encryptedThumbnailKey
iv = encryptedThumbnailIv
} else if (unencryptedThumbnailMxcUri) {
mxcUri = unencryptedThumbnailMxcUri
} else if (encryptedFileMxcUri &&
encryptedFileSha256 &&
encryptedFileKey &&
encryptedFileIv) {
mxcUri = encryptedFileMxcUri
sha256 = encryptedFileSha256
key = encryptedFileKey
iv = encryptedFileIv
} else if (unencryptedFileMxcUri) {
mxcUri = unencryptedFileMxcUri
} else {
return
}
const mediaId = fileHandler.getMediaId(mxcUri)
const url = fileHandler.matrixSdk.mxcUriToHttp(mxcUri)
fileHandler.cachedFile = fileHandler.kazvIOManager.cacheFile(url, mediaId, sha256, key, iv)
fileHandler.updateKazvIOJob(mediaId)
}
function getMediaId(mxcUri) {
return mxcUri.toString().split('/').slice(-1)[0]
}
function downloadFile(saveFileUrl) {
let mxcUri = ""
let sha256 = "", key = "", iv = ""
if (encryptedFileMxcUri &&
encryptedFileSha256 &&
encryptedFileKey &&
encryptedFileIv) {
mxcUri = encryptedFileMxcUri
sha256 = encryptedFileSha256
key = encryptedFileKey
iv = encryptedFileIv
} else if (unencryptedFileMxcUri) {
mxcUri = unencryptedFileMxcUri
} else {
return
}
const url = fileHandler.matrixSdk.mxcUriToHttp(mxcUri)
const mediaId = fileHandler.getMediaId(mxcUri)
fileHandler.kazvIOManager.startNewDownloadJob(url,saveFileUrl, mediaId, sha256, key, iv)
fileHandler.updateKazvIOJob(mediaId)
startDownload()
}
function setResult(ec) {
fileHandler.result = ec
if (ec == MK.KazvIOBaseJob.NoError) {
fileHandler.success = true
} else {
fileHandler.success = false
}
}
function updateKazvIOJob(mediaId) {
fileHandler.kazvIOJob = fileHandler.kazvIOManager.getDownloadJob(mediaId)
if (kazvIOJob) {
fileHandler.kazvIOJob.result.connect(fileHandler.setResult)
}
}
Component.onCompleted: {
fileHandler.updateKazvIOJob()
}
}

File Metadata

Mime Type
text/plain
Expires
Wed, May 14, 7:28 AM (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
166744
Default Alt Text
FileHandler.qml (5 KB)

Event Timeline