From ab4ceb877be1f7b55f835fa259e1eae1c777d722 Mon Sep 17 00:00:00 2001 From: suricata Date: Fri, 16 Jan 2026 13:45:15 +0000 Subject: [PATCH] it works --- server/routes/convert.post.ts | 39 ++++++++++++++++++++++------ server/routes/download/[file].get.ts | 32 +++++++++++++++++++++++ 2 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 server/routes/download/[file].get.ts diff --git a/server/routes/convert.post.ts b/server/routes/convert.post.ts index a393cfc..86d0fd9 100644 --- a/server/routes/convert.post.ts +++ b/server/routes/convert.post.ts @@ -1,15 +1,38 @@ export default defineEventHandler(async (event) => { // this one requires auth - //const login = fetch("http://127.0.0.1:81/login"); - console.log(await readBody(event)) + const config = await useRuntimeConfig(event) + const body = await readBody(event) const session = await getUserSession(event) - console.log(session) - try { - const userId = session.secure!.user_id; - } - catch { + //console.log(session) + + const userId = session.secure?.user_id; + if (userId == undefined || userId == null) throw createError({ status: 401 }) - } + else if (body.song == undefined) + throw createError({ status: 400, message: "Song name required"}) + + const req = await fetch(config.fastapi_url + "/convert", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + video_url : body.url, + user_id : userId, + artist : body.artist || null, + album : body.album || null, + song : body.song + }) + }) + setResponseStatus(event, req.status) + setResponseHeader(event, "Content-Type", "application/json") + + //console.log(await req.json()) + return await req.json() + + + + }) \ No newline at end of file diff --git a/server/routes/download/[file].get.ts b/server/routes/download/[file].get.ts new file mode 100644 index 0000000..f3674ee --- /dev/null +++ b/server/routes/download/[file].get.ts @@ -0,0 +1,32 @@ +import { createReadStream, existsSync, unlink, unlinkSync } from "node:fs" +import { } from "fs/promises" +import { join } from "path" + +export default defineEventHandler(async (event) => { + const session = await getUserSession(event) + //const query = await getQuery(event) + + // @ts-ignore + const userId = session.secure?.user_id; + if (userId == undefined || userId == null) + throw createError({ status: 401 }) + + const file = await getRouterParam(event, 'file'); + if (file == undefined) + throw createError({ status: 400, message: "No file"}) + + const def_filepath = "/home/suricatingss/Projects/WebStorm/yt2mp3/server/fastapi_server/downloads"; + const filepath = join(def_filepath, userId.toString(), file.toString()) + console.log(filepath) + + if (!existsSync(filepath)) + throw createError({ status: 404, message: "Not found"}) + + setHeader(event, 'Content-Disposition', `attachment; filename="${file}"`) + + const file_stream = createReadStream(filepath) + + file_stream.on("close", () => unlink(filepath, (err) => { if (err) console.warn(err.message) })) + + return file_stream; +}) \ No newline at end of file