server-ts: Convert routes/api/attachments

This commit is contained in:
Elian Doran 2024-04-05 20:16:46 +03:00
parent b13ad5d01e
commit 9330241045
No known key found for this signature in database
3 changed files with 20 additions and 18 deletions

View File

@ -1,27 +1,28 @@
const becca = require('../../becca/becca'); import becca = require('../../becca/becca');
const blobService = require('../../services/blob'); import blobService = require('../../services/blob');
const ValidationError = require('../../errors/validation_error'); import ValidationError = require('../../errors/validation_error');
const imageService = require("../../services/image"); import imageService = require("../../services/image");
import { Request } from 'express';
function getAttachmentBlob(req) { function getAttachmentBlob(req: Request) {
const preview = req.query.preview === 'true'; const preview = req.query.preview === 'true';
return blobService.getBlobPojo('attachments', req.params.attachmentId, { preview }); return blobService.getBlobPojo('attachments', req.params.attachmentId, { preview });
} }
function getAttachments(req) { function getAttachments(req: Request) {
const note = becca.getNoteOrThrow(req.params.noteId); const note = becca.getNoteOrThrow(req.params.noteId);
return note.getAttachments({includeContentLength: true}); return note.getAttachments({includeContentLength: true});
} }
function getAttachment(req) { function getAttachment(req: Request) {
const {attachmentId} = req.params; const {attachmentId} = req.params;
return becca.getAttachmentOrThrow(attachmentId, {includeContentLength: true}); return becca.getAttachmentOrThrow(attachmentId, {includeContentLength: true});
} }
function getAllAttachments(req) { function getAllAttachments(req: Request) {
const {attachmentId} = req.params; const {attachmentId} = req.params;
// one particular attachment is requested, but return all note's attachments // one particular attachment is requested, but return all note's attachments
@ -29,18 +30,18 @@ function getAllAttachments(req) {
return attachment.getNote()?.getAttachments({includeContentLength: true}) || []; return attachment.getNote()?.getAttachments({includeContentLength: true}) || [];
} }
function saveAttachment(req) { function saveAttachment(req: Request) {
const {noteId} = req.params; const {noteId} = req.params;
const {attachmentId, role, mime, title, content} = req.body; const {attachmentId, role, mime, title, content} = req.body;
const {matchBy} = req.query; const {matchBy} = req.query as any;
const note = becca.getNoteOrThrow(noteId); const note = becca.getNoteOrThrow(noteId);
note.saveAttachment({attachmentId, role, mime, title, content}, matchBy); note.saveAttachment({attachmentId, role, mime, title, content}, matchBy);
} }
function uploadAttachment(req) { function uploadAttachment(req: Request) {
const {noteId} = req.params; const {noteId} = req.params;
const {file} = req; const {file} = req as any;
const note = becca.getNoteOrThrow(noteId); const note = becca.getNoteOrThrow(noteId);
let url; let url;
@ -65,7 +66,7 @@ function uploadAttachment(req) {
}; };
} }
function renameAttachment(req) { function renameAttachment(req: Request) {
const {title} = req.body; const {title} = req.body;
const {attachmentId} = req.params; const {attachmentId} = req.params;
@ -79,7 +80,7 @@ function renameAttachment(req) {
attachment.save(); attachment.save();
} }
function deleteAttachment(req) { function deleteAttachment(req: Request) {
const {attachmentId} = req.params; const {attachmentId} = req.params;
const attachment = becca.getAttachment(attachmentId); const attachment = becca.getAttachment(attachmentId);
@ -89,14 +90,14 @@ function deleteAttachment(req) {
} }
} }
function convertAttachmentToNote(req) { function convertAttachmentToNote(req: Request) {
const {attachmentId} = req.params; const {attachmentId} = req.params;
const attachment = becca.getAttachmentOrThrow(attachmentId); const attachment = becca.getAttachmentOrThrow(attachmentId);
return attachment.convertToNote(); return attachment.convertToNote();
} }
module.exports = { export = {
getAttachmentBlob, getAttachmentBlob,
getAttachments, getAttachments,
getAttachment, getAttachment,

View File

@ -25,7 +25,7 @@ const indexRoute = require('./index.js');
const treeApiRoute = require('./api/tree.js'); const treeApiRoute = require('./api/tree.js');
const notesApiRoute = require('./api/notes.js'); const notesApiRoute = require('./api/notes.js');
const branchesApiRoute = require('./api/branches.js'); const branchesApiRoute = require('./api/branches.js');
const attachmentsApiRoute = require('./api/attachments.js'); const attachmentsApiRoute = require('./api/attachments');
const autocompleteApiRoute = require('./api/autocomplete.js'); const autocompleteApiRoute = require('./api/autocomplete.js');
const cloningApiRoute = require('./api/cloning'); const cloningApiRoute = require('./api/cloning');
const revisionsApiRoute = require('./api/revisions'); const revisionsApiRoute = require('./api/revisions');

View File

@ -4,7 +4,8 @@ import protectedSessionService = require('./protected_session');
import utils = require('./utils'); import utils = require('./utils');
import type { Blob } from "./blob-interface"; import type { Blob } from "./blob-interface";
function getBlobPojo(entityName: string, entityId: string) { function getBlobPojo(entityName: string, entityId: string, opts?: { preview: boolean }) {
// TODO: Unused opts.
const entity = becca.getEntity(entityName, entityId); const entity = becca.getEntity(entityName, entityId);
if (!entity) { if (!entity) {
throw new NotFoundError(`Entity ${entityName} '${entityId}' was not found.`); throw new NotFoundError(`Entity ${entityName} '${entityId}' was not found.`);