Skip to content

Instantly share code, notes, and snippets.

@Tercus
Created April 4, 2016 23:41
Show Gist options
  • Save Tercus/5603efdafcb99eb005c4bec21eecae97 to your computer and use it in GitHub Desktop.
Save Tercus/5603efdafcb99eb005c4bec21eecae97 to your computer and use it in GitHub Desktop.
'use strict';
module.exports = {
load: function (request, reply) {
const template = require('../template.js')
const WebTorrent = require('webtorrent-hybrid')
const fs = require('fs')
const sqlite3 = require('sqlite3').verbose()
var client = new WebTorrent()
var file = 'test.db'
var db = new sqlite3.Database(file)
var parseTorrent = require('parse-torrent')
console.log(request.method)
if(request.method === 'get') {
reply (template.filled('upload', {}))
} else {
var download = request.payload
console.log('infoHash of torrent to download: ' + download)
var opts = {
path: './storage/' + download + '/',
announce: ['https://localhost:8080', 'udp://localhost:8080', 'ws://localhost:8080']
}
//var magnet = parseTorrent.toMagnetURI({ infoHash: download, announce: ['https://localhost:8080', 'udp://localhost:8080', 'ws://localhost:8080'] })
//console.log(magnet)
//client.add(magnet, function (torrent) {
client.add(download, opts, function (torrent) {
console.log('added torrent')
torrent.files.forEach(function (file) {
console.log('Started saving ' + file.name)
file.getBuffer(function (err, buffer) {
if (err) {
console.error('Error downloading ' + file.name)
return
}
fs.writeFile(file.name, buffer, function (err) {
console.error('Error saving ' + file.name)
})
})
})
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment