local image storage fixes

This commit is contained in:
zadam 2020-03-25 18:21:55 +01:00
parent 8a92786012
commit ef61e22f1f
4 changed files with 18 additions and 8 deletions

View File

@ -18,7 +18,7 @@ async function returnImage(req, res) {
res.set('Content-Type', 'image/png'); res.set('Content-Type', 'image/png');
return res.send(fs.readFileSync(RESOURCE_DIR + '/db/image-deleted.png')); return res.send(fs.readFileSync(RESOURCE_DIR + '/db/image-deleted.png'));
} }
image.mime = image.mime.replace("image/svg", "image/svg+xml");
res.set('Content-Type', image.mime); res.set('Content-Type', image.mime);
res.send(await image.getContent()); res.send(await image.getContent());

View File

@ -44,6 +44,12 @@ function getImageType(buffer) {
} }
} }
function getImageMimeFromExtension(ext) {
ext = ext.toLowerCase();
return 'image/' + (ext === 'svg' ? 'svg+xml' : ext);
}
async function updateImage(noteId, uploadBuffer, originalName) { async function updateImage(noteId, uploadBuffer, originalName) {
const {buffer, imageFormat} = await processImage(uploadBuffer, originalName, true); const {buffer, imageFormat} = await processImage(uploadBuffer, originalName, true);
@ -51,7 +57,7 @@ async function updateImage(noteId, uploadBuffer, originalName) {
await noteRevisionService.createNoteRevision(note); await noteRevisionService.createNoteRevision(note);
note.mime = 'image/' + imageFormat.ext.toLowerCase(); note.mime = getImageMimeFromExtension(imageFormat.ext);
await note.setContent(buffer); await note.setContent(buffer);
@ -72,7 +78,7 @@ async function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSw
title: fileName, title: fileName,
content: buffer, content: buffer,
type: 'image', type: 'image',
mime: 'image/' + imageFormat.ext.toLowerCase(), mime: getImageMimeFromExtension(imageFormat.ext),
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable() isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable()
}); });

View File

@ -280,11 +280,13 @@ async function downloadImage(noteId, imageUrl) {
const downloadImagePromises = {}; const downloadImagePromises = {};
function replaceUrl(content, url, imageNote) { function replaceUrl(content, url, imageNote) {
return content.replace(new RegExp(url, "g"), `api/images/${imageNote.noteId}/${imageNote.title}`); const quoted = url.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return content.replace(new RegExp(quoted, "g"), `api/images/${imageNote.noteId}/${imageNote.title}`);
} }
async function downloadImages(noteId, content) { async function downloadImages(noteId, content) {
const re = /<img\s.*?src=['"]([^'">]+)['"]/ig; const re = /<img.*?\ssrc=['"]([^'">]+)['"]/ig;
let match; let match;
while (match = re.exec(content)) { while (match = re.exec(content)) {
@ -323,7 +325,7 @@ async function downloadImages(noteId, content) {
} }
} }
await Promise.all(Object.values(downloadImagePromises)).then(() => { Promise.all(Object.values(downloadImagePromises)).then(() => {
setTimeout(async () => { setTimeout(async () => {
const imageNotes = await repository.getNotes(Object.values(imageUrlToNoteIdMapping)); const imageNotes = await repository.getNotes(Object.values(imageUrlToNoteIdMapping));
@ -344,6 +346,8 @@ async function downloadImages(noteId, content) {
} }
}, 5000); }, 5000);
}); });
return content;
} }
async function saveLinks(note, content) { async function saveLinks(note, content) {
@ -363,7 +367,7 @@ async function saveLinks(note, content) {
content = findExternalLinks(content, foundLinks); content = findExternalLinks(content, foundLinks);
content = findIncludeNoteLinks(content, foundLinks); content = findIncludeNoteLinks(content, foundLinks);
downloadImages(note.noteId, content); content = await downloadImages(note.noteId, content);
} }
else if (note.type === 'relation-map') { else if (note.type === 'relation-map') {
findRelationMapLinks(content, foundLinks); findRelationMapLinks(content, foundLinks);

View File

@ -158,7 +158,7 @@ function getContentDisposition(filename) {
return `file; filename="${sanitizedFilename}"; filename*=UTF-8''${sanitizedFilename}`; return `file; filename="${sanitizedFilename}"; filename*=UTF-8''${sanitizedFilename}`;
} }
const STRING_MIME_TYPES = ["application/x-javascript", "image/svg"]; const STRING_MIME_TYPES = ["application/x-javascript", "image/svg+xml"];
function isStringNote(type, mime) { function isStringNote(type, mime) {
return ["text", "code", "relation-map", "search"].includes(type) return ["text", "code", "relation-map", "search"].includes(type)