Commit 66841d75 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

Export Page: Add export page to handle download and share

parent 9b79fd6e
......@@ -31,11 +31,18 @@
pattern: app_list[i].application_path
}),
gadget.getUrlFor({
page: "save_load",
page: "export",
crib_enable_url: app_list[i].application_crib,
zip_download_path: app_list[i].application_path,
zip_name: app_list[i].application_title + ".zip",
start_zip_download: true
}),
gadget.getUrlFor({
page: "export",
crib_enable_url: app_list[i].application_crib,
zip_download_path: app_list[i].application_path,
zip_name: app_list[i].application_title + ".zip",
start_seeding: true
})
]));
}
......@@ -66,6 +73,9 @@
]),
domsugar("a", {href: url_list[i][1], class:"btn btn-default",
role:"button", text:"", title: "Download"}),
domsugar("a", {href: url_list[i][2], class:"btn btn-default",
role:"button", text:"🏹",
title: "Share"}),
domsugar("a", {href: url_list[i][0], class:"btn btn-default", role:"button", text:"Edit"}),
domsugar("a", {href: tmp_url , class:"btn btn-primary", role:"button", text:"GO", target: "_blank"})
......
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<title>CribJS Header</title>
<!-- renderjs -->
<script src="../lib/rsvp.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/mimetype.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 -->
<script src="./gadget_cribjs_page_export.js" type="text/javascript"></script>
</head>
<body>
<div class="nav_content save_load container">
<h3>Export: <a class="url-list-scope" href=""></a></h3>
<form class="crib-save-to-zip form-inline">
<h3>Download Zip</h3>
<div class="info crib-save-to-zip-status" style="display:none; background-color: #dff0d8;"></div>
<div class="info crib-save-to-zip-link" style="display:none;">
<p><a href="">Download your zip</a></p>
</div>
<div class="form-group">
<label>Path to Export:
<input class="save-zip-path form-control"
name="save-zip-path"
type="text" size="30" value=""
placeholder="Leave Empty to Export everything"></label>
<button name="save-zip-contents" type="submit" class="btn btn-default">Export to Zip</button>
</div>
</form>
<h3>Share Torrent</h3>
<div class="crib-export-to-zip-torrent-status"
style="display:none; background-color: #dff0d8;"></div>
<form class="crib-export-torrent form-inline">
<div class="form-group">
<label>Path to Export:
<input class="torrent-save-zip-path form-control"
name="torrent-save-zip-path"
type="text" size="30" value=""
placeholder="Leave Empty to Export everything"></label>
<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>
<p>Send this link for people to import your app directly.</p>
<div class="form-group">
<label>Share Link:
</label>
<a href="" style="overflow-wrap: anywhere;"></a>
</div>
<div class="form-group">
<label>Magnet:
</label>
<textarea name="load-zip-path" class="magnet-link form-control" readonly value="">Data</textarea>
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
/*global window, rJS, loopEventListener, RSVP, console, document, WebTorrent,
File, setInterval, clearInterval, mimeType, URL*/
/*jslint nomen: true, indent: 2, maxerr: 3*/
(function (window, rJS, loopEventListener, JSZip, WebTorrent,
mimeType) {
"use strict";
var CRIBJS_URL = "https://cribjs.nexedi.net/";
function log(gadget, str, klass) {
var p = document.createElement('p'),
old_p_element = gadget.props.element.querySelector(klass + ' p'),
log_element = gadget.props.element.querySelector(klass);
if (old_p_element) {
old_p_element.remove();
}
p.innerHTML = str;
log_element.style.display = "";
log_element.appendChild(p);
}
function logExportZipFile(gadget, str) {
return log(gadget, str, ".crib-save-to-zip-status");
}
function logExportZipTorrent(gadget, str) {
return log(gadget, str, ".crib-export-to-zip-torrent-status");
}
// Zip Methods
function prepareZip(gadget, event, options, path_to_save, logFunction) {
var path_to_save_length, crib_sw_gadget,
url_list = [], final_url_list = [], saved_number = 0, zip;
if (options === undefined) {
options = {};
}
if (options.type === undefined) {
options.type = "blob";
}
path_to_save_length = path_to_save.length;
logFunction(gadget, "Preparing");
return new RSVP.Queue()
.push(function () {
return gadget.crib_sw_allDocs({cached_only: true});
})
.push(function (data) {
var promise_list = [],
i, i_len, url;
if (data.hasOwnProperty("urls")) {
data = data.urls;
} else {
data = data;
}
if (Array.isArray(data)) {
url_list = data;
} else {
url_list = Object.keys(data);
}
for (i = 0, i_len = url_list.length; i < i_len; i += 1) {
url = String(url_list[i]);
if (url.indexOf(path_to_save) === 0) {
saved_number += 1;
promise_list.push(gadget.crib_sw_get(url));
final_url_list.push(url);
}
}
logFunction(gadget, "Retrieving " + saved_number + " Files");
return RSVP.all(promise_list);
})
.push(function (response_list) {
var promise_list = [],
i, i_len, url, response, extension, zip;
zip = new JSZip();
for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
response = response_list[i];
url = final_url_list[i].substr(path_to_save_length);
if (url.endsWith("//")) {
url = url.substr(0, url.length - 1);
}
if (url.endsWith("/./")) {
url = url.substr(0, url.length - 2);
}
if (url.endsWith("/")) {
url = url + "index.html";
}
if (url.startsWith("./")) {
url = url.substr(1);
}
zip.file(url, response);
}
logFunction(gadget, "Preparing Zip");
return zip.generateAsync({type: options.type});
});
}
function formatSizeUnits(bytes) {
if (bytes >= 1073741824) { bytes = (bytes / 1073741824).toFixed(2) + " GB"; }
else if (bytes >= 1048576) { bytes = (bytes / 1048576).toFixed(2) + " MB"; }
else if (bytes >= 1024) { bytes = (bytes / 1024).toFixed(2) + " KB"; }
else if (bytes > 1) { bytes = bytes + " bytes"; }
else if (bytes == 1) { bytes = bytes + " byte"; }
else { bytes = "0 bytes"; }
return bytes;
}
function saveContentToZIP(gadget, event) {
var path_to_save = gadget.props.element
.querySelector('form.crib-save-to-zip .save-zip-path').value;
gadget.props.element.querySelector(".crib-save-to-zip-link").style.display = "none";
return prepareZip(gadget, event, {}, path_to_save, logExportZipFile)
.push(function (content) {
logExportZipFile(
gadget,
"Saved a " + formatSizeUnits(content.size) + " zip file at " + Date()
);
gadget.props.element.querySelector(".crib-save-to-zip-link a")
.href = URL.createObjectURL(content);
gadget.props.element.querySelector(".crib-save-to-zip-link").style.display = "";
})
.push(console.log, console.log);
}
function displayCribLink(gadget, magnet_uri) {
new RSVP.Queue()
.push(function () {
return gadget.getUrlFor({
page: "add_application",
magnet_link: magnet_uri
});
})
.push(function (url) {
var link = gadget.props.element.querySelector("div.magnet-link a");
url = CRIBJS_URL + url;
link.href = url;
link.innerHTML = url;
});
}
function seedZip(gadget, event) {
var path_to_save = gadget.props.element
.querySelector('form.crib-export-torrent .torrent-save-zip-path')
.value;
return prepareZip(gadget, event, {}, path_to_save, logExportZipTorrent)
.push(function (result) {
logExportZipTorrent(
gadget,
"Sharing a " + formatSizeUnits(result.size) + " zip file at " + Date()
);
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);
displayCribLink(gadget, torrent.magnetURI);
});
});
}
rJS(window)
.ready(function (g) {
g.props = {};
return g.getElement()
.push(function (element) {
g.props.element = element;
g.props.start_deferred = RSVP.defer();
});
})
.declareAcquiredMethod("crib_sw_allDocs", "crib_sw_allDocs")
.declareAcquiredMethod("crib_sw_get", "crib_sw_get")
.declareAcquiredMethod("getUrlFor", "getUrlFor")
.declareAcquiredMethod(
"crib_sw_setCribEnableGadgetUrl",
"crib_sw_setCribEnableGadgetUrl"
)
.declareAcquiredMethod("crib_sw_getScope", "crib_sw_getScope")
.declareMethod('render', function (options) {
var gadget = this;
if (options === undefined)
options = {};
gadget.props.options = options;
return new RSVP.Queue()
.push(function () {
if (gadget.props.options.crib_enable_url !== undefined) {
return gadget.crib_sw_setCribEnableGadgetUrl(
gadget.props.options.crib_enable_url
);
}
return;
})
.push(function () {
return gadget.crib_sw_getScope();
})
.push(function (scope) {
var url_list_scope_link;
url_list_scope_link = gadget.props.element
.querySelector("a.url-list-scope");
url_list_scope_link.href = scope;
url_list_scope_link.innerHTML = scope
.split("//")[1].split(".")[0];
return gadget.props.start_deferred.resolve();
});
})
.declareService(function () {
var gadget = this;
return new RSVP.Queue()
.push(function () {
return gadget.props.start_deferred.promise;
})
.push(function () {
if (gadget.props.options.zip_download_path) {
gadget.props.element
.querySelector('form.crib-save-to-zip .save-zip-path')
.value = gadget.props.options.zip_download_path;
}
if (gadget.props.options.start_zip_download) {
return saveContentToZIP(gadget, undefined);
} else if (gadget.props.options.start_seeding) {
return seedZip(gadget, undefined);
}
});
})
.declareService(function () {
var gadget = this;
return new RSVP.Queue()
.push(function () {
return gadget.props.start_deferred.promise;
})
.push(function () {
var promise_list = [];
// promise to save content to ZIP
promise_list.push(loopEventListener(
gadget.props.element.querySelector(".crib-save-to-zip"),
'submit',
false,
function (event) {saveContentToZIP(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, WebTorrent, mimeType));
\ No newline at end of file
......@@ -22,6 +22,7 @@
<li><a class="jslint" href="#page=jslint">JSLint</a></li>
<li><a class="mass_remove" href="#page=mass_remove">Remove</a> uncessary URLs</li>
<li><a class="select_site" href="#page=select_site">Select Site to Edit</a></li>
<li><a class="export" href="#page=save_load">Export/Share</a> current App.</li>
<li><a class="save_load" href="#page=save_load">Import and Export</a> Data. You can even share it with WebTorrent.</li>
<li><a class="crib_loader" href="#page=crib_loader">Update Crib or Load</a> your own.</li>
</ul>
......
......@@ -24,7 +24,7 @@
.declareMethod('render', function (options) {
var gadget = this,
page_list = ["select_site", "jslint", "mass_remove", "grep",
"save_load", "crib_loader"],
"save_load", "crib_loader", "export"],
promise_list = [];
page_list.forEach(function (page) {
......
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