unified file and image upload WIP

pull/255/head
zadam 2023-06-15 01:26:38 +07:00
parent 75c6afd1c3
commit 3ff5fe61b2
8 changed files with 23 additions and 37 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -89,7 +89,7 @@ const TPL = `
<div style="flex: 1 1;"></div> <div style="flex: 1 1;"></div>
</div> </div>
<div class="attachment-deletion-warning alert alert-info"></div> <div class="attachment-deletion-warning alert alert-info" style="margin-top: 15px;"></div>
<div class="attachment-content-wrapper"></div> <div class="attachment-content-wrapper"></div>
</div> </div>

@ -267,7 +267,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
} }
} }
else { else {
this.watchdog.editor.execute('referenceLink', { notePath: notePath }); this.watchdog.editor.execute('referenceLink', { href: '#' + notePath });
} }
this.watchdog.editor.editing.view.focus(); this.watchdog.editor.editing.view.focus();

@ -1,6 +1,7 @@
const becca = require("../../becca/becca"); const becca = require("../../becca/becca");
const blobService = require("../../services/blob"); const blobService = require("../../services/blob");
const ValidationError = require("../../errors/validation_error"); const ValidationError = require("../../errors/validation_error");
const imageService = require("../../services/image.js");
function getAttachmentBlob(req) { function getAttachmentBlob(req) {
const preview = req.query.preview === 'true'; const preview = req.query.preview === 'true';
@ -41,16 +42,25 @@ function uploadAttachment(req) {
const {file} = req; const {file} = req;
const note = becca.getNoteOrThrow(noteId); const note = becca.getNoteOrThrow(noteId);
let url;
note.saveAttachment({
role: 'file', if (["image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
mime: file.mimetype, const attachment = imageService.saveImageToAttachment(noteId, file.buffer, file.originalname, true, true);
title: file.originalname, url = `api/attachments/${attachment.attachmentId}/image/${encodeURIComponent(attachment.title)}`;
content: file.buffer } else {
}); const attachment = note.saveAttachment({
role: 'file',
mime: file.mimetype,
title: file.originalname,
content: file.buffer
});
url = `#${noteId}?viewMode=attachments&attachmentId=${attachment.attachmentId}`;
}
return { return {
uploaded: true uploaded: true,
url
}; };
} }

@ -4,7 +4,6 @@ const imageService = require('../../services/image');
const becca = require('../../becca/becca'); const becca = require('../../becca/becca');
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR; const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
const fs = require('fs'); const fs = require('fs');
const ValidationError = require("../../errors/validation_error");
function returnImage(req, res) { function returnImage(req, res) {
const image = becca.getNote(req.params.noteId); const image = becca.getNote(req.params.noteId);
@ -59,24 +58,6 @@ function returnAttachedImage(req, res) {
res.send(attachment.getContent()); res.send(attachment.getContent());
} }
function uploadImage(req) {
const {noteId} = req.query;
const {file} = req;
becca.getNoteOrThrow(noteId);
if (!["image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
throw new ValidationError(`Unknown image type '${file.mimetype}'`);
}
const {url} = imageService.saveImageToAttachment(noteId, file.buffer, file.originalname, true, true);
return {
uploaded: true,
url
};
}
function updateImage(req) { function updateImage(req) {
const {noteId} = req.params; const {noteId} = req.params;
const {file} = req; const {file} = req;
@ -98,6 +79,5 @@ function updateImage(req) {
module.exports = { module.exports = {
returnImage, returnImage,
returnAttachedImage, returnAttachedImage,
uploadImage,
updateImage updateImage
}; };

@ -201,7 +201,6 @@ function register(app) {
// :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename // :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename
route(GET, '/api/images/:noteId/:filename', [auth.checkApiAuthOrElectron], imageRoute.returnImage); route(GET, '/api/images/:noteId/:filename', [auth.checkApiAuthOrElectron], imageRoute.returnImage);
route(PST, '/api/images', [auth.checkApiAuthOrElectron, uploadMiddlewareWithErrorHandling, csrfMiddleware], imageRoute.uploadImage, apiResultHandler);
route(PUT, '/api/images/:noteId', [auth.checkApiAuthOrElectron, uploadMiddlewareWithErrorHandling, csrfMiddleware], imageRoute.updateImage, apiResultHandler); route(PUT, '/api/images/:noteId', [auth.checkApiAuthOrElectron, uploadMiddlewareWithErrorHandling, csrfMiddleware], imageRoute.updateImage, apiResultHandler);
apiRoute(GET, '/api/options', optionsApiRoute.getOptions); apiRoute(GET, '/api/options', optionsApiRoute.getOptions);

@ -162,10 +162,7 @@ function saveImageToAttachment(noteId, uploadBuffer, originalName, shrinkImageSw
}); });
}); });
return { return attachment;
attachment,
url: `api/attachments/${attachment.attachmentId}/image/${encodeURIComponent(fileName)}`
};
} }
async function shrinkImage(buffer, originalName) { async function shrinkImage(buffer, originalName) {