mirror of https://github.com/TriliumNext/Notes
Merge pull request #43 from TriliumNext/feature/typescript_backend_7
Convert backend to TypeScript (71% -> 80%)pull/65/head
commit
98d12901a5
@ -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
|
||||
};
|
||||
@ -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
|
||||
};
|
||||
@ -1,16 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
const similarityService = require('../../becca/similarity');
|
||||
const becca = require('../../becca/becca');
|
||||
|
||||
async function getSimilarNotes(req) {
|
||||
const noteId = req.params.noteId;
|
||||
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
return await similarityService.findSimilarNotes(noteId);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getSimilarNotes
|
||||
};
|
||||
@ -0,0 +1,18 @@
|
||||
"use strict";
|
||||
|
||||
import { Request } from "express";
|
||||
|
||||
import similarityService = require('../../becca/similarity');
|
||||
import becca = require('../../becca/becca');
|
||||
|
||||
async function getSimilarNotes(req: Request) {
|
||||
const noteId = req.params.noteId;
|
||||
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
return await similarityService.findSimilarNotes(noteId);
|
||||
}
|
||||
|
||||
export = {
|
||||
getSimilarNotes
|
||||
};
|
||||
Loading…
Reference in New Issue