mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
web: handle errors during uploaded file size check
This commit is contained in:
committed by
Abdullah Atta
parent
8e207d787d
commit
45a552adef
@@ -106,9 +106,12 @@ export async function checkAttachment(hash: string) {
|
|||||||
import("../interfaces/fs"),
|
import("../interfaces/fs"),
|
||||||
({ getUploadedFileSize }) => getUploadedFileSize(hash)
|
({ getUploadedFileSize }) => getUploadedFileSize(hash)
|
||||||
);
|
);
|
||||||
if (size <= 0) return { failed: "File length is 0." };
|
if (size === 0) throw new Error("File length is 0.");
|
||||||
|
else if (size === -1) throw new Error("File verification check failed.");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return { failed: e instanceof Error ? e.message : "Unknown error." };
|
const reason = e instanceof Error ? e.message : "Unknown error.";
|
||||||
|
await db.attachments.markAsFailed(attachment.id, reason);
|
||||||
|
return { failed: reason };
|
||||||
}
|
}
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -189,7 +189,7 @@ async function addAttachment(
|
|||||||
|
|
||||||
const exists = await db.attachments.attachment(hash);
|
const exists = await db.attachments.attachment(hash);
|
||||||
if (!forceWrite && exists) {
|
if (!forceWrite && exists) {
|
||||||
forceWrite = (await getUploadedFileSize(hash)) <= 0;
|
forceWrite = (await getUploadedFileSize(hash)) === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (forceWrite || !exists) {
|
if (forceWrite || !exists) {
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ import {
|
|||||||
Output,
|
Output,
|
||||||
RequestOptions
|
RequestOptions
|
||||||
} from "@notesnook/core/dist/interfaces";
|
} from "@notesnook/core/dist/interfaces";
|
||||||
|
import { logger } from "../utils/logger";
|
||||||
|
|
||||||
const ABYTES = 17;
|
const ABYTES = 17;
|
||||||
const CHUNK_SIZE = 512 * 1024;
|
const CHUNK_SIZE = 512 * 1024;
|
||||||
@@ -624,6 +625,13 @@ async function deleteFile(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `-1` means an error during file size
|
||||||
|
*
|
||||||
|
* `0` means file either doesn't exist or file is actually of 0 length
|
||||||
|
*
|
||||||
|
* `>0` means file is valid
|
||||||
|
*/
|
||||||
export async function getUploadedFileSize(filename: string) {
|
export async function getUploadedFileSize(filename: string) {
|
||||||
try {
|
try {
|
||||||
const url = `${hosts.API_HOST}/s3?name=${filename}`;
|
const url = `${hosts.API_HOST}/s3?name=${filename}`;
|
||||||
@@ -636,8 +644,8 @@ export async function getUploadedFileSize(filename: string) {
|
|||||||
const contentLength = parseInt(attachmentInfo.headers["content-length"]);
|
const contentLength = parseInt(attachmentInfo.headers["content-length"]);
|
||||||
return isNaN(contentLength) ? 0 : contentLength;
|
return isNaN(contentLength) ? 0 : contentLength;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logger.error(e, "Failed to get uploaded file size.", { filename });
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user