Skip to content

Commit

Permalink
Follow HTTP Redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Apr 5, 2023
1 parent c9ad29e commit 1c0b633
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
31 changes: 19 additions & 12 deletions app/js/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,8 @@ class Downloader {
return result;
}

downloadByIndex(part) {
const { name, cid, url } = this;
const downloadPath = document.getElementById("downloadPath").value;
const filename = document.getElementById("videoName").value || name || cid;
downloadByIndex(part, downloadPath, filename) {
const { url } = this;
const file = path.join(downloadPath, `${sanitize(filename)}-${part}.flv`);

if (this.tasks.some(item => item.url === this.links[part])) return "DUPLICATE";
Expand All @@ -159,15 +157,15 @@ class Downloader {
"Referer": url
}
};
const downloads = fs.createWriteStream(file, state ? { flags: "a" } : {}),
const stream = fs.createWriteStream(file, state ? { flags: "a" } : {}),
index = this.tasks.findIndex(item => item.url === options.url);
this.download(index, options, downloads);
this.download(index, options, stream);
//console.log(this.cid, file, options.url);

return state;
}

download(index, options, downloads) {
download(index, options, stream) {
// https://www.npmjs.com/package/progress-stream
const proStream = progress({
time: 250 //单位ms
Expand All @@ -185,11 +183,20 @@ class Downloader {
}
});
//先pipe到proStream再pipe到文件的写入流中
(options.url.startsWith("https") ? https : http).get(options.url, options, res => {
proStream.setLength(res.headers["content-length"]);
res.pipe(proStream).pipe(downloads).on("error", error => {
console.error(error);

let { url } = options;
function downloadLink(url) {
(url.startsWith("https") ? https : http).get(url, options, res => {
if (res.statusCode === 302) {
url = res.headers.location;
return downloadLink(url);
}
proStream.setLength(res.headers["content-length"]);
res.pipe(proStream).pipe(stream).on("error", error => {
console.error(error);
});
});
});
}
downloadLink(url);
}
}
6 changes: 4 additions & 2 deletions app/js/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ class Panel {
}

downloadChecked() {
const { cid } = downloader;
const { name, cid } = downloader;
const downloadPath = document.getElementById("downloadPath").value;
const filename = document.getElementById("videoName").value || name || cid;
let flag = true;
document.querySelectorAll("tbody input[type=checkbox]").forEach((element, part) => {
if (!element.checked) return;
const state = downloader.downloadByIndex(part);
const state = downloader.downloadByIndex(part, downloadPath, filename);
if (state === "DUPLICATE") return;
const addon = state ? `从 ${Math.round(state.size / 1e6)}MB 处恢复的下载` : "";
document.getElementById("download").insertAdjacentHTML("beforeend", `<span>${cid}-${part}</span>
Expand Down

0 comments on commit 1c0b633

Please sign in to comment.