fix media downloader
This commit is contained in:
1 parent
16aea2f26f
commit
bc46de9e41
4 files changed
+46
-61
No files matched your search
+16
-51
@@ -1,9 +1,8 @@
|
||||
import fs from "node:fs";
|
||||
import { IncomingMessage, ServerResponse } from "node:http";
|
||||
import https from "node:https";
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
import path from "node:path";
|
||||
import { Connect, Plugin, ViteDevServer } from "vite";
|
||||
import { defineConfig, MarkdownOptions } from "vitepress";
|
||||
import type { Connect, Plugin, ViteDevServer } from "vite";
|
||||
import { defineConfig, type MarkdownOptions } from "vitepress";
|
||||
|
||||
const host = process.env.TAURI_DEV_HOST;
|
||||
|
||||
@@ -130,68 +129,34 @@ function audioProxyPlugin(): Plugin {
|
||||
};
|
||||
}
|
||||
|
||||
function proxyAudio(
|
||||
async function proxyAudio(
|
||||
req: Connect.IncomingMessage,
|
||||
res: ServerResponse<IncomingMessage>,
|
||||
next: Connect.NextFunction,
|
||||
) {
|
||||
const url = new URL(
|
||||
`https://www.howtostudykorean.com/wp-content/uploads${req.url}`,
|
||||
);
|
||||
|
||||
// Forward common headers (important for Range streaming)
|
||||
const headers = {
|
||||
...req.headers,
|
||||
host: url.host,
|
||||
range: req.headers["range"],
|
||||
};
|
||||
const url = new URL(`https://www.howtostudykorean.com/wp-content${req.url}`);
|
||||
|
||||
const segs = req.url!.slice(1).split("/");
|
||||
const filename = segs.pop()!;
|
||||
const saveroot = "docs/public";
|
||||
const savetemp = path.join(saveroot, ...segs, filename + ".temp");
|
||||
const saveto = path.join(saveroot, ...segs, filename);
|
||||
|
||||
if (fs.existsSync(saveto)) {
|
||||
next();
|
||||
return;
|
||||
return next();
|
||||
}
|
||||
|
||||
fs.mkdirSync(path.join(saveroot, ...segs), { recursive: true });
|
||||
const fileStream = fs.createWriteStream(savetemp);
|
||||
|
||||
const upstreamReq = https.request(
|
||||
{
|
||||
protocol: url.protocol,
|
||||
hostname: url.hostname,
|
||||
port: url.port || undefined,
|
||||
path: url.pathname + url.search,
|
||||
method: req.method,
|
||||
headers,
|
||||
},
|
||||
(upstreamRes) => {
|
||||
// Pass status (200 or 206 for Range)
|
||||
res.writeHead(upstreamRes.statusCode || 200, upstreamRes.headers);
|
||||
const r = await fetch(url, { redirect: "follow" });
|
||||
if (!r.ok) {
|
||||
console.error(r);
|
||||
return next();
|
||||
}
|
||||
|
||||
// Stream through
|
||||
upstreamRes.pipe(res);
|
||||
upstreamRes.pipe(fileStream);
|
||||
},
|
||||
);
|
||||
fs.writeFileSync(saveto, Buffer.from(await r.arrayBuffer()));
|
||||
// console.log(fs.statSync(saveto));
|
||||
console.log("saved to", saveto);
|
||||
|
||||
fileStream.on("finish", () => {
|
||||
fs.renameSync(savetemp, saveto);
|
||||
console.log("saved to", saveto);
|
||||
});
|
||||
|
||||
fileStream.on("error", (err) => {
|
||||
console.error("write failed:", err);
|
||||
});
|
||||
|
||||
upstreamReq.on("error", (err) => {
|
||||
res.statusCode = 502;
|
||||
res.end(`Bad Gateway: ${err.message}`);
|
||||
});
|
||||
|
||||
req.pipe(upstreamReq);
|
||||
res.setHeader("Content-Type", "audio/mpeg");
|
||||
fs.createReadStream(saveto).pipe(res);
|
||||
}
|
||||
@@ -30,9 +30,11 @@ async function playAudio() {
|
||||
const uploadURL = "https://www.howtostudykorean.com/wp-content/uploads";
|
||||
let src = props.src;
|
||||
if (src.startsWith(uploadURL)) {
|
||||
src = src.replace(uploadURL, "/uploads/");
|
||||
src = src.replace(uploadURL, "/uploads");
|
||||
audioEl.value.onerror = () => {
|
||||
if (!audioEl.value) return;
|
||||
console.log(audioEl.value.error, src);
|
||||
|
||||
audioEl.value.src = props.src;
|
||||
audioEl.value.currentTime = 0;
|
||||
audioEl.value.play();
|
||||
|
||||
Reference in new issue
Block a user