2021-09-26 11:47:13 +05:00
|
|
|
import hosts from "../utils/constants";
|
|
|
|
|
import TokenManager from "../api/token-manager";
|
|
|
|
|
|
|
|
|
|
export default class FileStorage {
|
|
|
|
|
constructor(fs, storage) {
|
|
|
|
|
this.fs = fs;
|
|
|
|
|
this.tokenManager = new TokenManager(storage);
|
2021-09-29 09:53:50 +05:00
|
|
|
this._queue = [];
|
2021-09-26 11:47:13 +05:00
|
|
|
}
|
|
|
|
|
|
2021-09-29 09:53:50 +05:00
|
|
|
async downloadFile(groupId, hash) {
|
2021-09-26 11:47:13 +05:00
|
|
|
const url = `${hosts.API_HOST}/s3?name=${hash}`;
|
|
|
|
|
const token = await this.tokenManager.getAccessToken();
|
2021-09-29 09:53:50 +05:00
|
|
|
const { execute, cancel } = this.fs.downloadFile(hash, {
|
2021-09-26 11:47:13 +05:00
|
|
|
url,
|
|
|
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
|
|
|
});
|
2021-09-29 09:53:50 +05:00
|
|
|
this._queue.push({ groupId, hash, cancel, type: "download" });
|
2021-10-01 11:53:46 +05:00
|
|
|
const result = await execute();
|
|
|
|
|
this._deleteOp(groupId, "download");
|
|
|
|
|
return result;
|
2021-09-26 11:47:13 +05:00
|
|
|
}
|
|
|
|
|
|
2021-09-29 09:53:50 +05:00
|
|
|
async uploadFile(groupId, hash) {
|
2021-09-26 11:47:13 +05:00
|
|
|
const token = await this.tokenManager.getAccessToken();
|
|
|
|
|
const url = await this._getPresignedURL(hash, token, "PUT");
|
2021-09-29 09:53:50 +05:00
|
|
|
const { execute, cancel } = this.fs.uploadFile(hash, { url });
|
|
|
|
|
this._queue.push({ groupId, hash, cancel, type: "upload" });
|
2021-10-01 11:53:46 +05:00
|
|
|
const result = await execute();
|
|
|
|
|
this._deleteOp(groupId, "upload");
|
|
|
|
|
return result;
|
2021-09-29 09:53:50 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async cancel(groupId, type = undefined) {
|
2021-10-01 11:40:18 +05:00
|
|
|
console.trace("Cancelling", groupId, type);
|
2021-10-01 11:53:46 +05:00
|
|
|
const [op] = this._deleteOp(groupId, type);
|
|
|
|
|
if (!op) return;
|
|
|
|
|
await op.cancel("Operation canceled.");
|
|
|
|
|
console.log("Cancellation done:", groupId, this._queue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_deleteOp(groupId, type = undefined) {
|
|
|
|
|
const opIndex = this._queue.findIndex(
|
|
|
|
|
(item) => item.groupId === groupId && (!type || item.type === type)
|
2021-09-29 09:53:50 +05:00
|
|
|
);
|
2021-10-01 11:53:46 +05:00
|
|
|
if (opIndex < 0) return [];
|
|
|
|
|
return this._queue.splice(opIndex, 1);
|
2021-09-26 11:47:13 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readEncrypted(filename, encryptionKey, cipherData) {
|
|
|
|
|
return this.fs.readEncrypted(filename, encryptionKey, cipherData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeEncrypted(filename, data, type, encryptionKey) {
|
2021-09-29 09:53:50 +05:00
|
|
|
return this.fs.writeEncrypted(filename, {
|
2021-09-26 11:47:13 +05:00
|
|
|
data,
|
|
|
|
|
type,
|
|
|
|
|
key: encryptionKey,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deleteFile(filename) {
|
|
|
|
|
const token = await this.tokenManager.getToken();
|
|
|
|
|
const url = `${hosts.API_HOST}/s3?name=${hash}`;
|
|
|
|
|
return await this.fs.deleteFile(filename, {
|
|
|
|
|
url,
|
|
|
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {string} filename
|
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
|
*/
|
|
|
|
|
exists(filename) {
|
|
|
|
|
return this.fs.exists(filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _getPresignedURL(filename, token, verb) {
|
|
|
|
|
const response = await fetch(`${hosts.API_HOST}/s3?name=${filename}`, {
|
|
|
|
|
method: verb,
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (response.ok) return await response.text();
|
|
|
|
|
throw new Error("Couldn't get presigned url.");
|
|
|
|
|
}
|
|
|
|
|
}
|