Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Convert routes/api/attachments

Elian Doran9330241045af
diff --git a/src/routes/api/attachments.js b/src/routes/api/attachments.ts
similarity index 77%
rename from src/routes/api/attachments.js
rename to src/routes/api/attachments.ts
index 4cb782cb2b..9fde39fc52 100644
--- a/src/routes/api/attachments.js
+++ b/src/routes/api/attachments.ts
@@ -1,27 +1,28 @@
-const becca = require('../../becca/becca');
-const blobService = require('../../services/blob');
-const ValidationError = require('../../errors/validation_error');
-const imageService = require("../../services/image");
+import becca = require('../../becca/becca');
+import blobService = require('../../services/blob');
+import ValidationError = require('../../errors/validation_error');
+import imageService = require("../../services/image");
+import { Request } from 'express';
-function getAttachmentBlob(req) {
+function getAttachmentBlob(req: Request) {
const preview = req.query.preview === 'true';
return blobService.getBlobPojo('attachments', req.params.attachmentId, { preview });
}
-function getAttachments(req) {
+function getAttachments(req: Request) {
const note = becca.getNoteOrThrow(req.params.noteId);
return note.getAttachments({includeContentLength: true});
}
-function getAttachment(req) {
+function getAttachment(req: Request) {
const {attachmentId} = req.params;
return becca.getAttachmentOrThrow(attachmentId, {includeContentLength: true});
}
-function getAllAttachments(req) {
+function getAllAttachments(req: Request) {
const {attachmentId} = req.params;
// one particular attachment is requested, but return all note's attachments
@@ -29,18 +30,18 @@ function getAllAttachments(req) {
return attachment.getNote()?.getAttachments({includeContentLength: true}) || [];
}
-function saveAttachment(req) {
+function saveAttachment(req: Request) {
const {noteId} = req.params;
const {attachmentId, role, mime, title, content} = req.body;
- const {matchBy} = req.query;
+ const {matchBy} = req.query as any;
const note = becca.getNoteOrThrow(noteId);
note.saveAttachment({attachmentId, role, mime, title, content}, matchBy);
}
-function uploadAttachment(req) {
+function uploadAttachment(req: Request) {
const {noteId} = req.params;
- const {file} = req;
+ const {file} = req as any;
const note = becca.getNoteOrThrow(noteId);
let url;
@@ -65,7 +66,7 @@ function uploadAttachment(req) {
};
}
-function renameAttachment(req) {
+function renameAttachment(req: Request) {
const {title} = req.body;
const {attachmentId} = req.params;
@@ -79,7 +80,7 @@ function renameAttachment(req) {
attachment.save();
}
-function deleteAttachment(req) {
+function deleteAttachment(req: Request) {
const {attachmentId} = req.params;
const attachment = becca.getAttachment(attachmentId);
@@ -89,14 +90,14 @@ function deleteAttachment(req) {
}
}
-function convertAttachmentToNote(req) {
+function convertAttachmentToNote(req: Request) {
const {attachmentId} = req.params;
const attachment = becca.getAttachmentOrThrow(attachmentId);
return attachment.convertToNote();
}
-module.exports = {
+export = {
getAttachmentBlob,
getAttachments,
getAttachment,
diff --git a/src/routes/routes.js b/src/routes/routes.js
index c782f5e9cf..269f2a6ae6 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -25,7 +25,7 @@ const indexRoute = require('./index.js');
const treeApiRoute = require('./api/tree.js');
const notesApiRoute = require('./api/notes.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 cloningApiRoute = require('./api/cloning');
const revisionsApiRoute = require('./api/revisions');
diff --git a/src/services/blob.ts b/src/services/blob.ts
index fac1adfadc..41d23adc54 100644
--- a/src/services/blob.ts
+++ b/src/services/blob.ts
@@ -4,7 +4,8 @@ import protectedSessionService = require('./protected_session');
import utils = require('./utils');
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);
if (!entity) {
throw new NotFoundError(`Entity ${entityName} '${entityId}' was not found.`);