mirror of
https://github.com/zadam/trilium.git
synced 2025-11-08 06:15:48 +01:00
uploading image to attachment
This commit is contained in:
@@ -12,6 +12,8 @@ const sanitizeFilename = require('sanitize-filename');
|
||||
const isSvg = require('is-svg');
|
||||
const isAnimated = require('is-animated');
|
||||
const htmlSanitizer = require("./html_sanitizer");
|
||||
const {attach} = require("jsdom/lib/jsdom/living/helpers/svg/basic-types.js");
|
||||
const NotFoundError = require("../errors/not_found_error.js");
|
||||
|
||||
async function processImage(uploadBuffer, originalName, shrinkImageSwitch) {
|
||||
const compressImages = optionService.getOptionBool("compressImages");
|
||||
@@ -119,9 +121,7 @@ function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSwitch,
|
||||
note.title = sanitizeFilename(originalName);
|
||||
}
|
||||
|
||||
note.save();
|
||||
|
||||
note.setContent(buffer);
|
||||
note.setContent(buffer, { forceSave: true });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -133,6 +133,47 @@ function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSwitch,
|
||||
};
|
||||
}
|
||||
|
||||
function saveImageToAttachment(noteId, uploadBuffer, originalName, shrinkImageSwitch, trimFilename = false) {
|
||||
log.info(`Saving image '${originalName}' as attachment into note '${noteId}'`);
|
||||
|
||||
if (trimFilename && originalName.length > 40) {
|
||||
// https://github.com/zadam/trilium/issues/2307
|
||||
originalName = "image";
|
||||
}
|
||||
|
||||
const fileName = sanitizeFilename(originalName);
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Could not find note '${noteId}'`);
|
||||
}
|
||||
|
||||
const attachment = note.saveAttachment({
|
||||
role: 'image',
|
||||
mime: 'unknown',
|
||||
title: fileName
|
||||
});
|
||||
|
||||
// resizing images asynchronously since JIMP does not support sync operation
|
||||
processImage(uploadBuffer, originalName, shrinkImageSwitch).then(({buffer, imageFormat}) => {
|
||||
sql.transactional(() => {
|
||||
attachment.mime = getImageMimeFromExtension(imageFormat.ext);
|
||||
|
||||
if (!originalName.includes(".")) {
|
||||
originalName += `.${imageFormat.ext}`;
|
||||
attachment.title = sanitizeFilename(originalName);
|
||||
}
|
||||
|
||||
attachment.setContent(buffer, { forceSave: true });
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
attachment,
|
||||
url: `api/notes/${note.noteId}/images/${attachment.attachmentId}/${encodeURIComponent(fileName)}`
|
||||
};
|
||||
}
|
||||
|
||||
async function shrinkImage(buffer, originalName) {
|
||||
let jpegQuality = optionService.getOptionInt('imageJpegQuality');
|
||||
|
||||
@@ -187,5 +228,6 @@ async function resize(buffer, quality) {
|
||||
|
||||
module.exports = {
|
||||
saveImage,
|
||||
saveImageToAttachment,
|
||||
updateImage
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user