mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
server-ts: Convert routes/api/sender
This commit is contained in:
parent
fa82158e30
commit
90cf913083
@ -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
|
||||
};
|
83
src/routes/api/sender.ts
Normal file
83
src/routes/api/sender.ts
Normal file
@ -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
|
||||
};
|
@ -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');
|
||||
|
Loading…
x
Reference in New Issue
Block a user