Skip to content

Commit

Permalink
Also send referrer when fetching images for EPUB.
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo committed May 27, 2024
1 parent cf4d959 commit 9da4385
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
22 changes: 13 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,6 @@ async function cleanup(url, options) {

err.write(' ✓\n');

console.log(getUrlOrigin(final_url));

if (options.inline) {
await inlineImages(
parsed.content,
Expand All @@ -314,7 +312,7 @@ async function cleanup(url, options) {
stripping the URL down to its origin,
but just in case, let’s strip it ourselves.
*/
referrer: final_url,
referrer: getUrlOrigin(final_url),
referrerPolicy: 'strict-origin-when-cross-origin',
timeout: 10 * 1000
},
Expand Down Expand Up @@ -893,17 +891,23 @@ async function epubgen(data, output_path, options) {
let entry = remoteResources[i];
try {
if (options.debug) {
err.write(`Fetching: ${entry[0]}\n`);
err.write(`Fetching: ${entry.original}\n`);
}
let stream = (
await fetch(entry[0], {
await fetch(entry.original, {
headers: {
'user-agent': UA
},
/*
Send the referrer as the browser would
when fetching the image to render it.
*/
referrer: entry.origin,
referrerPolicy: 'strict-origin-when-cross-origin',
timeout: 10 * 1000
})
).body;
archive.append(stream, { name: `OEBPS/${entry[1]}` });
archive.append(stream, { name: `OEBPS/${entry.mapped}` });
} catch (err) {
console.error(err);
}
Expand Down Expand Up @@ -958,9 +962,9 @@ async function epubgen(data, output_path, options) {
}
: undefined,
remoteResources: remoteResources.map(entry => ({
id: entry[1].replace(/[^a-z0-9]/gi, ''),
href: entry[1],
mimetype: fileMimetype(entry[1])
id: entry.mapped.replace(/[^a-z0-9]/gi, ''),
href: entry.mapped,
mimetype: fileMimetype(entry.mapped)
}))
});

Expand Down
11 changes: 8 additions & 3 deletions src/remote-resources.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { randomUUID as uuid } from 'node:crypto';
import { parseSrcset, stringifySrcset } from 'srcset';
import { REGEX_IMAGE_URL } from './constants/regex.js';
import { getUrlOrigin } from './util/url-origin.js';

export default function remoteResources(doc) {
let srcs = new Map();
Expand All @@ -21,9 +22,13 @@ export default function remoteResources(doc) {
return src;
}
if (!srcs.has(src)) {
srcs.set(src, `rr-${uuid()}.${match[1]}`);
srcs.set(src, {
original: src,
mapped: `rr-${uuid()}.${match[1]}`,
origin: getUrlOrigin(doc.baseURI)
});
}
return `./${srcs.get(src)}`;
return `./${srcs.get(src).mapped}`;
}

Array.from(doc.querySelectorAll('picture source[src], img[src]')).forEach(
Expand Down Expand Up @@ -63,5 +68,5 @@ export default function remoteResources(doc) {
console.error(err);
}
});
return Array.from(srcs.entries());
return Array.from(srcs.values());
}

0 comments on commit 9da4385

Please sign in to comment.