Commit 338d627c authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

Add WebTorrent for experimentation export is functional

parent 521cc215
......@@ -10,6 +10,7 @@
<script src="../lib/renderjs.js" type="text/javascript"></script>
<script src="../lib/jszip.js" type="text/javascript"></script>
<script src="../lib/FileSaver.js" type="text/javascript"></script>
<script src="../lib/webtorrent.min.js" type="text/javascript"></script>
<script src="./gadget_global.js" type="text/javascript"></script>
<!-- Custom -->
......@@ -46,6 +47,18 @@
<button name="load-zip-contents" class="load-zip-contents btn btn-default" type="submit">Import from zip</button>
</form>
<div><span class="crib-load-from-zip-status"></span></div>
<h3>Share as Torrent</h3>
<form class="crib-export-torrent form-inline">
<div class="form-group">
<button name="submit-crib-export-torrent" class="export-torrent btn btn-default" type="submit">Export Torrent</button>
</div>
</form>
<div class="magnet-link" style="display:none;">
<p>This Magnet will be valid as long as this page stays open</p>
<label> Magnet:
</label>
<textarea name="load-zip-path" class="magnet-link form-control" readonly value="cribeditor/v1.1">Data</textarea>
</div>
</div>
<div data-gadget-url="./gadget_jio.html"
data-gadget-scope="jio_gadget"
......
/*global window, rJS, loopEventListener, RSVP, console, document, saveAs */
/*global window, rJS, loopEventListener, RSVP, console, document, saveAs, WebTorrent,
File*/
/*jslint nomen: true, indent: 2, maxerr: 3*/
(function (window, rJS, loopEventListener, JSZip, FileSaver) {
(function (window, rJS, loopEventListener, JSZip, FileSaver, WebTorrent) {
"use strict";
function getExtension(url) {
......@@ -12,11 +13,16 @@
}
// Zip Methods
function saveContentToZIP(gadget, event) {
var path_to_save, path_to_save_length, application_id, crib_sw_gadget,
jio_gadget, url_list = [], saved_number = 0, zip;
function prepareZip(gadget, event, options) {
var path_to_save, path_to_save_length, crib_sw_gadget,
jio_gadget, url_list = [], final_url_list = [], saved_number = 0, zip;
if (options === undefined) {
options = {};
}
if (options.type === undefined) {
options.type = "blob";
}
path_to_save = gadget.props.element.querySelector('form.crib-save-to-zip .save-zip-path').value;
application_id = gadget.props.element.querySelector('form.crib-save-to-zip .save-zip-id').value;
path_to_save_length = path_to_save.length;
return new RSVP.Queue()
.push(function () {
......@@ -46,6 +52,7 @@
if (url.indexOf(path_to_save) === 0) {
saved_number += 1;
promise_list.push(gadget.crib_sw_get(url));
final_url_list.push(url);
}
}
return RSVP.all(promise_list);
......@@ -56,7 +63,7 @@
zip = new JSZip();
for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
response = response_list[i];
url = url_list[i].substr(path_to_save_length);
url = final_url_list[i].substr(path_to_save_length);
if (url.endsWith("//")) {
url = url.substr(0, url.length - 1);
}
......@@ -71,18 +78,37 @@
}
zip.file(url, response);
}
return zip.generateAsync({type: "blob"});
})
.push(function (content) {
return saveAs(content, application_id);
})
.push(function () {
gadget.props.element.querySelector(".crib-save-to-zip-status")
.textContent = "Saved " + saved_number + " files at " + Date();
})
.push(console.log, console.log);
return zip.generateAsync({type: options.type});
});
}
function saveContentToZIP(gadget, event) {
return prepareZip(gadget, event)
.push(function (content) {
var application_id = gadget.props.element
.querySelector('form.crib-save-to-zip .save-zip-id').value;
return saveAs(content, application_id);
})
.push(function () {
gadget.props.element.querySelector(".crib-save-to-zip-status")
.textContent = "Saved " + "X" + " files at " + Date();
})
.push(console.log, console.log);
}
function seedZip(gadget, event) {
return prepareZip(gadget, event)
.push(function (result) {
gadget.client = new WebTorrent();
gadget.client.seed(new File([result], "data.zip"), function (torrent) {
gadget.props.element.querySelector("div.magnet-link").style.display = "";
gadget.props.element
.querySelector("textarea.magnet-link").textContent = torrent.magnetURI;
console.log('Client is seeding ' + torrent.magnetURI);
});
})
.push(console.log, console.log);
}
function loadContentFromZIP(gadget, event) {
var path_to_load, path_to_load_length, file_list, crib_sw_gadget,
......@@ -161,7 +187,6 @@
if (options === undefined)
options = {};
gadget.props.options = options;
gadget.props.element.querySelector('form.crib-save-to-zip .save-zip-path').value = document.location.origin;
return new RSVP.Queue()
.push(function () {
return gadget.props.start_deferred.resolve();
......@@ -189,7 +214,13 @@
false,
function (event) {loadContentFromZIP(gadget, event); }
));
promise_list.push(loopEventListener(
gadget.props.element.querySelector(".crib-export-torrent"),
'submit',
false,
function (event) {seedZip(gadget, event); }
));
return RSVP.all(promise_list);
});
});
}(window, rJS, loopEventListener, JSZip, FileSaver));
\ No newline at end of file
}(window, rJS, loopEventListener, JSZip, FileSaver, WebTorrent));
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
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