Notes/src/routes/api/sender.js

67 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');
2024-02-20 23:29:03 +02:00
const imageService = require('../../services/image');
const noteService = require('../../services/notes');
2024-04-03 23:05:06 +03:00
const { sanitizeAttributeName } = require('../../services/sanitize_attribute_name');
const specialNotesService = require('../../services/special_notes');
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
const parentNote = specialNotesService.getInboxNote(req.headers['x-local-date']);
2018-02-11 00:18:59 -05:00
2024-04-03 23:05:06 +03: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);
2024-04-03 23:05:06 +03:00
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) {
const parentNote = specialNotesService.getInboxNote(req.headers['x-local-date']);
2018-02-11 10:54:56 -05:00
2024-04-03 23:05:06 +03: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) {
2024-04-03 23:05:06 +03:00
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
};