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 @@ ...@@ -10,6 +10,7 @@
<script src="../lib/renderjs.js" type="text/javascript"></script> <script src="../lib/renderjs.js" type="text/javascript"></script>
<script src="../lib/jszip.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/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> <script src="./gadget_global.js" type="text/javascript"></script>
<!-- Custom --> <!-- Custom -->
...@@ -46,6 +47,18 @@ ...@@ -46,6 +47,18 @@
<button name="load-zip-contents" class="load-zip-contents btn btn-default" type="submit">Import from zip</button> <button name="load-zip-contents" class="load-zip-contents btn btn-default" type="submit">Import from zip</button>
</form> </form>
<div><span class="crib-load-from-zip-status"></span></div> <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>
<div data-gadget-url="./gadget_jio.html" <div data-gadget-url="./gadget_jio.html"
data-gadget-scope="jio_gadget" 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*/ /*jslint nomen: true, indent: 2, maxerr: 3*/
(function (window, rJS, loopEventListener, JSZip, FileSaver) { (function (window, rJS, loopEventListener, JSZip, FileSaver, WebTorrent) {
"use strict"; "use strict";
function getExtension(url) { function getExtension(url) {
...@@ -12,11 +13,16 @@ ...@@ -12,11 +13,16 @@
} }
// Zip Methods // Zip Methods
function saveContentToZIP(gadget, event) { function prepareZip(gadget, event, options) {
var path_to_save, path_to_save_length, application_id, crib_sw_gadget, var path_to_save, path_to_save_length, crib_sw_gadget,
jio_gadget, url_list = [], saved_number = 0, zip; 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; 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; path_to_save_length = path_to_save.length;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -46,6 +52,7 @@ ...@@ -46,6 +52,7 @@
if (url.indexOf(path_to_save) === 0) { if (url.indexOf(path_to_save) === 0) {
saved_number += 1; saved_number += 1;
promise_list.push(gadget.crib_sw_get(url)); promise_list.push(gadget.crib_sw_get(url));
final_url_list.push(url);
} }
} }
return RSVP.all(promise_list); return RSVP.all(promise_list);
...@@ -56,7 +63,7 @@ ...@@ -56,7 +63,7 @@
zip = new JSZip(); zip = new JSZip();
for (i = 0, i_len = response_list.length; i < i_len; i += 1) { for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
response = response_list[i]; 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("//")) { if (url.endsWith("//")) {
url = url.substr(0, url.length - 1); url = url.substr(0, url.length - 1);
} }
...@@ -71,18 +78,37 @@ ...@@ -71,18 +78,37 @@
} }
zip.file(url, response); zip.file(url, response);
} }
return zip.generateAsync({type: "blob"}); return zip.generateAsync({type: options.type});
}) });
.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);
} }
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) { function loadContentFromZIP(gadget, event) {
var path_to_load, path_to_load_length, file_list, crib_sw_gadget, var path_to_load, path_to_load_length, file_list, crib_sw_gadget,
...@@ -161,7 +187,6 @@ ...@@ -161,7 +187,6 @@
if (options === undefined) if (options === undefined)
options = {}; options = {};
gadget.props.options = 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() return new RSVP.Queue()
.push(function () { .push(function () {
return gadget.props.start_deferred.resolve(); return gadget.props.start_deferred.resolve();
...@@ -189,7 +214,13 @@ ...@@ -189,7 +214,13 @@
false, false,
function (event) {loadContentFromZIP(gadget, event); } 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); return RSVP.all(promise_list);
}); });
}); });
}(window, rJS, loopEventListener, JSZip, FileSaver)); }(window, rJS, loopEventListener, JSZip, FileSaver, WebTorrent));
\ No newline at end of file \ 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