Notes/src/routes/api/sender.js

54 lines
1.4 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');
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
}
2019-07-10 23:01:30 +02:00
const originalName = "Sender image." + imageType(file.buffer).ext;
2020-06-20 12:31:38 +02:00
const parentNote = dateNoteService.getDateNote(req.headers['x-local-date']);
2018-02-11 00:18:59 -05:00
2020-06-20 12:31:38 +02:00
const {noteId} = imageService.saveImage(parentNote.noteId, file.buffer, originalName, true);
2018-02-11 00:18:59 -05:00
return {
noteId: noteId
};
}
2018-02-11 00:18:59 -05:00
2020-06-20 12:31:38 +02:00
function saveNote(req) {
const parentNote = dateNoteService.getDateNote(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) {
note.setLabel(name, value);
}
}
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
};