Files
notesnook/apps/web/src/common/attachments.ts

116 lines
3.3 KiB
TypeScript
Raw Normal View History

/*
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
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
2023-11-18 10:17:23 +05:00
import { lazify } from "../utils/lazify";
2023-08-21 16:24:17 +05:00
import { Attachment } from "@notesnook/core";
import { db } from "./db";
async function download(hash: string, groupId?: string) {
2023-08-21 16:24:17 +05:00
const attachment = db.attachments.attachment(hash);
if (!attachment) return;
2023-08-21 16:24:17 +05:00
const downloadResult = await db
.fs()
.downloadFile(
groupId || attachment.metadata.hash,
attachment.metadata.hash,
attachment.chunkSize,
attachment.metadata
);
if (!downloadResult) throw new Error("Failed to download file.");
2023-08-21 16:24:17 +05:00
const key = await db.attachments.decryptKey(attachment.key);
if (!key) throw new Error("Invalid key for attachment.");
2023-05-27 15:06:43 +05:00
return { key, attachment };
}
export async function saveAttachment(hash: string) {
const response = await download(hash);
if (!response) return;
const { attachment, key } = response;
2023-11-18 10:17:23 +05:00
await lazify(import("../interfaces/fs"), ({ default: FS }) =>
FS.saveFile(attachment.metadata.hash, {
key,
iv: attachment.iv,
name: attachment.metadata.filename,
type: attachment.metadata.type,
isUploaded: !!attachment.dateUploaded
})
);
}
2023-05-27 15:06:43 +05:00
type OutputTypeToReturnType = {
blob: Blob;
base64: string;
text: string;
};
export async function downloadAttachment<
TType extends "blob" | "base64" | "text",
TOutputType = OutputTypeToReturnType[TType]
>(
hash: string,
type: TType,
groupId?: string
): Promise<TOutputType | undefined> {
const response = await download(hash, groupId);
2023-05-27 15:06:43 +05:00
if (!response) return;
const { attachment, key } = response;
2023-05-27 15:06:43 +05:00
if (type === "base64" || type === "text")
2023-08-21 16:24:17 +05:00
return (await db.attachments.read(hash, type)) as TOutputType;
2023-11-18 10:17:23 +05:00
const blob = await lazify(import("../interfaces/fs"), ({ default: FS }) =>
FS.decryptFile(attachment.metadata.hash, {
key,
iv: attachment.iv,
name: attachment.metadata.filename,
type: attachment.metadata.type,
isUploaded: !!attachment.dateUploaded
})
);
2023-05-27 15:06:43 +05:00
if (!blob) return;
return blob as TOutputType;
}
2023-05-27 15:06:43 +05:00
export async function checkAttachment(hash: string) {
2023-08-21 16:24:17 +05:00
const attachment = db.attachments.attachment(hash);
if (!attachment) return { failed: "Attachment not found." };
try {
2023-11-18 10:17:23 +05:00
const size = await lazify(import("../interfaces/fs"), ({ default: FS }) =>
FS.getUploadedFileSize(hash)
);
if (size <= 0) return { failed: "File length is 0." };
} catch (e) {
2023-05-27 15:06:43 +05:00
return { failed: e instanceof Error ? e.message : "Unknown error." };
}
return { success: true };
}
const ABYTES = 17;
2023-08-21 16:24:17 +05:00
export function getTotalSize(attachments: Attachment[]) {
let size = 0;
2023-05-27 15:06:43 +05:00
for (const attachment of attachments) {
size += attachment.length + ABYTES;
}
return size;
}