fix noproxy handling for image downloading

This commit is contained in:
zadam 2020-08-03 23:33:44 +02:00
parent 4fc8bace94
commit 782127dd91
2 changed files with 9 additions and 13 deletions

View File

@ -279,18 +279,15 @@ const downloadImagePromises = {};
function replaceUrl(content, url, imageNote) {
const quotedUrl = utils.quoteRegex(url);
return content.replace(new RegExp(`\\s+src=[\"']${quotedUrl}[\"']`, "g"), ` src="api/images/${imageNote.noteId}/${imageNote.title}"`);
return content.replace(new RegExp(`\\s+src=[\"']${quotedUrl}[\"']`, "ig"), ` src="api/images/${imageNote.noteId}/${imageNote.title}"`);
}
async function downloadImages(noteId, content) {
const re = /<img[^>]*?\ssrc=['"]([^'">]+)['"]/ig;
let match;
const origContent = content;
while (match = re.exec(origContent)) {
const url = match[1];
const imageRe = /<img[^>]*?\ssrc=['"]([^'">]+)['"]/ig;
let imageMatch;
while (imageMatch = imageRe.exec(content)) {
const url = imageMatch[1];
const inlineImageMatch = /^data:image\/[a-z]+;base64,/.exec(url);
if (inlineImageMatch) {
@ -300,9 +297,9 @@ async function downloadImages(noteId, content) {
const imageService = require('../services/image');
const {note} = await imageService.saveImage(noteId, imageBuffer, "inline image", true);
content = content.substr(0, match.index)
content = content.substr(0, imageMatch.index)
+ `<img src="api/images/${note.noteId}/${note.title}"`
+ content.substr(match.index + match[0].length);
+ content.substr(imageMatch.index + imageMatch[0].length);
}
else if (!url.includes('api/images/')
// this is an exception for the web clipper's "imageId"
@ -316,7 +313,6 @@ async function downloadImages(noteId, content) {
}
else {
content = replaceUrl(content, url, imageNote);
continue;
}
}
@ -328,7 +324,6 @@ async function downloadImages(noteId, content) {
imageUrlToNoteIdMapping[url] = existingImage.noteId;
content = replaceUrl(content, url, existingImage);
continue;
}

View File

@ -84,10 +84,11 @@ function exec(opts) {
}
async function getImage(imageUrl) {
const proxyConf = await syncOptions.getSyncProxy();
const opts = {
method: 'GET',
url: imageUrl,
proxy: await syncOptions.getSyncProxy()
proxy: proxyConf !== "noproxy" ? proxyConf : null
};
const client = getClient(opts);