initial commit

Este commit está contenido en:
ale
2022-10-14 23:13:16 +02:00
commit d601f329e8
Se han modificado 9 ficheros con 265 adiciones y 0 borrados

15
public/css/main.css Archivo normal
Ver fichero

@@ -0,0 +1,15 @@
body {
margin: 0 auto;
text-align: center;
}
.holder {
border: dotted #404040 3px;
border-radius: 25px;
min-height: 150px;
padding: 2rem;
margin: 2rem;
vertical-align: middle;
}
.white-border {
border: solid black 3px;
}

29
public/index.html Archivo normal
Ver fichero

@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>WebTorrent</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/FileSaver.min.js"></script>
<script type="text/javascript" src="js/webtorrent.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<h2>WebTorrent</h2>
<h3 id="status">Not Supported</h3>
<div id="holder" class="holder white-border">Drag&Drop files here to share</div>
<div id="title"></div>
<div><span id="progress">0</span>% of <span id="size">0</span>Mb</div>
<h4><span id="peers">0</span> <span class="fa fa-users"></span><br /><br /><span id="downloading">0</span>Kbps - <span
id="downloaded">0</span>Mb <span class="fa fa-arrow-down"></span><br /><br /><span id="uploading">0</span>Kbps -
<span id="uploaded">0</span>Mb <span class="fa fa-arrow-up"></span>
</h4>
<p><button class="btn btn-primary" onclick="downloadTorrent(window.torrent);"><span class="fa fa-download"></span> .torrent</button></p>
<div><a id="hash" target="_blank"></a></div>
</body>
</html>

136
public/js/main.js Archivo normal
Ver fichero

@@ -0,0 +1,136 @@
var downloadTorrent = function (torrent) {
saveAs(torrent.torrentFileBlobURL, torrent.name + '.torrent')
}
$(document).ready(function () {
var trackers = [
'ws://0.0.0.0:8888'
'http://0.0.0.0:8888/announce'
],
iceServers = [
{ urls: ['turn:???.com', 'stun:???.com'], username: '???', credential: '???' }
],
holder = $('.holder'),
input = $('.input'),
file = $('.file')
function torrentEvents(torrent, seeding) {
torrent.on('metadata', function () {
$('#title').text(torrent.name)
$('#hash').text('/#' + torrent.infoHash)
$('#hash').attr('href', '/#' + torrent.infoHash)
$('#size').text(Math.round((torrent.length / 1024 / 1024) * 100) / 100)
})
torrent.on('ready', function () {
$('#holder').empty()
window.torrent = torrent
})
torrent.on('download', function (bytes) {
$('#status').text('Downloading')
updateTorrent(torrent)
$('#progress').text(Math.round(torrent.progress * 10000) / 100 || '0')
})
torrent.on('wire', function (wire) {
$('#peers').text(torrent.numPeers || '0')
})
torrent.on('done', function () {
$('#status').text('Downloaded')
torrent.files.forEach(function (file) {
file.appendTo('#holder', { autoplay: true, mute: true }, function (err, elem) {
if (err) {
$('#status').text(err)
} else {
$('#status').text('Content loaded')
}
})
})
updateTorrent(torrent)
$('#downloading').text('0')
$('#uploading').text('0')
if (!seeding) {
torrent.files.forEach(function (data) {
data.getBlobURL(function (err, url) {
saveAs(url, data.name)
})
})
}
})
torrent.on('upload', function (data) {
$('#status').text('Uploading')
updateTorrent(torrent)
})
torrent.on('noPeers', function () {
$('#status').text('No Peers')
updateTorrent(torrent)
})
}
function seed(files) {
var client = new WebTorrent({
tracker: WebTorrent.WEBRTC_SUPPORT ? {
rtcConfig: {
iceServers: iceServers
}
} : false
}),
torrent = client.seed(files, {
announce: trackers
})
torrentEvents(torrent, true)
}
function updateTorrent(torrent) {
$('#downloading').text(Math.round(torrent.downloadSpeed / 10) / 100 || '0')
$('#downloaded').text(Math.round((torrent.downloaded / 1024 / 1024) * 100) / 100)
$('#uploading').text(Math.round(torrent.uploadSpeed / 10) / 100 || '0')
$('#uploaded').text(Math.round((torrent.uploaded / 1024 / 1024) * 100) / 100)
$('#peers').text(torrent.numPeers || 0)
}
holder.on('drop', function (event) {
event.preventDefault()
event.stopPropagation()
holder.removeClass('white-border')
if (event.originalEvent.dataTransfer.files) {
var files = event.originalEvent.dataTransfer.files
if (files.length > 1) {
var names = []
for (var i = 0; i < files.length; i++) {
names.push(files[i].name)
}
$('#title').text(names.join(', '))
}
else {
$('#title').text(files[0].name)
}
seed(files)
}
return false
})
.on('dragover', function (event) {
event.preventDefault()
event.stopPropagation()
holder.addClass('white-border')
return false
})
.on('dragleave', function (event) {
event.preventDefault()
event.stopPropagation()
holder.removeClass('white-border')
return false
})
if (!window.FileReader || !WebTorrent.WEBRTC_SUPPORT) {
$('#status').text('WebTorrent NOT Ready')
} else {
$('#status').text('WebTorrent Ready')
$('#holder').text('Drag&Drop files here to share')
if (window.location.hash.substr(1)) {
var client = new WebTorrent({
tracker: WebTorrent.WEBRTC_SUPPORT ? {
rtcConfig: {
iceServers: iceServers
}
} : false
}),
torrent = client.add(window.location.hash.substr(1), {
announce: trackers
})
torrentEvents(torrent)
}
}
})