Beispiel #1
0
function init () {
  if (!WebTorrent.WEBRTC_SUPPORT) {
    util.error('This browser is unsupported. Please use a browser with WebRTC support.')
  }

  // For performance, create the client immediately
  getClient(function () {})

  // Seed via upload input element
  var upload = document.querySelector('input[name=upload]')
  if (upload) {
    uploadElement(upload, function (err, files) {
      if (err) return util.error(err)
      files = files.map(function (file) { return file.file })
      onFiles(files)
    })
  }

  // Seed via drag-and-drop
  dragDrop('body', onFiles)

  // Download via input element
  var form = document.querySelector('form')
  if (form) {
    form.addEventListener('submit', function (e) {
      e.preventDefault()
      downloadTorrent(document.querySelector('form input[name=torrentId]').value.trim())
    })
  }

  // Download by URL hash
  onHashChange()
  window.addEventListener('hashchange', onHashChange)
  function onHashChange () {
    var hash = decodeURIComponent(window.location.hash.substring(1)).trim()
    if (hash !== '') downloadTorrent(hash)
  }

  // Register a protocol handler for "magnet:" (will prompt the user)
  if ('registerProtocolHandler' in navigator) {
    navigator.registerProtocolHandler('magnet', window.location.origin + '#%s', 'Instant.io')
  }
}
Beispiel #2
0
    client.on('error', util.warning)
    cb(null, client)
  })
})

getClient(function (err, client) {
  if (err) return util.error(err)
  window.client = client
})

upload(document.querySelector('input[name=upload]'), function (err, results) {
  if (err) return util.error(err)
  var files = results.map(function (r) {
    var buf = toBuffer(r.target.result)
    buf.name = r.file.name
    buf.size = r.file.size
    buf.lastModifiedDate = r.file.lastModifiedDate
    buf.type = r.file.type
    return buf
  })
  onFiles(files)
})

dragDrop('body', onFiles)

function onFiles (files) {
  // .torrent file = start downloading the torrent
  files.filter(isTorrent).forEach(downloadTorrent)

  // everything else = seed these files
  seed(files.filter(isNotTorrent))
}
Beispiel #3
0
var dragDrop = require('drag-drop/buffer')
var toBuffer = require('typedarray-to-buffer')
var upload = require('upload-element')
var prettysize = require('prettysize')
var WebTorrent = require('webtorrent')

var client = new WebTorrent()

upload(document.querySelector('input[name=upload]'), { type: 'array' }, onFile)

function onFile (err, results) {
  var files = results.map(function (r) {
    var buf = toBuffer(new Uint8Array(r.target.result))
    buf.name = r.file.name
    buf.size = r.file.size
    buf.lastModifiedDate = r.file.lastModifiedDate
    buf.type = r.file.type
    return buf
  })
  client.seed(files, onTorrent)
}

dragDrop('body', function (files) {
  client.seed(files, onTorrent)
})

function onTorrent (torrent) {
  logAppend('Thanks for seeding!')
  logAppend('Torrent info hash: ' + torrent.infoHash + ' <a href="https://instant.io/#' + torrent.infoHash + '" target="_blank">(link)</a>')
  logAppend('Progress: starting...')
Beispiel #4
0
    var client = new WebTorrent({ rtcConfig: rtcConfig })
    client.on('warning', util.warning)
    client.on('error', util.error)
    cb(null, client)
  })
})

getClient(function (err, client) {
  if (err) return util.error(err)
  window.client = client
})

var upload = document.querySelector('input[name=upload]')
uploadElement(upload, function (err, files) {
  if (err) return util.error(err)
  files = files.map(function (file) { return file.file })
  onFiles(files)
})

dragDrop('body', onFiles)

function onFiles (files) {
  // .torrent file = start downloading the torrent
  files.filter(isTorrent).forEach(downloadTorrent)

  // everything else = seed these files
  seed(files.filter(isNotTorrent))
}

function isTorrent (file) {
  var extname = path.extname(file.name)