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');
|
2024-02-17 22:58:54 +02:00
|
|
|
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)) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return [400, `Unknown image type: ${file.mimetype}`];
|
2018-02-11 00:18:59 -05:00
|
|
|
}
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
const originalName = `Sender image.${imageType(file.buffer).ext}`;
|
2019-07-10 23:01:30 +02:00
|
|
|
|
2023-01-08 20:40:38 +01: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);
|
2021-12-19 21:56:19 +01:00
|
|
|
|
|
|
|
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);
|
2021-12-19 21:56:19 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-11 00:18:59 -05:00
|
|
|
|
2022-02-05 16:23:30 +01:00
|
|
|
note.setLabel("sentFromSender");
|
|
|
|
|
2019-02-19 21:24:35 +01:00
|
|
|
return {
|
|
|
|
noteId: noteId
|
|
|
|
};
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
2018-02-11 00:18:59 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function saveNote(req) {
|
2023-01-08 20:40:38 +01:00
|
|
|
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'
|
|
|
|
});
|
2019-02-19 21:24:35 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:24:35 +01:00
|
|
|
return {
|
|
|
|
noteId: note.noteId,
|
|
|
|
branchId: branch.branchId
|
|
|
|
};
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
2018-02-11 10:54:56 -05:00
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
module.exports = {
|
|
|
|
uploadImage,
|
|
|
|
saveNote
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|