using node to download magnet url
This commit is contained in:
parent
abb7175818
commit
52a750f657
1228
package-lock.json
generated
1228
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -21,7 +21,8 @@
|
|||||||
"npm": "^6.3.0",
|
"npm": "^6.3.0",
|
||||||
"popper.js": "^1.14.4",
|
"popper.js": "^1.14.4",
|
||||||
"watchify": "^3.11.0",
|
"watchify": "^3.11.0",
|
||||||
"webtorrent": "^0.102.1"
|
"webtorrent": "^0.102.1",
|
||||||
|
"webtorrent-hybrid": "^1.0.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^1.18.3"
|
"nodemon": "^1.18.3"
|
||||||
|
|||||||
53
routes.js
53
routes.js
@ -3,6 +3,7 @@ const router = express.Router();
|
|||||||
const { check, validationResult } = require('express-validator/check')
|
const { check, validationResult } = require('express-validator/check')
|
||||||
const { matchedData } = require('express-validator/filter')
|
const { matchedData } = require('express-validator/filter')
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const WebtorrentHybrid = require('webtorrent-hybrid')
|
||||||
|
|
||||||
const client = require('./server.js');
|
const client = require('./server.js');
|
||||||
const funcs = require('./public/js/funcs')
|
const funcs = require('./public/js/funcs')
|
||||||
@ -42,6 +43,56 @@ router.get('/download-magnetic-uri', (req, res)=>{
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.post('/download-magnetic-uri', (req, res)=>{
|
||||||
|
|
||||||
|
let params = _.pick(req.body, ['job', 'torrentId'])
|
||||||
|
|
||||||
|
if (params.job != 'download-magnetic-uri') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var client = new WebtorrentHybrid()
|
||||||
|
|
||||||
|
client.on('error', function (err) {
|
||||||
|
console.error('ERROR: ' + err.message)
|
||||||
|
})
|
||||||
|
|
||||||
|
var torrentId = params.torrentId
|
||||||
|
|
||||||
|
if (_.trim(torrentId) == '') {
|
||||||
|
//alert("Please specify a magnetic url.");
|
||||||
|
console.error("Empty torrent id");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
client.add(torrentId, function (torrent) {
|
||||||
|
|
||||||
|
var files = torrent.files
|
||||||
|
var length = files.length
|
||||||
|
// Stream each file to the disk
|
||||||
|
files.forEach(function (file) {
|
||||||
|
console.log(file);
|
||||||
|
|
||||||
|
let fullpath = path.resolve(file.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()
|
||||||
|
}).pipe(destination)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
router.get('/send-to-blockchain', (req, res)=>{
|
router.get('/send-to-blockchain', (req, res)=>{
|
||||||
res.render('to_flo', {
|
res.render('to_flo', {
|
||||||
data: {},
|
data: {},
|
||||||
@ -156,7 +207,7 @@ router.post('/fetch-from-blockchain', [
|
|||||||
|
|
||||||
for (let t = 0; t < lt.length; t++) {
|
for (let t = 0; t < lt.length; t++) {
|
||||||
const elem = lt[t];
|
const elem = lt[t];
|
||||||
if (elem.address==floaddr && elem.category=='send') {
|
if (elem.address==floaddr && elem.category=='receive') {
|
||||||
tx_arr.push(elem.txid)
|
tx_arr.push(elem.txid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,55 +19,41 @@
|
|||||||
|
|
||||||
$(document).on('click', '#dwn-seed', function() {
|
$(document).on('click', '#dwn-seed', function() {
|
||||||
|
|
||||||
var client = new WebTorrent()
|
// var client = new WebTorrent()
|
||||||
|
|
||||||
var torrentId = document.getElementById('mag_text').value;
|
// client.on('error', function (err) {
|
||||||
|
// console.error('ERROR: ' + err.message)
|
||||||
|
// })
|
||||||
|
|
||||||
if (torrentId.trim() == '') {
|
var torrentId = document.getElementById('mag_text').value;
|
||||||
alert("Please specify a magnetic url.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
client.add(torrentId, function (torrent) {
|
if (torrentId.trim() == '') {
|
||||||
// Torrents can contain many files. Let's use the .mp4 file
|
alert("Please specify a magnetic url.");
|
||||||
var file = torrent.files.find(function (file) {
|
return;
|
||||||
console.log(file);
|
}
|
||||||
if (file.name.endsWith('.mp4')) {
|
|
||||||
return file.name.endsWith('.mp4')
|
|
||||||
} else if (file.name.endsWith('.webm')) {
|
|
||||||
return file.name.endsWith('.webm')
|
|
||||||
} else if (file.name.endsWith('.avi')) {
|
|
||||||
return file.name.endsWith('.avi')
|
|
||||||
} else if (file.name.endsWith('.pdf')) {
|
|
||||||
return file.name.endsWith('.pdf')
|
|
||||||
} else if (file.name.endsWith('.mp3')) {
|
|
||||||
return file.name.endsWith('.mp3')
|
|
||||||
} else if (file.name.endsWith('.jpg')) {
|
|
||||||
return file.name.endsWith('.jpg')
|
|
||||||
} else if (file.name.endsWith('.png')) {
|
|
||||||
return file.name.endsWith('.png')
|
|
||||||
} else if (file.name.endsWith('.gif')) {
|
|
||||||
return file.name.endsWith('.gif')
|
|
||||||
} else if (file.name.endsWith('.jpeg')) {
|
|
||||||
return file.name.endsWith('.jpeg')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log('Client is downloading:', torrent.infoHash)
|
var job = 'download-magnetic-uri';
|
||||||
|
|
||||||
// Display the file by adding it to the DOM. Supports video, audio, image, etc. files
|
|
||||||
file.appendTo('#mag-res')
|
|
||||||
|
|
||||||
file.getBlobURL(function (err, url) {
|
$.ajax({
|
||||||
if (err) throw err
|
url: '/download-magnetic-uri',
|
||||||
var a = document.createElement('a')
|
type: 'post',
|
||||||
a.download = file.name
|
data: {job:job, torrentId:torrentId},
|
||||||
a.href = url
|
success: function(response) {
|
||||||
a.textContent = 'Download ' + file.name
|
console.log(response);
|
||||||
document.getElementById("dwld").appendChild(a)
|
if (typeof response.file != undefined) {
|
||||||
})
|
let file = response.file;
|
||||||
|
var a = document.createElement('a')
|
||||||
|
a.download = `files/${response.file}`
|
||||||
|
a.href = response.location
|
||||||
|
a.textContent = 'Download ' + response.file
|
||||||
|
document.getElementById("dwld").appendChild(a)
|
||||||
|
}
|
||||||
|
|
||||||
})
|
},
|
||||||
|
error: function(e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user