Commit 55b64dfe authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

Add import torrent from magnet

parent b57bd50a
......@@ -59,6 +59,16 @@
</label>
<textarea name="load-zip-path" class="magnet-link form-control" readonly value="cribeditor/v1.1">Data</textarea>
</div>
<form class="crib-import-torrent form-inline">
<h3>Import from zip Torrent</h3>
<div class="log" style="display:none;">
</div>
<div class="form-group">
<label>Magnet:
<input name="zip-magnet" class="zip-magnet form-control" type="text" size="40"></label>
</div>
<button name="load-zip-magnet" class="load-zip-magnet btn btn-default" type="submit">Import from Torrent</button>
</form>
</div>
<div data-gadget-url="./gadget_jio.html"
data-gadget-scope="jio_gadget"
......
/*global window, rJS, loopEventListener, RSVP, console, document, saveAs, WebTorrent,
File*/
File, setInterval, clearInterval*/
/*jslint nomen: true, indent: 2, maxerr: 3*/
(function (window, rJS, loopEventListener, JSZip, FileSaver, WebTorrent) {
"use strict";
......@@ -12,6 +12,46 @@
return "." + extension;
}
function log(gadget, str) {
var p = document.createElement('p'),
old_p_element = gadget.props.element.querySelector('.log p'),
log_element = gadget.props.element.querySelector('.log');
if (old_p_element) {
old_p_element.remove();
}
p.innerHTML = str;
log_element.style.display = "";
log_element.appendChild(p);
}
function downloadTorrent(gadget, torrentId) {
return RSVP.Promise(function (resolve, reject) {
var client = new WebTorrent();
client.on('error', function (err) {
reject(err.message);
});
client.add(torrentId, function (torrent) {
log(gadget, 'Got torrent metadata!');
log(gadget,
'Torrent info hash: ' + torrent.infoHash + ' ' +
'<a href="' + torrent.magnetURI + '" target="_blank">[Magnet URI]</a> ' +
'<a href="' + torrent.torrentFileBlobURL + '" target="_blank" download="' + torrent.name + '.torrent">[Download .torrent]</a>'
);
// Torrents can contain many files. Here we suppose there is only one zip
var interval = setInterval(function () {
log(gadget, 'Progress: ' + (torrent.progress * 100).toFixed(1) + '%');
}, 5000);
torrent.on('done', function () {
log(gadget, "Done");
resolve(torrent.files);
clearInterval(interval);
});
});
});
}
// Zip Methods
function prepareZip(gadget, event, options) {
var path_to_save, path_to_save_length, crib_sw_gadget,
......@@ -110,21 +150,39 @@
.push(console.log, console.log);
}
function loadContentFromZIP(gadget, event) {
var path_to_load, path_to_load_length, file_list, crib_sw_gadget,
jio_gadget, url_list = [], file, url_number = 0;
path_to_load = gadget.props.element.querySelector('form.crib-load-from-zip .load-zip-path').value;
function loadContentFromZIPFile(gadget, event) {
var file_list, file;
file_list = gadget.props.element.querySelector('form.crib-load-from-zip .load-zip-file').files;
if (file_list.length === 0) {
gadget.props.element.querySelector(".crib-load-from-zip-status")
.textContent = "Please put a Zip at " + Date();
return;
}
path_to_load_length = path_to_load.length;
file = file_list[0];
return loadContentFromZIP(gadget, event, file);
}
function loadContentFromZIPMagnet(gadget, event) {
var magnet;
magnet = gadget.props.element.querySelector('form.crib-import-torrent .zip-magnet').value;
return RSVP.Queue()
.push(function () {
return downloadTorrent(gadget, magnet);
})
.push(function (files) {
return loadContentFromZIP(gadget, event, files[0]);
});
}
function loadContentFromZIP(gadget, event, zip_data) {
var path_to_load, path_to_load_length, file_list, crib_sw_gadget,
jio_gadget, url_list = [], file, url_number = 0;
path_to_load = gadget.props.element.querySelector('form.crib-load-from-zip .load-zip-path').value;
path_to_load_length = path_to_load.length;
return new RSVP.Queue()
.push(function () {
return JSZip.loadAsync(file);
return JSZip.loadAsync(zip_data);
})
.push(function (zip) {
var promise_list = [];
......@@ -212,7 +270,7 @@
gadget.props.element.querySelector(".crib-load-from-zip"),
'submit',
false,
function (event) {loadContentFromZIP(gadget, event); }
function (event) {loadContentFromZIPFile(gadget, event); }
));
promise_list.push(loopEventListener(
gadget.props.element.querySelector(".crib-export-torrent"),
......@@ -220,6 +278,12 @@
false,
function (event) {seedZip(gadget, event); }
));
promise_list.push(loopEventListener(
gadget.props.element.querySelector(".crib-import-torrent"),
'submit',
false,
function (event) {loadContentFromZIPMagnet(gadget, event); }
));
return RSVP.all(promise_list);
});
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment