2022-08-30 16:13:11 +05:00
|
|
|
/* This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2022 Streetwriters (Private) Limited
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
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-11-18 15:17:02 +05:00
|
|
|
async downloadFile(groupId, filename, chunkSize, metadata) {
|
|
|
|
|
const url = `${hosts.API_HOST}/s3?name=${filename}`;
|
2021-09-26 11:47:13 +05:00
|
|
|
const token = await this.tokenManager.getAccessToken();
|
2021-11-18 15:17:02 +05:00
|
|
|
const { execute, cancel } = this.fs.downloadFile(filename, {
|
|
|
|
|
metadata,
|
2021-09-26 11:47:13 +05:00
|
|
|
url,
|
2021-10-30 13:49:41 +05:00
|
|
|
chunkSize,
|
2021-09-26 11:47:13 +05:00
|
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
|
|
|
});
|
2021-11-18 15:17:02 +05:00
|
|
|
this._queue.push({ groupId, filename, 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-11-18 15:17:02 +05:00
|
|
|
async uploadFile(groupId, filename) {
|
2021-09-26 11:47:13 +05:00
|
|
|
const token = await this.tokenManager.getAccessToken();
|
2021-11-18 15:17:02 +05:00
|
|
|
const url = `${hosts.API_HOST}/s3?name=${filename}`;
|
|
|
|
|
const { execute, cancel } = this.fs.uploadFile(filename, {
|
2021-10-15 11:12:05 +05:00
|
|
|
url,
|
|
|
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
|
|
|
});
|
2021-11-18 15:17:02 +05:00
|
|
|
this._queue.push({ groupId, filename, 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:53:46 +05:00
|
|
|
const [op] = this._deleteOp(groupId, type);
|
|
|
|
|
if (!op) return;
|
|
|
|
|
await op.cancel("Operation canceled.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 11:41:17 +05:00
|
|
|
async deleteFile(filename, localOnly) {
|
|
|
|
|
if (localOnly) return await this.fs.deleteFile(filename);
|
|
|
|
|
|
2022-02-28 12:59:58 +05:00
|
|
|
const token = await this.tokenManager.getAccessToken();
|
2021-11-18 15:17:02 +05:00
|
|
|
const url = `${hosts.API_HOST}/s3?name=${filename}`;
|
2021-09-26 11:47:13 +05:00
|
|
|
return await this.fs.deleteFile(filename, {
|
|
|
|
|
url,
|
|
|
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {string} filename
|
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
|
*/
|
|
|
|
|
exists(filename) {
|
|
|
|
|
return this.fs.exists(filename);
|
|
|
|
|
}
|
2021-11-02 14:31:30 +05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
|
|
|
|
clear() {
|
|
|
|
|
return this.fs.clearFileStorage();
|
|
|
|
|
}
|
2021-09-26 11:47:13 +05:00
|
|
|
}
|