mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
web: compare uploaded file size with actual file size after upload
This commit is contained in:
@@ -116,12 +116,10 @@ export async function checkAttachment(hash: string) {
|
|||||||
if (!attachment) return { failed: "Attachment not found." };
|
if (!attachment) return { failed: "Attachment not found." };
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const size = await lazify(
|
await lazify(import("../interfaces/fs"), ({ checkUpload }) =>
|
||||||
import("../interfaces/fs"),
|
checkUpload(hash, attachment.chunkSize, attachment.size)
|
||||||
({ getUploadedFileSize }) => getUploadedFileSize(hash)
|
|
||||||
);
|
);
|
||||||
if (size === 0) throw new Error("File length is 0.");
|
await db.attachments.markAsFailed(attachment.id);
|
||||||
else if (size === -1) throw new Error("File verification check failed.");
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const reason = e instanceof Error ? e.message : "Unknown error.";
|
const reason = e instanceof Error ? e.message : "Unknown error.";
|
||||||
await db.attachments.markAsFailed(attachment.id, reason);
|
await db.attachments.markAsFailed(attachment.id, reason);
|
||||||
|
|||||||
@@ -256,7 +256,11 @@ async function uploadFile(
|
|||||||
: await multiPartUploadFile(fileHandle, filename, requestOptions);
|
: await multiPartUploadFile(fileHandle, filename, requestOptions);
|
||||||
|
|
||||||
if (uploaded) {
|
if (uploaded) {
|
||||||
await checkUpload(filename);
|
await checkUpload(
|
||||||
|
filename,
|
||||||
|
requestOptions.chunkSize,
|
||||||
|
fileHandle.file.size
|
||||||
|
);
|
||||||
await fileHandle.addAdditionalData("uploaded", true);
|
await fileHandle.addAdditionalData("uploaded", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -448,13 +452,21 @@ async function resetUpload(fileHandle: FileHandle) {
|
|||||||
await fileHandle.addAdditionalData("uploaded", false);
|
await fileHandle.addAdditionalData("uploaded", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkUpload(filename: string) {
|
export async function checkUpload(
|
||||||
|
filename: string,
|
||||||
|
chunkSize: number,
|
||||||
|
expectedSize: number
|
||||||
|
) {
|
||||||
const size = await getUploadedFileSize(filename);
|
const size = await getUploadedFileSize(filename);
|
||||||
|
const totalChunks = Math.ceil(size / chunkSize);
|
||||||
|
const decryptedLength = size - totalChunks * ABYTES;
|
||||||
const error =
|
const error =
|
||||||
size === 0
|
size === 0
|
||||||
? `Upload verification failed: file size is 0. Please upload this file again. (File hash: ${filename})`
|
? `File size is 0.`
|
||||||
: size === -1
|
: size === -1
|
||||||
? `Upload verification failed.`
|
? `File verification check failed.`
|
||||||
|
: expectedSize !== decryptedLength
|
||||||
|
? `File size mismatch. Expected ${size} bytes but got ${decryptedLength} bytes.`
|
||||||
: undefined;
|
: undefined;
|
||||||
if (error) throw new Error(error);
|
if (error) throw new Error(error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user