mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	note revisions for images, WIP
This commit is contained in:
		| @@ -61,6 +61,11 @@ $list.on('change', async () => { | |||||||
|     else if (note.type === 'code') { |     else if (note.type === 'code') { | ||||||
|         $content.html($("<pre>").text(fullNoteRevision.content)); |         $content.html($("<pre>").text(fullNoteRevision.content)); | ||||||
|     } |     } | ||||||
|  |     else if (note.type === 'image') { | ||||||
|  |         $content.html($("<img>") | ||||||
|  |             .attr("src", `data:${note.mime};base64,` + fullNoteRevision.content) | ||||||
|  |             .css("width", "100%")); | ||||||
|  |     } | ||||||
|     else { |     else { | ||||||
|         $content.text("Preview isn't available for this note type."); |         $content.text("Preview isn't available for this note type."); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -64,6 +64,8 @@ class NoteDetailImage { | |||||||
|             if (result.uploaded) { |             if (result.uploaded) { | ||||||
|                 toastService.showMessage("New revision of the image has been uploaded.") |                 toastService.showMessage("New revision of the image has been uploaded.") | ||||||
|  |  | ||||||
|  |                 await utils.clearBrowserCache(); | ||||||
|  |  | ||||||
|                 await noteDetailService.reload(); |                 await noteDetailService.reload(); | ||||||
|             } |             } | ||||||
|             else { |             else { | ||||||
|   | |||||||
| @@ -205,6 +205,13 @@ function isHtmlEmpty(html) { | |||||||
|     return $("<div>").html(html).text().trim().length === 0 && !html.toLowerCase().includes('<img'); |     return $("<div>").html(html).text().trim().length === 0 && !html.toLowerCase().includes('<img'); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | async function clearBrowserCache() { | ||||||
|  |     if (isElectron()) { | ||||||
|  |         const win = require('electron').remote.getCurrentWindow(); | ||||||
|  |         await win.webContents.session.clearCache(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|     reloadApp, |     reloadApp, | ||||||
|     parseDate, |     parseDate, | ||||||
| @@ -236,5 +243,6 @@ export default { | |||||||
|     getNoteTypeClass, |     getNoteTypeClass, | ||||||
|     getMimeTypeClass, |     getMimeTypeClass, | ||||||
|     closeActiveDialog, |     closeActiveDialog, | ||||||
|     isHtmlEmpty |     isHtmlEmpty, | ||||||
|  |     clearBrowserCache | ||||||
| }; | }; | ||||||
| @@ -18,6 +18,10 @@ async function getNoteRevision(req) { | |||||||
|  |  | ||||||
|     await noteRevision.getContent(); |     await noteRevision.getContent(); | ||||||
|  |  | ||||||
|  |     if (noteRevision.content && ['file', 'image'].includes(noteRevision.type)) { | ||||||
|  |         noteRevision.content = noteRevision.content.toString('base64'); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     return noteRevision; |     return noteRevision; | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -12,6 +12,8 @@ const imageminGifLossy = require('imagemin-giflossy'); | |||||||
| const jimp = require('jimp'); | const jimp = require('jimp'); | ||||||
| const imageType = require('image-type'); | const imageType = require('image-type'); | ||||||
| const sanitizeFilename = require('sanitize-filename'); | const sanitizeFilename = require('sanitize-filename'); | ||||||
|  | const dateUtils = require('./date_utils'); | ||||||
|  | const NoteRevision = require("../entities/note_revision"); | ||||||
|  |  | ||||||
| async function processImage(uploadBuffer, originalName, shrinkImageSwitch) { | async function processImage(uploadBuffer, originalName, shrinkImageSwitch) { | ||||||
|     const origImageFormat = imageType(uploadBuffer); |     const origImageFormat = imageType(uploadBuffer); | ||||||
| @@ -36,12 +38,31 @@ async function updateImage(noteId, uploadBuffer, originalName) { | |||||||
|  |  | ||||||
|     const note = await repository.getNote(noteId); |     const note = await repository.getNote(noteId); | ||||||
|  |  | ||||||
|  |     const noteRevision = await new NoteRevision({ | ||||||
|  |         noteId: note.noteId, | ||||||
|  |         // title and text should be decrypted now | ||||||
|  |         title: note.title, | ||||||
|  |         contentLength: -1, // will be updated in .setContent() | ||||||
|  |         type: note.type, | ||||||
|  |         mime: note.mime, | ||||||
|  |         isProtected: false, // will be fixed in the protectNoteRevisions() call | ||||||
|  |         utcDateLastEdited: note.utcDateModified, | ||||||
|  |         utcDateCreated: dateUtils.utcNowDateTime(), | ||||||
|  |         utcDateModified: dateUtils.utcNowDateTime(), | ||||||
|  |         dateLastEdited: note.dateModified, | ||||||
|  |         dateCreated: dateUtils.localNowDateTime() | ||||||
|  |     }).save(); | ||||||
|  |  | ||||||
|  |     await noteRevision.setContent(await note.getContent()); | ||||||
|  |  | ||||||
|     note.mime = 'image/' + imageFormat.ext.toLowerCase(); |     note.mime = 'image/' + imageFormat.ext.toLowerCase(); | ||||||
|  |  | ||||||
|     await note.setContent(buffer); |     await note.setContent(buffer); | ||||||
|  |  | ||||||
|     await note.setLabel('originalFileName', originalName); |     await note.setLabel('originalFileName', originalName); | ||||||
|     await note.setLabel('fileSize', buffer.byteLength); |     await note.setLabel('fileSize', buffer.byteLength); | ||||||
|  |  | ||||||
|  |     await noteService.protectNoteRevisions(note); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSwitch) { | async function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSwitch) { | ||||||
| @@ -62,13 +83,11 @@ async function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSw | |||||||
|         ] |         ] | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     const imageHash = note.utcDateModified.replace(" ", "_"); |  | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
|         fileName, |         fileName, | ||||||
|         note, |         note, | ||||||
|         noteId: note.noteId, |         noteId: note.noteId, | ||||||
|         url: `api/images/${note.noteId}/${fileName}?${imageHash}` |         url: `api/images/${note.noteId}/${fileName}` | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -537,5 +537,6 @@ module.exports = { | |||||||
|     deleteBranch, |     deleteBranch, | ||||||
|     protectNoteRecursively, |     protectNoteRecursively, | ||||||
|     scanForLinks, |     scanForLinks, | ||||||
|     duplicateNote |     duplicateNote, | ||||||
|  |     protectNoteRevisions | ||||||
| }; | }; | ||||||
		Reference in New Issue
	
	Block a user