mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-04-12 16:24:04 +02:00
Merge pull request #20971 from Snuffleupagus/downloadFile-fetch
Download test PDFs with the Fetch API
This commit is contained in:
commit
3e651dd690
@ -15,9 +15,6 @@
|
||||
|
||||
import crypto from "crypto";
|
||||
import fs from "fs";
|
||||
import http from "http";
|
||||
import https from "https";
|
||||
import { resolve as urlResolve } from "url";
|
||||
|
||||
function rewriteWebArchiveUrl(url) {
|
||||
// Web Archive URLs need to be transformed to add `if_` after the ID.
|
||||
@ -32,54 +29,21 @@ function rewriteWebArchiveUrl(url) {
|
||||
return url;
|
||||
}
|
||||
|
||||
function downloadFile(file, url, redirects = 0) {
|
||||
async function downloadFile(file, url) {
|
||||
url = rewriteWebArchiveUrl(url);
|
||||
const protocol = /^https:\/\//.test(url) ? https : http;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
protocol
|
||||
.get(url, async function (response) {
|
||||
if ([301, 302, 307, 308].includes(response.statusCode)) {
|
||||
if (redirects > 10) {
|
||||
response.resume();
|
||||
reject(new Error("Too many redirects"));
|
||||
return;
|
||||
}
|
||||
const redirectTo = urlResolve(url, response.headers.location);
|
||||
try {
|
||||
await downloadFile(file, redirectTo, ++redirects);
|
||||
resolve();
|
||||
} catch (ex) {
|
||||
response.resume();
|
||||
reject(ex);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.statusCode !== 200) {
|
||||
response.resume();
|
||||
reject(new Error(`HTTP ${response.statusCode}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const stream = fs.createWriteStream(file);
|
||||
stream.on("error", error => reject(error));
|
||||
stream.on("finish", () => {
|
||||
stream.end();
|
||||
resolve();
|
||||
});
|
||||
response.pipe(stream);
|
||||
})
|
||||
.on("error", error => reject(error));
|
||||
});
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
return fs.promises.writeFile(file, response.body);
|
||||
}
|
||||
|
||||
async function downloadManifestFiles(manifest) {
|
||||
const links = manifest
|
||||
.filter(item => item.link && !fs.existsSync(item.file))
|
||||
.map(item => {
|
||||
let url = fs.readFileSync(`${item.file}.link`).toString();
|
||||
url = url.replace(/\s+$/, "");
|
||||
const url = fs.readFileSync(`${item.file}.link`).toString().trimEnd();
|
||||
return { file: item.file, url };
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user