Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
crowdozer committed Jan 14, 2024
1 parent ec68e46 commit 258e884
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ pnpm-debug.log*
# might have an old src for reference
src-old/

/public/downloads/*
!/public/downloads/.gitkeep

# lighthouse
.lighthouseci/
41 changes: 40 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"react-dom": "^18.2.0",
"tailwind-merge": "^1.14.0",
"tailwindcss": "^3.3.3",
"tailwindcss-animate": "^1.0.7"
"tailwindcss-animate": "^1.0.7",
"ytdl-core": "^4.11.5"
},
"devDependencies": {
"@lhci/cli": "^0.13.0",
Expand Down Expand Up @@ -64,4 +65,4 @@
"prettier-plugin-tailwindcss"
]
}
}
}
Empty file added public/downloads/.gitkeep
Empty file.
49 changes: 49 additions & 0 deletions src/pages/ytdl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { APIRoute } from 'astro'
import fs from 'fs'
import ytdl from 'ytdl-core'
import crypto from 'crypto'

const sleep = (ms: number) => {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve()
}, ms)
})
}

export const GET: APIRoute = async () => {
const url = 'https://www.youtube.com/watch?v=a5ITNmnS680'
const id = crypto.randomUUID()
const filename = `${id}.mp4`
const target = `./public/downloads/${filename}`

let isRunning = true

console.log('downloading %s', url)
ytdl(url)
.pipe(fs.createWriteStream(target))
.on('finish', () => {
console.log('Download completed!')
isRunning = false
})

while (isRunning) {
console.log('still downloading...')
await sleep(5000)
}

console.log('download complete!')

return new Response(
JSON.stringify({
message: 'your file is ready',
time: new Date(),
}),
{
status: 200,
headers: {
'Content-Disposition': `attachment; filename="${target}"`,
},
},
)
}

0 comments on commit 258e884

Please sign in to comment.