core: add download start & canceled events

This commit is contained in:
ammarahm-ed
2023-09-20 19:44:04 +05:00
committed by Ammar Ahmed
parent 6ba18c5bd1
commit 883fe48946
2 changed files with 26 additions and 4 deletions

View File

@@ -89,6 +89,10 @@ export const EVENTS = {
noteRemoved: "note:removed",
tokenRefreshed: "token:refreshed",
userUnauthorized: "user:unauthorized",
downloadCanceled: "file:downloadCanceled",
uploadCanceled: "file:uploadCanceled",
fileDownload: "file:download",
fileUpload: "file:upload",
fileDownloaded: "file:downloaded",
fileUploaded: "file:uploaded",
attachmentDeleted: "attachment:deleted",

View File

@@ -45,8 +45,14 @@ export default class FileStorage {
});
file.cancel = cancel;
const result = await execute().catch(() => false);
EV.publish(EVENTS.fileDownload, {
total,
current,
groupId,
filename
});
const result = await execute().catch(() => false);
if (eventData)
EV.publish(EVENTS.fileDownloaded, {
success: result,
@@ -75,6 +81,13 @@ export default class FileStorage {
});
file.cancel = cancel;
EV.publish(EVENTS.fileUpload, {
total,
current,
groupId,
filename
});
let error = null;
const result = await execute().catch((e) => {
console.error("Failed to upload attachment:", e);
@@ -111,7 +124,7 @@ export default class FileStorage {
async cancel(groupId, type) {
const queue =
type === "downloads"
type === "download"
? this.downloads.get(groupId)
: this.uploads.get(groupId);
if (!queue) return;
@@ -120,8 +133,13 @@ export default class FileStorage {
if (file.cancel) await file.cancel("Operation canceled.");
queue.splice(i, 1);
}
if (type === "download") this.downloads.delete(groupId);
else if (type === "upload") this.uploads.delete(groupId);
if (type === "download") {
this.downloads.delete(groupId);
EV.publish(EVENTS.downloadCanceled, { groupId, canceled: true });
} else if (type === "upload") {
this.uploads.delete(groupId);
EV.publish(EVENTS.uploadCanceled, { groupId, canceled: true });
}
}
readEncrypted(filename, encryptionKey, cipherData) {