2022-08-30 16:13:11 +05:00
|
|
|
/* This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2022 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-30 13:59:11 +05:00
|
|
|
import FS from "../interfaces/fs";
|
|
|
|
|
import { db } from "./db";
|
|
|
|
|
|
|
|
|
|
export async function downloadAttachment(hash) {
|
|
|
|
|
const attachment = db.attachments.attachment(hash);
|
|
|
|
|
if (!attachment) return;
|
|
|
|
|
const downloadResult = await db.fs.downloadFile(
|
|
|
|
|
attachment.metadata.hash,
|
|
|
|
|
attachment.metadata.hash,
|
2021-11-18 15:17:50 +05:00
|
|
|
attachment.chunkSize,
|
|
|
|
|
attachment.metadata
|
2021-10-30 13:59:11 +05:00
|
|
|
);
|
|
|
|
|
if (!downloadResult) throw new Error("Failed to download file.");
|
|
|
|
|
|
|
|
|
|
const key = await db.attachments.decryptKey(attachment.key);
|
|
|
|
|
if (!key) throw new Error("Invalid key for attachment.");
|
|
|
|
|
|
|
|
|
|
await FS.saveFile(attachment.metadata.hash, {
|
|
|
|
|
key,
|
|
|
|
|
iv: attachment.iv,
|
|
|
|
|
name: attachment.metadata.filename,
|
|
|
|
|
type: attachment.metadata.type,
|
2022-08-26 16:19:39 +05:00
|
|
|
isUploaded: !!attachment.dateUploaded
|
2021-10-30 13:59:11 +05:00
|
|
|
});
|
|
|
|
|
}
|
2022-03-01 07:08:50 +05:00
|
|
|
|
|
|
|
|
export async function checkAttachment(hash) {
|
|
|
|
|
const attachment = db.attachments.attachment(hash);
|
|
|
|
|
if (!attachment) return { failed: "Attachment not found." };
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const size = await FS.getUploadedFileSize(hash);
|
|
|
|
|
if (size <= 0) return { failed: "File length is 0." };
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return { failed: e.message };
|
|
|
|
|
}
|
|
|
|
|
return { success: true };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ABYTES = 17;
|
|
|
|
|
export function getTotalSize(attachments) {
|
|
|
|
|
let size = 0;
|
|
|
|
|
for (let attachment of attachments) {
|
|
|
|
|
size += attachment.length + ABYTES;
|
|
|
|
|
}
|
|
|
|
|
return size;
|
|
|
|
|
}
|