mirror of
https://github.com/khairul169/home-lab.git
synced 2026-10-03 01:23:58 +07:00
feat: add yt mp3 downloader
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { createReadStream } from "fs";
|
||||
import fs from "node:fs/promises";
|
||||
import { HTTPException } from "hono/http-exception";
|
||||
import { getMimeType } from "../../lib/utils";
|
||||
import { filesDirList } from "./utils";
|
||||
import type { Context } from "hono";
|
||||
|
||||
export const download = async (c: Context) => {
|
||||
const dlFile = c.req.query("dl") === "true";
|
||||
const url = new URL(c.req.url, `http://${c.req.header("host")}`);
|
||||
const pathname = decodeURI(url.pathname).split("/");
|
||||
const pathSlice = pathname.slice(pathname.indexOf("download") + 1);
|
||||
const baseName = pathSlice[0];
|
||||
const path = "/" + pathSlice.slice(1).join("/");
|
||||
const filename = path.substring(1);
|
||||
|
||||
try {
|
||||
if (!baseName?.length) {
|
||||
throw new Error("baseName is empty");
|
||||
}
|
||||
|
||||
const baseDir = filesDirList.find((i) => i.name === baseName)?.path;
|
||||
if (!baseDir) {
|
||||
throw new Error("baseDir not found");
|
||||
}
|
||||
|
||||
const filepath = baseDir + path;
|
||||
const stat = await fs.stat(filepath);
|
||||
const size = stat.size;
|
||||
|
||||
if (dlFile) {
|
||||
c.header("Content-Type", "application/octet-stream");
|
||||
c.header(
|
||||
"Content-Disposition",
|
||||
`attachment; filename="${encodeURIComponent(filename)}"`
|
||||
);
|
||||
} else {
|
||||
c.header("Content-Type", getMimeType(filepath));
|
||||
}
|
||||
|
||||
if (c.req.method == "HEAD" || c.req.method == "OPTIONS") {
|
||||
c.header("Content-Length", size.toString());
|
||||
c.status(200);
|
||||
return c.body(null);
|
||||
}
|
||||
|
||||
const range = c.req.header("range") || "";
|
||||
|
||||
if (!range || dlFile) {
|
||||
c.header("Content-Length", size.toString());
|
||||
return c.body(createReadStream(filepath), 200);
|
||||
}
|
||||
|
||||
c.header("Accept-Ranges", "bytes");
|
||||
c.header("Date", stat.birthtime.toUTCString());
|
||||
|
||||
const parts = range.replace(/bytes=/, "").split("-", 2);
|
||||
const start = parts[0] ? parseInt(parts[0], 10) : 0;
|
||||
let end = parts[1] ? parseInt(parts[1], 10) : stat.size - 1;
|
||||
if (size < end - start + 1) {
|
||||
end = size - 1;
|
||||
}
|
||||
|
||||
const chunksize = end - start + 1;
|
||||
const stream = createReadStream(filepath, { start, end });
|
||||
|
||||
c.header("Content-Length", chunksize.toString());
|
||||
c.header("Content-Range", `bytes ${start}-${end}/${stat.size}`);
|
||||
|
||||
return c.body(stream, 206);
|
||||
} catch (err) {
|
||||
// console.log("err", err);
|
||||
throw new HTTPException(404, { message: "Not Found!" });
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { Context } from "hono";
|
||||
import type { GetFilesSchema } from "./schema";
|
||||
import fs from "node:fs/promises";
|
||||
import { filesDirList, getFilePath } from "./utils";
|
||||
|
||||
export const getFiles = async (c: Context) => {
|
||||
const input: GetFilesSchema = c.req.query();
|
||||
const { baseName, path, pathname } = getFilePath(input.path);
|
||||
|
||||
if (!baseName?.length) {
|
||||
return c.json(
|
||||
filesDirList.map((i) => ({
|
||||
name: i.name,
|
||||
path: "/" + i.name,
|
||||
isDirectory: true,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const entities = await fs.readdir(path, { withFileTypes: true });
|
||||
|
||||
const files = entities
|
||||
.filter((e) => !e.name.startsWith("."))
|
||||
.map((e) => ({
|
||||
name: e.name,
|
||||
path: [pathname, e.name].join("/"),
|
||||
isDirectory: e.isDirectory(),
|
||||
}))
|
||||
.sort((a, b) => {
|
||||
if (a.isDirectory && !b.isDirectory) {
|
||||
return -1;
|
||||
} else if (!a.isDirectory && b.isDirectory) {
|
||||
return 1;
|
||||
} else {
|
||||
return a.name.localeCompare(b.name);
|
||||
}
|
||||
});
|
||||
|
||||
return c.json(files);
|
||||
} catch (err) {}
|
||||
|
||||
return c.json([]);
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { Hono } from "hono";
|
||||
import { getFilesSchema, ytdlSchema } from "./schema";
|
||||
import { getFiles } from "./get";
|
||||
import { download } from "./download";
|
||||
import { getYtdl, ytdl } from "./ytdl";
|
||||
|
||||
const route = new Hono()
|
||||
.get("/", zValidator("query", getFilesSchema), getFiles)
|
||||
.post("/upload")
|
||||
.post("/ytdl", zValidator("json", ytdlSchema), ytdl)
|
||||
.get("/ytdl/:id", getYtdl)
|
||||
.get("/download/*", download);
|
||||
|
||||
export default route;
|
||||
@@ -0,0 +1,24 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const getFilesSchema = z
|
||||
.object({
|
||||
path: z.string(),
|
||||
})
|
||||
.partial()
|
||||
.optional();
|
||||
|
||||
export type GetFilesSchema = z.infer<typeof getFilesSchema>;
|
||||
|
||||
export const uploadSchema = z.object({
|
||||
path: z.string().min(1),
|
||||
size: z.string().min(1),
|
||||
});
|
||||
|
||||
export type UploadSchema = z.infer<typeof uploadSchema>;
|
||||
|
||||
export const ytdlSchema = z.object({
|
||||
url: z.string().min(1),
|
||||
path: z.string().min(1),
|
||||
});
|
||||
|
||||
export type YtdlSchema = z.infer<typeof ytdlSchema>;
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { Context } from "hono";
|
||||
import fs from "node:fs/promises";
|
||||
import { uploadSchema } from "./schema";
|
||||
import { HTTPException } from "hono/http-exception";
|
||||
import { getFilePath } from "./utils";
|
||||
|
||||
export const upload = async (c: Context) => {
|
||||
const input: any = (await c.req.parseBody()) as never;
|
||||
const data = await uploadSchema.parseAsync(input);
|
||||
|
||||
const size = parseInt(input.size);
|
||||
if (Number.isNaN(size) || !size) {
|
||||
throw new HTTPException(400, { message: "Size is empty!" });
|
||||
}
|
||||
|
||||
const files: File[] = [...Array(size)]
|
||||
.map((_, idx) => input[`files.${idx}`])
|
||||
.filter((i) => !!i);
|
||||
|
||||
if (!files.length) {
|
||||
throw new HTTPException(400, { message: "Files is empty!" });
|
||||
}
|
||||
|
||||
const { baseDir, path: targetDir } = getFilePath(data.path);
|
||||
if (!baseDir?.length) {
|
||||
throw new HTTPException(400, { message: "Path not found!" });
|
||||
}
|
||||
|
||||
// files.forEach((file) => {
|
||||
// const filepath = targetDir + "/" + file.name;
|
||||
// if (existsSync(filepath)) {
|
||||
// throw new HTTPException(400, { message: "File already exists!" });
|
||||
// }
|
||||
// });
|
||||
|
||||
await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const filepath = targetDir + "/" + file.name;
|
||||
const buffer = await file.arrayBuffer();
|
||||
await fs.writeFile(filepath, new Uint8Array(buffer));
|
||||
})
|
||||
);
|
||||
|
||||
return c.json({ success: true });
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
export const filesDirList = process.env.FILE_DIRS
|
||||
? process.env.FILE_DIRS.split(";").map((i) => ({
|
||||
name: i.split("/").at(-1),
|
||||
path: i,
|
||||
}))
|
||||
: [];
|
||||
|
||||
export function getFilePath(path?: string) {
|
||||
const pathSlices =
|
||||
path
|
||||
?.replace(/\/{2,}/g, "/")
|
||||
.replace(/\/$/, "")
|
||||
.split("/") || [];
|
||||
const baseName = pathSlices[1] || null;
|
||||
const filePath = pathSlices.slice(2).join("/");
|
||||
const baseDir = filesDirList.find((i) => i.name === baseName)?.path;
|
||||
|
||||
return {
|
||||
path: [baseDir || "", filePath].join("/").replace(/\/$/, ""),
|
||||
pathname: ["", baseName, filePath].join("/").replace(/\/$/, ""),
|
||||
baseName,
|
||||
baseDir,
|
||||
filePath,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import type { Context } from "hono";
|
||||
import type { YtdlSchema } from "./schema";
|
||||
import Queue from "better-queue";
|
||||
import { nanoid } from "nanoid";
|
||||
import yt2mp3, { getVideoInfo } from "../../lib/yt2mp3";
|
||||
import { getFilePath } from "./utils";
|
||||
import { HTTPException } from "hono/http-exception";
|
||||
|
||||
type Task = {
|
||||
id: string;
|
||||
path: string;
|
||||
url: string;
|
||||
title: string;
|
||||
thumb: string;
|
||||
length: string;
|
||||
status?: "pending" | "resolved" | "rejected";
|
||||
};
|
||||
|
||||
const tasks = new Map<string, Task>();
|
||||
const queue = new Queue<Task>((input: Task, cb) => {
|
||||
const { id, path, url } = input;
|
||||
|
||||
const task = tasks.get(id);
|
||||
if (!task) return cb(new Error("Task not found!"));
|
||||
|
||||
const out = getFilePath(path);
|
||||
if (!out.path) return cb(new Error("Path not found!"));
|
||||
|
||||
tasks.set(id, { ...task, status: "pending" });
|
||||
|
||||
yt2mp3(url, out.path)
|
||||
.then((data) => cb(null, data))
|
||||
.catch(cb);
|
||||
});
|
||||
|
||||
queue.on("task_finish", (taskId: string) => {
|
||||
const task = tasks.get(taskId);
|
||||
task && tasks.set(taskId, { ...task, status: "resolved" });
|
||||
console.log("task finish!", taskId);
|
||||
});
|
||||
|
||||
queue.on("task_failed", (taskId: string) => {
|
||||
const task = tasks.get(taskId);
|
||||
task && tasks.set(taskId, { ...task, status: "rejected" });
|
||||
console.error("task failed!", taskId);
|
||||
});
|
||||
|
||||
export const ytdl = async (c: Context) => {
|
||||
const input: YtdlSchema = await c.req.json();
|
||||
|
||||
try {
|
||||
const info = await getVideoInfo(input.url);
|
||||
|
||||
const task: Task = {
|
||||
id: nanoid(),
|
||||
url: input.url,
|
||||
title: info.title,
|
||||
path: input.path,
|
||||
thumb: info.thumb?.url,
|
||||
length: info.length,
|
||||
};
|
||||
|
||||
queue.push(task);
|
||||
tasks.set(task.id, task);
|
||||
|
||||
return c.json(task);
|
||||
} catch (err) {
|
||||
throw new HTTPException(400, {
|
||||
message: (err as any)?.message || "Cannot download from youtube!",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getYtdl = async (c: Context) => {
|
||||
const id = c.req.param("id");
|
||||
return c.json(tasks.get(id));
|
||||
};
|
||||
Reference in New Issue
Block a user