gtts plugin, sounds better than opentts:ko

This commit is contained in:
polv committed 2026-08-26 18:21:47 +07:00
1 parent 7a4c71675b
commit 7e8c38a80f
6 files changed
+162 -59

No files matched your search

+12 -58
View File
@@ -1,8 +1,6 @@
import fs from "node:fs";
import type { IncomingMessage, ServerResponse } from "node:http";
import path from "node:path";
import type { Connect, Plugin, ViteDevServer } from "vite";
import { defineConfig, type MarkdownOptions } from "vitepress";
import { mp3DownloaderPlugin } from "./vite-plugins/mp3-downloader";
import { gttsPlugin } from "./vite-plugins/gtts";
const host = process.env.TAURI_DEV_HOST;
@@ -69,7 +67,12 @@ export default defineConfig({
},
},
vite: {
plugins: [audioProxyPlugin() as any],
plugins: [
mp3DownloaderPlugin() as any,
// (Python) pip install gtts
// gtts will intercept /api/tts away from opentts:ko container
gttsPlugin(),
],
envDir: process.cwd(),
ssr: {
noExternal: [],
@@ -98,6 +101,10 @@ export default defineConfig({
port: 5174,
}
: undefined,
proxy: {
// docker run -it -p 5500:5500 synesthesiam/opentts:ko
"/api/tts": "http://localhost:5500",
},
},
// Env variables starting with the item of `envPrefix` will be exposed in tauri's source code through `import.meta.env`.
envPrefix: ["VITE_", "TAURI_ENV_*"],
@@ -112,56 +119,3 @@ export default defineConfig({
},
},
});
function audioProxyPlugin(): Plugin {
return {
name: "audio-proxy",
configureServer(server: ViteDevServer) {
// Vite dev server middlewares are connect-style: (req, res, next)
server.middlewares.use(async (req, res, next) => {
const reqUrl = req.url;
if (!reqUrl || !reqUrl.startsWith("/uploads/")) return next();
if (!reqUrl.endsWith(".mp3")) return next();
try {
await proxyAudio(req, res, next);
} catch (e) {
console.error(e, reqUrl);
next();
}
});
},
};
}
async function proxyAudio(
req: Connect.IncomingMessage,
res: ServerResponse<IncomingMessage>,
next: Connect.NextFunction,
) {
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 saveto = path.join(saveroot, ...segs, filename);
if (fs.existsSync(saveto)) {
return next();
}
fs.mkdirSync(path.join(saveroot, ...segs), { recursive: true });
const r = await fetch(url, { redirect: "follow" });
if (!r.ok) {
console.error(r);
return next();
}
fs.writeFileSync(saveto, Buffer.from(await r.arrayBuffer()));
// console.log(fs.statSync(saveto));
console.log("saved to", saveto);
res.setHeader("Content-Type", "audio/mpeg");
fs.createReadStream(saveto).pipe(res);
}