webtor/views/magnetic.ejs
2018-08-15 18:20:21 +05:30

74 lines
2.4 KiB
Plaintext

<%include partials/header.ejs %>
<div class="container">
<label for="mag_text">Provide a magnetic URL below:</label>
<textarea class="form-control" id="mag_text" rows="3"></textarea>
<p><button id="dwn-seed" class="btn btn-primary">View</button></p>
<p id="dwld"></p>
<div class="pd-10">
<div id="mag-res"></div>
</div>
</div>
<%include partials/footer.ejs %>
<script>
$(document).on('click', '#dwn-seed', function() {
var client = new WebTorrent()
var torrentId = document.getElementById('mag_text').value;
if (torrentId.trim() == '') {
alert("Please specify a magnetic url.");
return;
}
client.add(torrentId, function (torrent) {
// Torrents can contain many files. Let's use the .mp4 file
var file = torrent.files.find(function (file) {
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)
// Display the file by adding it to the DOM. Supports video, audio, image, etc. files
file.appendTo('#mag-res')
file.getBlobURL(function (err, url) {
if (err) throw err
var a = document.createElement('a')
a.download = file.name
a.href = url
a.textContent = 'Download ' + file.name
document.getElementById("dwld").appendChild(a)
})
})
});
</script>