fixes for offline downloading of images

This commit is contained in:
zadam 2020-05-03 14:33:59 +02:00
parent 4eb2407c73
commit 227c3e4dcc

View File

@ -276,9 +276,9 @@ async function downloadImage(noteId, imageUrl) {
const downloadImagePromises = {}; const downloadImagePromises = {};
function replaceUrl(content, url, imageNote) { function replaceUrl(content, url, imageNote) {
const quoted = url.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const quoted = url.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
return content.replace(new RegExp(`\s+src=[\"']${quoted}[\"']`, "g"), ` src="api/images/${imageNote.noteId}/${imageNote.title}"`); return content.replace(new RegExp(`\\s+src=[\"']${quoted}[\"']`, "g"), ` src="api/images/${imageNote.noteId}/${imageNote.title}"`);
} }
async function downloadImages(noteId, content) { async function downloadImages(noteId, content) {
@ -288,11 +288,11 @@ async function downloadImages(noteId, content) {
const origContent = content; const origContent = content;
while (match = re.exec(origContent)) { while (match = re.exec(origContent)) {
const url = match[1].toLowerCase(); const url = match[1];
if (!url.startsWith('api/images/') if (!url.startsWith('api/images/')
// this is and exception for the web clipper's "imageId" // this is and exception for the web clipper's "imageId"
&& (url.length !== 20 || url.startsWith('http'))) { && (url.length !== 20 || url.toLowerCase().startsWith('http'))) {
if (url in downloadImagePromises) { if (url in downloadImagePromises) {
// download is already in progress // download is already in progress
continue; continue;
@ -347,7 +347,7 @@ async function downloadImages(noteId, content) {
for (const url in imageUrlToNoteIdMapping) { for (const url in imageUrlToNoteIdMapping) {
const imageNote = imageNotes.find(note => note.noteId === imageUrlToNoteIdMapping[url]); const imageNote = imageNotes.find(note => note.noteId === imageUrlToNoteIdMapping[url]);
if (imageNote) { if (imageNote && !imageNote.isDeleted) {
updatedContent = replaceUrl(updatedContent, url, imageNote); updatedContent = replaceUrl(updatedContent, url, imageNote);
} }
} }
@ -356,6 +356,8 @@ async function downloadImages(noteId, content) {
if (updatedContent !== origContent) { if (updatedContent !== origContent) {
await origNote.setContent(updatedContent); await origNote.setContent(updatedContent);
await scanForLinks(origNote);
console.log(`Fixed the image links for note ${noteId} to the offline saved.`); console.log(`Fixed the image links for note ${noteId} to the offline saved.`);
} }
}, 5000); }, 5000);
@ -376,11 +378,11 @@ async function saveLinks(note, content) {
const foundLinks = []; const foundLinks = [];
if (note.type === 'text') { if (note.type === 'text') {
content = await downloadImages(note.noteId, content);
content = findImageLinks(content, foundLinks); content = findImageLinks(content, foundLinks);
content = findInternalLinks(content, foundLinks); content = findInternalLinks(content, foundLinks);
content = findIncludeNoteLinks(content, foundLinks); content = findIncludeNoteLinks(content, foundLinks);
content = await downloadImages(note.noteId, content);
} }
else if (note.type === 'relation-map') { else if (note.type === 'relation-map') {
findRelationMapLinks(content, foundLinks); findRelationMapLinks(content, foundLinks);