pasted images should have trimmed names if too long (too often it's just garbage), closes #2307

This commit is contained in:
zadam 2021-11-04 21:48:46 +01:00
parent b02a5b872a
commit e57dee2f14
3 changed files with 17 additions and 5 deletions

View File

@ -38,7 +38,7 @@ function uploadImage(req) {
return [400, "Unknown image type: " + file.mimetype];
}
const {url} = imageService.saveImage(noteId, file.buffer, file.originalname, true);
const {url} = imageService.saveImage(noteId, file.buffer, file.originalname, true, true);
return {
uploaded: true,

View File

@ -73,11 +73,15 @@ function updateImage(noteId, uploadBuffer, originalName) {
});
}
function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSwitch) {
function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSwitch, trimFilename = false) {
log.info(`Saving image ${originalName}`);
const fileName = sanitizeFilename(originalName);
if (trimFilename && originalName.length > 40) {
// https://github.com/zadam/trilium/issues/2307
originalName = "image";
}
const fileName = sanitizeFilename(originalName);
const parentNote = becca.getNote(parentNoteId);
const {note} = noteService.createNewNote({
@ -95,6 +99,14 @@ function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSwitch)
processImage(uploadBuffer, originalName, shrinkImageSwitch).then(({buffer, imageFormat}) => {
sql.transactional(() => {
note.mime = getImageMimeFromExtension(imageFormat.ext);
if (!originalName.includes(".")) {
originalName += "." + imageFormat.ext;
note.setLabel('originalFileName', originalName);
note.title = sanitizeFilename(originalName);
}
note.save();
note.setContent(buffer);

View File

@ -270,7 +270,7 @@ async function downloadImage(noteId, imageUrl) {
const title = path.basename(parsedUrl.pathname);
const imageService = require('../services/image');
const {note} = imageService.saveImage(noteId, imageBuffer, title, true);
const {note} = imageService.saveImage(noteId, imageBuffer, title, true, true);
note.addLabel('imageUrl', imageUrl);
@ -305,7 +305,7 @@ function downloadImages(noteId, content) {
const imageBuffer = Buffer.from(imageBase64, 'base64');
const imageService = require('../services/image');
const {note} = imageService.saveImage(noteId, imageBuffer, "inline image", true);
const {note} = imageService.saveImage(noteId, imageBuffer, "inline image", true, true);
content = content.substr(0, imageMatch.index)
+ `<img src="api/images/${note.noteId}/${note.title}"`