Skip to content

Commit

Permalink
Merge pull request #27 from jai-dewani/client-side-download
Browse files Browse the repository at this point in the history
Client side download
  • Loading branch information
utkarsh-raj committed Sep 20, 2020
2 parents 5371317 + a7531bf commit b96518f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APICREDENTIAL=your-key-here
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
.env
.env
*.mp4
24 changes: 19 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ app.get("/download/:videoUrl", function(req, res) {
var video = youtube("http://www.youtube.com/watch?v=" + req.params.videoUrl,
["--format=18"],
{cwd: cwd});
console.log(video);
var size, filename;

video.on("info", function(info) {
console.log("Download Started");
if (info.track === null) {
Expand All @@ -61,12 +62,25 @@ app.get("/download/:videoUrl", function(req, res) {
else {
track = String(info.track + ".mp4");
}
console.log(track);
// console.log(track);
size = info.size
filename = info._filename
res.writeHead(200, {
"Content-Disposition": "attachment;filename=" + filename,
'Content-Type': 'video/mp4',
'Content-Length': size
});
});;

video.pipe(fs.createWriteStream(track));
video.on('data',(data)=>{
res.write(data)
})

res.redirect("/started");
});;
video.on('end',(end)=>{
console.log('Download end')

res.end();
})
});

app.get("/started", function(req, res) {
Expand Down
1 change: 0 additions & 1 deletion sample.env

This file was deleted.

0 comments on commit b96518f

Please sign in to comment.