2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2021-10-10 18:56:05 +05:00
|
|
|
import localforage from "localforage";
|
|
|
|
|
import FileHandle from "./src/filehandle";
|
|
|
|
|
import { IStreamableFS } from "./src/interfaces";
|
|
|
|
|
import { File } from "./src/types";
|
|
|
|
|
|
|
|
|
|
export class StreamableFS implements IStreamableFS {
|
|
|
|
|
private storage: LocalForage;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param db name of the indexeddb database
|
|
|
|
|
*/
|
|
|
|
|
constructor(db: string) {
|
|
|
|
|
this.storage = localforage.createInstance({
|
|
|
|
|
storeName: "streamable-fs",
|
|
|
|
|
name: db,
|
2022-08-26 16:19:39 +05:00
|
|
|
driver: [localforage.INDEXEDDB]
|
2021-10-10 18:56:05 +05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createFile(
|
|
|
|
|
filename: string,
|
|
|
|
|
size: number,
|
|
|
|
|
type: string
|
|
|
|
|
): Promise<FileHandle> {
|
|
|
|
|
if (await this.exists(filename)) throw new Error("File already exists.");
|
|
|
|
|
|
|
|
|
|
const file: File = await this.storage.setItem<File>(filename, {
|
|
|
|
|
filename,
|
|
|
|
|
size,
|
|
|
|
|
type,
|
2022-08-26 16:19:39 +05:00
|
|
|
chunks: 0
|
2021-10-10 18:56:05 +05:00
|
|
|
});
|
|
|
|
|
return new FileHandle(this.storage, file);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 11:35:40 +05:00
|
|
|
async readFile(filename: string): Promise<FileHandle | undefined> {
|
2021-10-10 18:56:05 +05:00
|
|
|
const file = await this.storage.getItem<File>(filename);
|
2021-10-29 11:35:40 +05:00
|
|
|
if (!file) return undefined;
|
2021-10-10 18:56:05 +05:00
|
|
|
return new FileHandle(this.storage, file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async exists(filename: string): Promise<boolean> {
|
|
|
|
|
const file = await this.storage.getItem<File>(filename);
|
|
|
|
|
return !!file;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 11:35:40 +05:00
|
|
|
async deleteFile(filename: string): Promise<boolean> {
|
2021-10-10 18:56:05 +05:00
|
|
|
const handle = await this.readFile(filename);
|
2021-10-29 11:35:40 +05:00
|
|
|
if (!handle) return true;
|
2021-10-10 18:56:05 +05:00
|
|
|
await handle.delete();
|
2021-10-29 11:35:40 +05:00
|
|
|
return true;
|
2021-10-10 18:56:05 +05:00
|
|
|
}
|
2021-11-02 14:35:24 +05:00
|
|
|
|
|
|
|
|
async clear(): Promise<void> {
|
|
|
|
|
await this.storage.clear();
|
|
|
|
|
}
|
2021-10-10 18:56:05 +05:00
|
|
|
}
|