feat: add upload/download cancellation support

This commit is contained in:
thecodrr
2021-09-29 09:53:50 +05:00
parent 7d16b8f388
commit 6b619e5d3d
14 changed files with 121 additions and 91 deletions

View File

@@ -5,21 +5,36 @@ export default class FileStorage {
constructor(fs, storage) {
this.fs = fs;
this.tokenManager = new TokenManager(storage);
this._queue = [];
}
async downloadFile(hash) {
async downloadFile(groupId, hash) {
const url = `${hosts.API_HOST}/s3?name=${hash}`;
const token = await this.tokenManager.getAccessToken();
return await this.fs.downloadFile(hash, {
const { execute, cancel } = this.fs.downloadFile(hash, {
url,
headers: { Authorization: `Bearer ${token}` },
});
this._queue.push({ groupId, hash, cancel, type: "download" });
return await execute();
}
async uploadFile(hash) {
async uploadFile(groupId, hash) {
const token = await this.tokenManager.getAccessToken();
const url = await this._getPresignedURL(hash, token, "PUT");
return await this.fs.uploadFile(hash, { url });
const { execute, cancel } = this.fs.uploadFile(hash, { url });
this._queue.push({ groupId, hash, cancel, type: "upload" });
return await execute();
}
async cancel(groupId, type = undefined) {
await Promise.all(
this._queue
.filter(
(item) => item.groupId === groupId && (!type || item.type === type)
)
.map(async (op) => await op.cancel())
);
}
readEncrypted(filename, encryptionKey, cipherData) {
@@ -27,7 +42,7 @@ export default class FileStorage {
}
writeEncrypted(filename, data, type, encryptionKey) {
return this._db.fs.writeEncrypted(filename, {
return this.fs.writeEncrypted(filename, {
data,
type,
key: encryptionKey,