Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
server-ts: Convert routes/api/sender
Elian Doran90cf91308328
diff --git a/src/routes/api/sender.js b/src/routes/api/sender.jsdeleted file mode 100644index b2309d3c76..0000000000--- a/src/routes/api/sender.js+++ /dev/null@@ -1,66 +0,0 @@-"use strict";--const imageType = require('image-type');-const imageService = require('../../services/image');-const noteService = require('../../services/notes');-const { sanitizeAttributeName } = require('../../services/sanitize_attribute_name');-const specialNotesService = require('../../services/special_notes');--function uploadImage(req) {- const file = req.file;-- if (!["image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {- return [400, `Unknown image type: ${file.mimetype}`];- }-- const originalName = `Sender image.${imageType(file.buffer).ext}`;-- const parentNote = specialNotesService.getInboxNote(req.headers['x-local-date']);-- const { note, noteId } = imageService.saveImage(parentNote.noteId, file.buffer, originalName, true);-- const labelsStr = req.headers['x-labels'];-- if (labelsStr?.trim()) {- const labels = JSON.parse(labelsStr);-- for (const { name, value } of labels) {- note.setLabel(sanitizeAttributeName(name), value);- }- }-- note.setLabel("sentFromSender");-- return {- noteId: noteId- };-}--function saveNote(req) {- const parentNote = specialNotesService.getInboxNote(req.headers['x-local-date']);-- const { note, branch } = noteService.createNewNote({- parentNoteId: parentNote.noteId,- title: req.body.title,- content: req.body.content,- isProtected: false,- type: 'text',- mime: 'text/html'- });-- if (req.body.labels) {- for (const { name, value } of req.body.labels) {- note.setLabel(sanitizeAttributeName(name), value);- }- }-- return {- noteId: note.noteId,- branchId: branch.branchId- };-}--module.exports = {- uploadImage,- saveNote-};diff --git a/src/routes/api/sender.ts b/src/routes/api/sender.tsnew file mode 100644index 0000000000..dc31c42f60--- /dev/null+++ b/src/routes/api/sender.ts@@ -0,0 +1,83 @@+"use strict";++import imageType = require('image-type');+import imageService = require('../../services/image');+import noteService = require('../../services/notes');+import sanitize_attribute_name = require('../../services/sanitize_attribute_name');+import specialNotesService = require('../../services/special_notes');+import { Request } from 'express';++function uploadImage(req: Request) {+ const file = (req as any).file;++ if (!["image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {+ return [400, `Unknown image type: ${file.mimetype}`];+ }++ const uploadedImageType = imageType(file.buffer);+ if (!uploadedImageType) {+ return [400, "Unable to determine image type."];+ }+ const originalName = `Sender image.${uploadedImageType.ext}`;++ if (!req.headers["x-local-date"] || Array.isArray(req.headers["x-local-date"])) {+ return [400, "Invalid local date"];+ }++ if (Array.isArray(req.headers["x-labels"])) {+ return [400, "Invalid value type."];+ }++ const parentNote = specialNotesService.getInboxNote(req.headers['x-local-date']);++ const { note, noteId } = imageService.saveImage(parentNote.noteId, file.buffer, originalName, true);++ const labelsStr = req.headers['x-labels'];++ if (labelsStr?.trim()) {+ const labels = JSON.parse(labelsStr);++ for (const { name, value } of labels) {+ note.setLabel(sanitize_attribute_name.sanitizeAttributeName(name), value);+ }+ }++ note.setLabel("sentFromSender");++ return {+ noteId: noteId+ };+}++function saveNote(req: Request) {+ if (!req.headers["x-local-date"] || Array.isArray(req.headers["x-local-date"])) {+ return [400, "Invalid local date"];+ }++ const parentNote = specialNotesService.getInboxNote(req.headers['x-local-date']);++ const { note, branch } = noteService.createNewNote({+ parentNoteId: parentNote.noteId,+ title: req.body.title,+ content: req.body.content,+ isProtected: false,+ type: 'text',+ mime: 'text/html'+ });++ if (req.body.labels) {+ for (const { name, value } of req.body.labels) {+ note.setLabel(sanitize_attribute_name.sanitizeAttributeName(name), value);+ }+ }++ return {+ noteId: note.noteId,+ branchId: branch.branchId+ };+}++export = {+ uploadImage,+ saveNote+};diff --git a/src/routes/routes.js b/src/routes/routes.jsindex 3285b5b979..a88dccd80a 100644--- a/src/routes/routes.js+++ b/src/routes/routes.js@@ -44,7 +44,7 @@ const databaseRoute = require('./api/database');const imageRoute = require('./api/image');const attributesRoute = require('./api/attributes');const scriptRoute = require('./api/script');-const senderRoute = require('./api/sender.js');+const senderRoute = require('./api/sender');const filesRoute = require('./api/files');const searchRoute = require('./api/search');const bulkActionRoute = require('./api/bulk_action');