Notes/src/routes/api/sender.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-02-11 00:18:59 -05:00
"use strict";
2019-07-10 23:01:30 +02:00
const imageType = require('image-type');
const imageService = require('../../services/image');
const dateNoteService = require('../../services/date_notes');
const noteService = require('../../services/notes');
2020-11-17 22:35:20 +01:00
const attributeService = require('../../services/attributes');
2022-12-23 14:18:40 +01:00
const {sanitizeAttributeName} = require("../../services/sanitize_attribute_name.js");
2018-02-11 00:18:59 -05:00
2020-06-20 12:31:38 +02:00
function uploadImage(req) {
2018-02-11 00:18:59 -05:00
const file = req.file;
2020-06-28 23:10:45 +02:00
if (!["image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
return [400, `Unknown image type: ${file.mimetype}`];
2018-02-11 00:18:59 -05:00
}
const originalName = `Sender image.${imageType(file.buffer).ext}`;
2019-07-10 23:01:30 +02:00
2022-01-10 17:09:20 +01:00
const parentNote = dateNoteService.getDayNote(req.headers['x-local-date']);
2018-02-11 00:18:59 -05:00
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) {
2022-12-23 14:18:40 +01:00
note.setLabel(sanitizeAttributeName(name), value);
}
}
2018-02-11 00:18:59 -05:00
note.setLabel("sentFromSender");
return {
noteId: noteId
};
}
2018-02-11 00:18:59 -05:00
2020-06-20 12:31:38 +02:00
function saveNote(req) {
2022-01-10 17:09:20 +01:00
const parentNote = dateNoteService.getDayNote(req.headers['x-local-date']);
2018-02-11 10:54:56 -05:00
2020-06-20 12:31:38 +02:00
const {note, branch} = noteService.createNewNote({
2019-11-16 11:09:52 +01:00
parentNoteId: parentNote.noteId,
2018-02-11 10:54:56 -05:00
title: req.body.title,
content: req.body.content,
isProtected: false,
type: 'text',
mime: 'text/html'
});
2020-10-16 19:43:20 +02:00
if (req.body.labels) {
for (const {name, value} of req.body.labels) {
2022-12-23 14:18:40 +01:00
note.setLabel(sanitizeAttributeName(name), value);
2020-10-16 19:43:20 +02:00
}
}
return {
noteId: note.noteId,
branchId: branch.branchId
};
}
2018-02-11 10:54:56 -05:00
module.exports = {
uploadImage,
saveNote
2020-06-20 12:31:38 +02:00
};