downloading files in download-magnetic-uri

This commit is contained in:
Abhishek Sinha 2018-08-25 23:11:41 +05:30
parent 65143ac553
commit 47c3bc61f0
5 changed files with 49 additions and 23 deletions

5
package-lock.json generated
View File

@ -1021,6 +1021,11 @@
"safe-buffer": "^5.0.1"
}
},
"circular-json": {
"version": "0.5.5",
"resolved": "https://registry.npmjs.org/circular-json/-/circular-json-0.5.5.tgz",
"integrity": "sha512-13YaR6kiz0kBNmIVM87Io8Hp7bWOo4r61vkEANy8iH9R9bc6avud/1FT0SBpqR1RpIQADOh/Q+yHZDA1iL6ysA=="
},
"class-utils": {
"version": "0.3.6",
"resolved": "https://registry.npmjs.org/class-utils/-/class-utils-0.3.6.tgz",

View File

@ -8,6 +8,7 @@
"body-parser": "^1.18.3",
"bootstrap": "^4.1.3",
"browserify": "^16.2.2",
"circular-json": "^0.5.5",
"cookie-parser": "^1.4.3",
"drag-drop": "^4.2.0",
"ejs": "^2.6.1",

View File

@ -11,6 +11,8 @@ const funcs = require('./public/js/funcs')
var path = require('path')
var fs = require('fs');
//const JSON = require('circular-json');
router.get('/', (req, res)=>{
res.render('index.ejs', {
data: {},
@ -71,20 +73,19 @@ router.post('/download-magnetic-uri', (req, res)=>{
var length = files.length
// Stream each file to the disk
files.forEach(function (file) {
//console.log(file);
let fullpath = path.resolve(file.path)
//console.log(file._torrent.path);
var source = file.createReadStream()
var destination = fs.createWriteStream(`files/${file.name}`)
source.on('end', function () {
console.log('file:\t\t', file.name)
//console.log(destination);
//res.json({file:file.name, location:fullpath})
res.json({file:file.name, location:fullpath})
// close after all files are saved
if (!--length) process.exit()
let fullpath = path.resolve(`files/${file.name}`)
source.on('end', function () {
console.log('file:\t\t', file.name)
res.json({file:file.name, location:fullpath})
// close after all files are saved
if (!--length) process.exit()
}).pipe(destination)
})

View File

@ -33,18 +33,37 @@
type: 'post',
data: {job:job, torrentId:torrentId},
success: function(response) {
console.log(response);
if (typeof response.file != undefined) {
let file = response.file;
// var a = document.createElement('a')
// a.download = `files/${response.file}`
// //a.href = response.location
// a.href = `/files/${response.file}`
// a.textContent = 'Download ' + response.file
// document.getElementById("dwld").appendChild(a)
console.log('ok');
window.open(response.location);
}
// if (typeof response.file != undefined) {
// let file = response.file;
// var a = document.createElement('a')
// filename = response.file;
// uri = `file://${response.location}`;
// var link = document.createElement('a');
// if (typeof link.download === 'string') {
// console.log("a");
// link.href = uri;
// link.download = filename;
// //Firefox requires the link to be in the body
// document.getElementById("dwld").appendChild(link);
// //simulate click
// link.click();
// //remove the link when done
// document.getElementById("dwld").removeChild(link);
// console.log("aa")
// } else {
// console.log("b");
// window.open(uri);
// }
// }
document.getElementById('dwld').innerHTML = `<a href="file://${response.location}" target="_blank" download onclick="window.open('file://${response.location}')">[Download .torrent]</a>`;
},
error: function(e) {