it works
This commit is contained in:
32
server/routes/download/[file].get.ts
Normal file
32
server/routes/download/[file].get.ts
Normal file
@@ -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;
|
||||
})
|
||||
Reference in New Issue
Block a user