2024-07-18 21:35:17 +03:00
|
|
|
import becca from "../../becca/becca.js";
|
|
|
|
import blobService from "../../services/blob.js";
|
|
|
|
import ValidationError from "../../errors/validation_error.js";
|
|
|
|
import imageService from "../../services/image.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { Request } from "express";
|
2023-05-05 16:37:39 +02:00
|
|
|
|
2024-04-05 20:16:46 +03:00
|
|
|
function getAttachmentBlob(req: Request) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const preview = req.query.preview === "true";
|
2023-05-05 16:37:39 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return blobService.getBlobPojo("attachments", req.params.attachmentId, { preview });
|
2023-05-05 16:37:39 +02:00
|
|
|
}
|
2023-04-01 23:55:04 +02:00
|
|
|
|
2024-04-05 20:16:46 +03:00
|
|
|
function getAttachments(req: Request) {
|
2023-05-08 00:02:08 +02:00
|
|
|
const note = becca.getNoteOrThrow(req.params.noteId);
|
2023-04-01 23:55:04 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return note.getAttachments({ includeContentLength: true });
|
2023-04-01 23:55:04 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 20:16:46 +03:00
|
|
|
function getAttachment(req: Request) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const { attachmentId } = req.params;
|
2023-04-01 23:55:04 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return becca.getAttachmentOrThrow(attachmentId, { includeContentLength: true });
|
2023-04-01 23:55:04 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 20:16:46 +03:00
|
|
|
function getAllAttachments(req: Request) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const { attachmentId } = req.params;
|
2023-05-29 00:19:54 +02:00
|
|
|
// one particular attachment is requested, but return all note's attachments
|
|
|
|
|
|
|
|
const attachment = becca.getAttachmentOrThrow(attachmentId);
|
2025-01-09 18:07:02 +02:00
|
|
|
return attachment.getNote()?.getAttachments({ includeContentLength: true }) || [];
|
2023-05-29 00:19:54 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 20:16:46 +03:00
|
|
|
function saveAttachment(req: Request) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const { noteId } = req.params;
|
|
|
|
const { attachmentId, role, mime, title, content } = req.body;
|
|
|
|
const { matchBy } = req.query as any;
|
2023-04-01 23:55:04 +02:00
|
|
|
|
2023-05-08 00:02:08 +02:00
|
|
|
const note = becca.getNoteOrThrow(noteId);
|
2025-01-09 18:07:02 +02:00
|
|
|
note.saveAttachment({ attachmentId, role, mime, title, content }, matchBy);
|
2023-04-01 23:55:04 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 20:16:46 +03:00
|
|
|
function uploadAttachment(req: Request) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const { noteId } = req.params;
|
|
|
|
const { file } = req as any;
|
2023-06-06 12:31:38 +02:00
|
|
|
|
|
|
|
const note = becca.getNoteOrThrow(noteId);
|
2023-06-15 01:26:38 +02:00
|
|
|
let url;
|
|
|
|
|
|
|
|
if (["image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
|
|
|
|
const attachment = imageService.saveImageToAttachment(noteId, file.buffer, file.originalname, true, true);
|
|
|
|
url = `api/attachments/${attachment.attachmentId}/image/${encodeURIComponent(attachment.title)}`;
|
|
|
|
} else {
|
|
|
|
const attachment = note.saveAttachment({
|
2025-01-09 18:07:02 +02:00
|
|
|
role: "file",
|
2023-06-15 01:26:38 +02:00
|
|
|
mime: file.mimetype,
|
|
|
|
title: file.originalname,
|
|
|
|
content: file.buffer
|
|
|
|
});
|
|
|
|
|
2023-07-13 23:54:47 +02:00
|
|
|
url = `#root/${noteId}?viewMode=attachments&attachmentId=${attachment.attachmentId}`;
|
2023-06-15 01:26:38 +02:00
|
|
|
}
|
2023-06-06 12:31:38 +02:00
|
|
|
|
|
|
|
return {
|
2023-06-15 01:26:38 +02:00
|
|
|
uploaded: true,
|
|
|
|
url
|
2023-06-06 12:31:38 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:16:46 +03:00
|
|
|
function renameAttachment(req: Request) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const { title } = req.body;
|
|
|
|
const { attachmentId } = req.params;
|
2023-06-14 00:28:59 +02:00
|
|
|
|
|
|
|
const attachment = becca.getAttachmentOrThrow(attachmentId);
|
|
|
|
|
|
|
|
if (!title?.trim()) {
|
|
|
|
throw new ValidationError("Title must not be empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
attachment.title = title;
|
|
|
|
attachment.save();
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:16:46 +03:00
|
|
|
function deleteAttachment(req: Request) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const { attachmentId } = req.params;
|
2023-04-01 23:55:04 +02:00
|
|
|
|
2023-04-14 16:49:06 +02:00
|
|
|
const attachment = becca.getAttachment(attachmentId);
|
2023-04-01 23:55:04 +02:00
|
|
|
|
|
|
|
if (attachment) {
|
|
|
|
attachment.markAsDeleted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:16:46 +03:00
|
|
|
function convertAttachmentToNote(req: Request) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const { attachmentId } = req.params;
|
2023-04-01 23:55:04 +02:00
|
|
|
|
2023-05-08 00:02:08 +02:00
|
|
|
const attachment = becca.getAttachmentOrThrow(attachmentId);
|
2023-04-14 16:49:06 +02:00
|
|
|
return attachment.convertToNote();
|
2023-04-01 23:55:04 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2023-05-05 16:37:39 +02:00
|
|
|
getAttachmentBlob,
|
2023-04-01 23:55:04 +02:00
|
|
|
getAttachments,
|
|
|
|
getAttachment,
|
2023-05-29 00:19:54 +02:00
|
|
|
getAllAttachments,
|
2023-04-01 23:55:04 +02:00
|
|
|
saveAttachment,
|
2023-06-06 12:31:38 +02:00
|
|
|
uploadAttachment,
|
2023-06-14 00:28:59 +02:00
|
|
|
renameAttachment,
|
2023-04-01 23:55:04 +02:00
|
|
|
deleteAttachment,
|
|
|
|
convertAttachmentToNote
|
|
|
|
};
|