mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	improved detection of image notes in ENEX import, fixes #1348
This commit is contained in:
		@@ -40,7 +40,6 @@
 | 
				
			|||||||
    "electron-window-state": "5.0.3",
 | 
					    "electron-window-state": "5.0.3",
 | 
				
			||||||
    "express": "4.17.1",
 | 
					    "express": "4.17.1",
 | 
				
			||||||
    "express-session": "1.17.1",
 | 
					    "express-session": "1.17.1",
 | 
				
			||||||
    "file-type": "16.0.0",
 | 
					 | 
				
			||||||
    "fs-extra": "9.0.1",
 | 
					    "fs-extra": "9.0.1",
 | 
				
			||||||
    "helmet": "4.1.1",
 | 
					    "helmet": "4.1.1",
 | 
				
			||||||
    "html": "1.0.0",
 | 
					    "html": "1.0.0",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,4 @@
 | 
				
			|||||||
const sax = require("sax");
 | 
					const sax = require("sax");
 | 
				
			||||||
const FileType = require('file-type');
 | 
					 | 
				
			||||||
const stream = require('stream');
 | 
					const stream = require('stream');
 | 
				
			||||||
const log = require("../log");
 | 
					const log = require("../log");
 | 
				
			||||||
const utils = require("../utils");
 | 
					const utils = require("../utils");
 | 
				
			||||||
@@ -138,17 +137,6 @@ function importEnex(taskContext, file, parentNote) {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            else if (currentTag === 'mime') {
 | 
					            else if (currentTag === 'mime') {
 | 
				
			||||||
                resource.mime = text.toLowerCase();
 | 
					                resource.mime = text.toLowerCase();
 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (text.startsWith("image/")) {
 | 
					 | 
				
			||||||
                    resource.title = "image";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    // images don't have "file-name" tag so we'll create attribute here
 | 
					 | 
				
			||||||
                    resource.attributes.push({
 | 
					 | 
				
			||||||
                        type: 'label',
 | 
					 | 
				
			||||||
                        name: 'originalFileName',
 | 
					 | 
				
			||||||
                        value: resource.title + "." + text.substr(6) // extension from mime type
 | 
					 | 
				
			||||||
                    });
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else if (previousTag === 'note') {
 | 
					        else if (previousTag === 'note') {
 | 
				
			||||||
@@ -243,11 +231,7 @@ function importEnex(taskContext, file, parentNote) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            const mediaRegex = new RegExp(`<en-media hash="${hash}"[^>]*>`, 'g');
 | 
					            const mediaRegex = new RegExp(`<en-media hash="${hash}"[^>]*>`, 'g');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            const fileTypeFromBuffer = FileType.fromBuffer(resource.content);
 | 
					            resource.mime = resource.mime || "application/octet-stream";
 | 
				
			||||||
            if (fileTypeFromBuffer) {
 | 
					 | 
				
			||||||
              // If fileType returns something for buffer, then set the mime given
 | 
					 | 
				
			||||||
              resource.mime = fileTypeFromBuffer.mime;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            const createFileNote = () => {
 | 
					            const createFileNote = () => {
 | 
				
			||||||
                const resourceNote = noteService.createNewNote({
 | 
					                const resourceNote = noteService.createNewNote({
 | 
				
			||||||
@@ -260,7 +244,7 @@ function importEnex(taskContext, file, parentNote) {
 | 
				
			|||||||
                }).note;
 | 
					                }).note;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                for (const attr of resource.attributes) {
 | 
					                for (const attr of resource.attributes) {
 | 
				
			||||||
                    noteEntity.addAttribute(attr.type, attr.name, attr.value);
 | 
					                    resourceNote.addAttribute(attr.type, attr.name, attr.value);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                updateDates(resourceNote.noteId, utcDateCreated, utcDateModified);
 | 
					                updateDates(resourceNote.noteId, utcDateCreated, utcDateModified);
 | 
				
			||||||
@@ -274,10 +258,18 @@ function importEnex(taskContext, file, parentNote) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            if (resource.mime && resource.mime.startsWith('image/')) {
 | 
					            if (resource.mime && resource.mime.startsWith('image/')) {
 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    const originalName = "image." + resource.mime.substr(6);
 | 
					                    const originalName = (resource.title && resource.title !== 'resource')
 | 
				
			||||||
 | 
					                        ? resource.title
 | 
				
			||||||
 | 
					                        : `image.${resource.mime.substr(6)}`; // default if real name is not present
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    const {url, note: imageNote} = imageService.saveImage(noteEntity.noteId, resource.content, originalName, taskContext.data.shrinkImages);
 | 
					                    const {url, note: imageNote} = imageService.saveImage(noteEntity.noteId, resource.content, originalName, taskContext.data.shrinkImages);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    for (const attr of resource.attributes) {
 | 
				
			||||||
 | 
					                        if (attr.name !== 'originalFileName') { // this one is already saved in imageService
 | 
				
			||||||
 | 
					                            imageNote.addAttribute(attr.type, attr.name, attr.value);
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    updateDates(imageNote.noteId, utcDateCreated, utcDateModified);
 | 
					                    updateDates(imageNote.noteId, utcDateCreated, utcDateModified);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    const imageLink = `<img src="${url}">`;
 | 
					                    const imageLink = `<img src="${url}">`;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user