mirror of
https://github.com/zadam/trilium.git
synced 2025-01-17 20:48:12 +08:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
f5b933149a
3 changed files with 34 additions and 10 deletions
|
@ -33,6 +33,7 @@
|
|||
"electron-in-page-search": "1.3.2",
|
||||
"express": "4.16.4",
|
||||
"express-session": "1.15.6",
|
||||
"file-type": "10.7.0",
|
||||
"fs-extra": "7.0.1",
|
||||
"get-port": "4.1.0",
|
||||
"helmet": "3.15.0",
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
"use strict";
|
||||
|
||||
const repository = require('./repository');
|
||||
const log = require('./log');
|
||||
const protectedSessionService = require('./protected_session');
|
||||
const noteService = require('./notes');
|
||||
const imagemin = require('imagemin');
|
||||
|
@ -13,7 +14,13 @@ const sanitizeFilename = require('sanitize-filename');
|
|||
|
||||
async function saveImage(buffer, originalName, parentNoteId) {
|
||||
const resizedImage = await resize(buffer);
|
||||
const optimizedImage = await optimize(resizedImage);
|
||||
let optimizedImage;
|
||||
try {
|
||||
optimizedImage = await optimize(resizedImage);
|
||||
} catch (e) {
|
||||
log.error(e);
|
||||
optimizedImage = resizedImage;
|
||||
}
|
||||
|
||||
const imageFormat = imageType(optimizedImage);
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
const sax = require("sax");
|
||||
const fileType = require('file-type');
|
||||
const stream = require('stream');
|
||||
const xml2js = require('xml2js');
|
||||
const log = require("../log");
|
||||
|
@ -222,7 +223,26 @@ async function importEnex(file, parentNote) {
|
|||
|
||||
const mediaRegex = new RegExp(`<en-media hash="${hash}"[^>]*>`, 'g');
|
||||
|
||||
const fileTypeFromBuffer = fileType(resource.content);
|
||||
if (fileTypeFromBuffer) {
|
||||
// If fileType returns something for buffer, then set the mime given
|
||||
resource.mime = fileTypeFromBuffer.mime;
|
||||
}
|
||||
|
||||
const createResourceNote = async () => {
|
||||
const resourceNote = (await noteService.createNote(noteEntity.noteId, resource.title, resource.content, {
|
||||
attributes: resource.attributes,
|
||||
type: 'file',
|
||||
mime: resource.mime
|
||||
})).note;
|
||||
|
||||
const resourceLink = `<a href="#root/${resourceNote.noteId}">${utils.escapeHtml(resource.title)}</a>`;
|
||||
|
||||
noteEntity.content = noteEntity.content.replace(mediaRegex, resourceLink);
|
||||
}
|
||||
|
||||
if (["image/jpeg", "image/png", "image/gif"].includes(resource.mime)) {
|
||||
try {
|
||||
const originalName = "image." + resource.mime.substr(6);
|
||||
|
||||
const { url } = await imageService.saveImage(resource.content, originalName, noteEntity.noteId);
|
||||
|
@ -236,17 +256,13 @@ async function importEnex(file, parentNote) {
|
|||
// otherwise image would be removed since no note would include it
|
||||
note.content += imageLink;
|
||||
}
|
||||
} catch (e) {
|
||||
log.error("error when saving image from ENEX file: " + e);
|
||||
await createResourceNote();
|
||||
}
|
||||
}
|
||||
else {
|
||||
const resourceNote = (await noteService.createNote(noteEntity.noteId, resource.title, resource.content, {
|
||||
attributes: resource.attributes,
|
||||
type: 'file',
|
||||
mime: resource.mime
|
||||
})).note;
|
||||
|
||||
const resourceLink = `<a href="#root/${resourceNote.noteId}">${utils.escapeHtml(resource.title)}</a>`;
|
||||
|
||||
noteEntity.content = noteEntity.content.replace(mediaRegex, resourceLink);
|
||||
await createResourceNote();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue