mirror of
https://github.com/zadam/trilium.git
synced 2025-01-18 04:59:56 +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",
|
"electron-in-page-search": "1.3.2",
|
||||||
"express": "4.16.4",
|
"express": "4.16.4",
|
||||||
"express-session": "1.15.6",
|
"express-session": "1.15.6",
|
||||||
|
"file-type": "10.7.0",
|
||||||
"fs-extra": "7.0.1",
|
"fs-extra": "7.0.1",
|
||||||
"get-port": "4.1.0",
|
"get-port": "4.1.0",
|
||||||
"helmet": "3.15.0",
|
"helmet": "3.15.0",
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const repository = require('./repository');
|
const repository = require('./repository');
|
||||||
|
const log = require('./log');
|
||||||
const protectedSessionService = require('./protected_session');
|
const protectedSessionService = require('./protected_session');
|
||||||
const noteService = require('./notes');
|
const noteService = require('./notes');
|
||||||
const imagemin = require('imagemin');
|
const imagemin = require('imagemin');
|
||||||
|
@ -13,7 +14,13 @@ const sanitizeFilename = require('sanitize-filename');
|
||||||
|
|
||||||
async function saveImage(buffer, originalName, parentNoteId) {
|
async function saveImage(buffer, originalName, parentNoteId) {
|
||||||
const resizedImage = await resize(buffer);
|
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);
|
const imageFormat = imageType(optimizedImage);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const sax = require("sax");
|
const sax = require("sax");
|
||||||
|
const fileType = require('file-type');
|
||||||
const stream = require('stream');
|
const stream = require('stream');
|
||||||
const xml2js = require('xml2js');
|
const xml2js = require('xml2js');
|
||||||
const log = require("../log");
|
const log = require("../log");
|
||||||
|
@ -222,7 +223,26 @@ async function importEnex(file, parentNote) {
|
||||||
|
|
||||||
const mediaRegex = new RegExp(`<en-media hash="${hash}"[^>]*>`, 'g');
|
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)) {
|
if (["image/jpeg", "image/png", "image/gif"].includes(resource.mime)) {
|
||||||
|
try {
|
||||||
const originalName = "image." + resource.mime.substr(6);
|
const originalName = "image." + resource.mime.substr(6);
|
||||||
|
|
||||||
const { url } = await imageService.saveImage(resource.content, originalName, noteEntity.noteId);
|
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
|
// otherwise image would be removed since no note would include it
|
||||||
note.content += imageLink;
|
note.content += imageLink;
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log.error("error when saving image from ENEX file: " + e);
|
||||||
|
await createResourceNote();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const resourceNote = (await noteService.createNote(noteEntity.noteId, resource.title, resource.content, {
|
await createResourceNote();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue