A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: add support for bulk delete files Signed-off-by: 01zulfi <[email protected]>
01zulfi0aeea965c08b
diff --git a/apps/web/src/interfaces/fs.ts b/apps/web/src/interfaces/fs.tsindex 77c27629f..7bc5cb340 100644--- a/apps/web/src/interfaces/fs.ts+++ b/apps/web/src/interfaces/fs.ts@@ -667,6 +667,28 @@ async function deleteFile(}}+async function bulkDeleteFiles(+ filenames: string[],+ requestOptions?: RequestOptionsWithSignal+) {+ if (!requestOptions) {+ await streamablefs.bulkDeleteFiles(filenames);+ return true;+ }++ try {+ const { url, headers } = requestOptions;+ const response = await axios.post(url, { names: filenames }, { headers });++ const result = isSuccessStatusCode(response.status);+ if (result) await streamablefs.bulkDeleteFiles(filenames);+ return result;+ } catch (e) {+ showError(toS3Error(e), "Could not bulk delete files");+ return false;+ }+}+/*** `-1` means an error during file size*@@ -701,6 +723,7 @@ export const FileStorage: IFileStorage = {uploadFile: cancellable(uploadFile),downloadFile: cancellable(downloadFile),deleteFile,+ bulkDeleteFiles,exists,clearFileStorage,hashBase64,diff --git a/packages/core/__mocks__/fs.mock.ts b/packages/core/__mocks__/fs.mock.tsindex e5044c30b..f191b9a63 100644--- a/packages/core/__mocks__/fs.mock.ts+++ b/packages/core/__mocks__/fs.mock.ts@@ -95,6 +95,16 @@ async function deleteFile(filename: string, _requestOptions: RequestOptions) {return true;}+async function bulkDeleteFiles(+ filenames: string[],+ _requestOptions: RequestOptions+) {+ for (const filename of filenames) {+ await deleteFile(filename, _requestOptions);+ }+ return true;+}+async function exists(filename) {return hasItem(filename);}@@ -109,6 +119,7 @@ export const FS: IFileStorage = {uploadFile: cancellable(uploadFile),downloadFile: cancellable(downloadFile),deleteFile,+ bulkDeleteFiles,exists,clearFileStorage,hashBase64diff --git a/packages/core/src/collections/attachments.ts b/packages/core/src/collections/attachments.tsindex 0f6a82e80..aafb6b7e3 100644--- a/packages/core/src/collections/attachments.ts+++ b/packages/core/src/collections/attachments.ts@@ -226,6 +226,44 @@ export class Attachments implements ICollection {return false;}+ async bulkRemove(attachments: Attachment[], localOnly: boolean) {+ logger.debug("Bulk removing attachments", {+ count: attachments.length,+ localOnly+ });+ if (attachments.length === 0) return;++ const detachable: Attachment[] = [];+ for (const attachment of attachments) {+ if (!localOnly && !(await this.canDetach(attachment))) continue;+ detachable.push(attachment);+ }++ const localOnlyHashes = detachable+ .filter((a) => localOnly || !a.dateUploaded)+ .map((a) => a.hash);+ const remoteHashes = detachable+ .filter((a) => !localOnly && !!a.dateUploaded)+ .map((a) => a.hash);++ if (localOnlyHashes.length > 0) {+ await this.db.fs().bulkDeleteFiles(localOnlyHashes, true);+ }+ if (remoteHashes.length > 0) {+ await this.db.fs().bulkDeleteFiles(remoteHashes, false);+ }++ if (!localOnly) {+ for (const attachment of detachable) {+ await this.detach(attachment);+ }+ }++ const ids = detachable.map((a) => a.id);+ await this.db.relations.unlinkOfType("attachment", ids);+ await this.collection.softDelete(ids);+ }+async detach(attachment: Attachment) {for (const note of await this.db.relations.to(attachment, "note")@@ -576,11 +614,8 @@ export class Attachments implements ICollection {}async removeOrphaned() {- for await (const attachment of this.db.attachments.orphaned.iterate()) {- try {- await this.db.attachments.remove(attachment.hash, false);- } catch (error) {}- }+ const orphaned = await this.db.attachments.orphaned.items();+ await this.bulkRemove(orphaned, false);}}diff --git a/packages/core/src/collections/trash.ts b/packages/core/src/collections/trash.tsindex 73915e302..30e4b2fb3 100644--- a/packages/core/src/collections/trash.ts+++ b/packages/core/src/collections/trash.ts@@ -30,6 +30,7 @@ import {import { sql } from "@streetwriters/kysely";import { MAX_SQL_PARAMETERS } from "../database/sql-collection.js";import { withSubNotebooks } from "./notebooks.js";+import { logger } from "../logger.js";export default class Trash {collections = ["notes", "notebooks"] as const;@@ -123,7 +124,14 @@ export default class Trash {);await this._delete(noteIds, notebookIds);- await this.db.attachments.removeOrphaned();+ this.db.attachments+ .removeOrphaned()+ .catch((e) =>+ logger.error(+ e,+ "Error removing orphaned attachments during trash cleanup"+ )+ );await this.buildCache();}diff --git a/packages/core/src/database/fs.ts b/packages/core/src/database/fs.tsindex 1ae799d52..48dbe353b 100644--- a/packages/core/src/database/fs.ts+++ b/packages/core/src/database/fs.ts@@ -292,6 +292,20 @@ export class FileStorage {});}+ async bulkDeleteFiles(filenames: string[], localOnly = false) {+ if (filenames.length === 0) return true;++ if (localOnly) return await this.fs.bulkDeleteFiles(filenames);++ const token = await this.tokenManager.getAccessToken();+ const url = `${hosts.API_HOST}/s3/bulk-delete`;+ return await this.fs.bulkDeleteFiles(filenames, {+ url,+ headers: { Authorization: `Bearer ${token}` },+ chunkSize: 0+ });+ }+exists(filename: string) {return this.fs.exists(filename);}diff --git a/packages/core/src/interfaces.ts b/packages/core/src/interfaces.tsindex eec44365f..7be148a66 100644--- a/packages/core/src/interfaces.ts+++ b/packages/core/src/interfaces.ts@@ -128,6 +128,10 @@ export interface IFileStorage {filename: string,requestOptions?: RequestOptions): Promise<boolean>;+ bulkDeleteFiles(+ filenames: string[],+ requestOptions?: RequestOptions+ ): Promise<boolean>;exists(filename: string): Promise<boolean>;bulkExists(filenames: string[]): Promise<string[]>;getUploadedFileSize(filename: string): Promise<number>;diff --git a/packages/streamable-fs/src/index.ts b/packages/streamable-fs/src/index.tsindex fc706673c..0fb60eaa2 100644--- a/packages/streamable-fs/src/index.ts+++ b/packages/streamable-fs/src/index.ts@@ -72,6 +72,13 @@ export class StreamableFS implements IStreamableFS {return true;}+ async bulkDeleteFiles(filenames: string[]): Promise<boolean> {+ for (const filename of filenames) {+ await this.deleteFile(filename);+ }+ return true;+ }+async moveFile(source: FileHandle, dest: FileHandle) {await source.readable.pipeTo(dest.writeable);await source.delete();diff --git a/packages/streamable-fs/src/interfaces.ts b/packages/streamable-fs/src/interfaces.tsindex 4cb71549a..c6a584e4c 100644--- a/packages/streamable-fs/src/interfaces.ts+++ b/packages/streamable-fs/src/interfaces.ts@@ -30,6 +30,7 @@ export interface IStreamableFS {readFile(filename: string): Promise<FileHandle | undefined>;exists(filename: string): Promise<boolean>;deleteFile(filename: string): Promise<boolean>;+ bulkDeleteFiles(filenames: string[]): Promise<boolean>;list(): Promise<string[]>;moveFile(source: FileHandle, dest: FileHandle): Promise<void>;clear(): Promise<void>;