Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: check if attachment really exists on insertion

Abdullah Atta95b9edca62b2
diff --git a/apps/web/src/components/editor/picker.ts b/apps/web/src/components/editor/picker.ts
index 169d14e66..c7a32ebe4 100644
--- a/apps/web/src/components/editor/picker.ts
+++ b/apps/web/src/components/editor/picker.ts
@@ -25,7 +25,6 @@ import { TaskManager } from "../../common/task-manager";
import { isUserPremium } from "../../hooks/use-is-user-premium";
import { showToast } from "../../utils/toast";
import { showFilePicker } from "../../utils/file-picker";
-import { lazify } from "../../utils/lazify";
const FILE_SIZE_LIMIT = 500 * 1024 * 1024;
const IMAGE_SIZE_LIMIT = 50 * 1024 * 1024;
@@ -151,29 +150,29 @@ async function addAttachment(
dataurl: string | undefined,
options: AddAttachmentOptions = {}
): Promise<Attachment> {
- const { expectedFileHash, forceWrite, showProgress = true } = options;
+ const { default: FS } = await import("../../interfaces/fs");
+ const { expectedFileHash, showProgress = true } = options;
+ let forceWrite = options.forceWrite;
const action = async () => {
const reader = file.stream().getReader();
-
- const { hash, type: hashType } = await lazify(
- import("../../interfaces/fs"),
- ({ default: FS }) => FS.hashStream(reader)
- );
+ const { hash, type: hashType } = await FS.hashStream(reader);
reader.releaseLock();
if (expectedFileHash && hash !== expectedFileHash)
throw new Error(
`Please select the same file for reuploading. Expected hash ${expectedFileHash} but got ${hash}.`
);
+
const exists = db.attachments?.exists(hash);
+ if (!forceWrite && exists) {
+ forceWrite = (await FS.getUploadedFileSize(hash)) <= 0;
+ }
+
if (forceWrite || !exists) {
const key: SerializedKey = await getEncryptionKey();
- const output = await lazify(
- import("../../interfaces/fs"),
- ({ default: FS }) => FS.writeEncryptedFile(file, key, hash)
- );
+ const output = await FS.writeEncryptedFile(file, key, hash);
if (!output) throw new Error("Could not encrypt file.");
if (forceWrite && exists) await db.attachments?.reset(hash);