Files
fnx_web/res/include/script/file_viewer/Toolbar.js

178 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-01-20 19:55:51 +01:00
class Toolbar {
constructor(viewer) {let t = this;
2020-01-22 19:48:58 +01:00
t.viewer = viewer;
t.visible = false;
t.sharebarVisible = false;
2020-01-20 19:55:51 +01:00
t.divToolbar = document.getElementById("toolbar");
t.divFilePreview = document.getElementById("filepreview");
t.downloadFrame = document.getElementById("download_frame");
t.spanViews = document.getElementById("stat_views");
t.spanDownloads = document.getElementById("stat_downloads");
2020-01-21 17:01:26 +01:00
t.spanSize = document.getElementById("stat_size");
2020-01-20 19:55:51 +01:00
t.btnToggleToolbar = document.getElementById("btn_toggle_toolbar");
t.btnDownload = document.getElementById("btn_download");
t.btnCopyLink = document.getElementById("btn_copy");
t.spanCopyLink = document.querySelector("#btn_copy > span");
t.btnShare = document.getElementById("btn_share");
t.divSharebar = document.getElementById("sharebar");
t.btnToggleToolbar.addEventListener("click", () => { t.toggle(); });
t.btnDownload.addEventListener("click", () => { t.download(); });
t.btnCopyLink.addEventListener("click", () => { t.copyUrl(); });
t.btnShare.addEventListener("click", () => { t.toggleSharebar(); });
}
toggle() {let t = this;
if (t.visible) {
if (t.sharebarVisible) { t.toggleSharebar(); }
t.divToolbar.style.left = "-8em";
t.divFilePreview.style.left = "0px";
t.btnToggleToolbar.classList.remove("button_highlight");
t.visible = false;
} else {
t.divToolbar.style.left = "0px";
t.divFilePreview.style.left = "8em";
t.btnToggleToolbar.classList.add("button_highlight");
t.visible = true;
}
}
toggleSharebar(){let t = this;
if (navigator.share) {
navigator.share({
title: t.viewer.title,
text: "Download " + t.viewer.title + " here",
url: window.location.href
});
return;
}
if(t.sharebarVisible){
t.divSharebar.style.left = "-8em";
t.btnShare.classList.remove("button_highlight")
t.sharebarVisible = false;
}else{
t.divSharebar.style.left = "8em";
t.btnShare.classList.add("button_highlight")
t.sharebarVisible = true;
}
}
download() {let t = this;
2020-01-21 20:27:34 +01:00
let triggerDL = (captchaResp = "") => {
if (captchaResp === "") {
t.downloadFrame.src = apiEndpoint+"/file/"+
t.viewer.currentFile+"?download";
} else {
t.downloadFrame.src = apiEndpoint+"/file/"+
t.viewer.currentFile+"?download&recaptcha_response="+captchaResp;
}
2020-01-20 19:55:51 +01:00
}
2020-01-21 20:27:34 +01:00
if (captchaKey === "none"){
2020-01-20 19:55:51 +01:00
// If the server doesn't support captcha there's no use in checking
// availability
triggerDL();
return;
}
2020-01-21 20:27:34 +01:00
if (recaptchaResponse !== "") {
// Captcha already filled in. Use the saved captcha responsse to
// download the file
triggerDL(recaptchaResponse);
// Reset the key
recaptchaResponse = "";
return;
}
2020-01-20 19:55:51 +01:00
fetch(apiEndpoint+"/file/"+t.viewer.currentFile+"/availability").then(resp => {
return resp.json();
}).then(resp => {
let popupDiv = document.getElementById("captcha_popup");
let popupTitle = document.getElementById("captcha_popup_title");
let popupContent = document.getElementById("captcha_popup_content");
2020-01-21 20:27:34 +01:00
let showCaptcha = () => {
2020-01-20 19:55:51 +01:00
// Load the recaptcha script with a load function
2020-01-21 20:27:34 +01:00
let script = document.createElement("script");
2020-01-20 19:55:51 +01:00
script.src = "https://www.google.com/recaptcha/api.js?onload=loadCaptcha&render=explicit";
2020-01-21 20:27:34 +01:00
document.body.appendChild(script);
// Show the popup
2020-01-20 19:55:51 +01:00
popupDiv.style.opacity = "1";
popupDiv.style.visibility = "visible";
}
if (resp.value === "file_rate_limited_captcha_required") {
popupTitle.innerText = "Rate limiting enabled!";
popupContent.innerText = "This file is using a suspicious "+
"amount of bandwidth relative to its popularity. To "+
"continue downloading this file you will have to "+
"prove that you're a human first.";
showCaptcha();
} else if (resp.value === "virus_detected_captcha_required") {
popupTitle.innerText = "Malware warning!";
popupContent.innerText = "According to our scanning "+
"systems this file may contain a virus of type '"+
resp.extra+"'. You can continue downloading this file at "+
"your own risk, but you will have to prove that you're a "+
"human first.";
showCaptcha();
} else {
2020-01-21 20:27:34 +01:00
console.warn("resp.value not valid: "+resp.value);
2020-01-20 19:55:51 +01:00
triggerDL();
}
}).catch(e => {
2020-01-21 20:27:34 +01:00
console.warn("fetch availability failed: "+e);
2020-01-20 19:55:51 +01:00
triggerDL();
});
}
copyUrl() {let t = this;
if(copyText(window.location.href)) {
console.log('Text copied');
t.spanCopyLink.innerText = "Copied!";
t.btnCopyLink.classList.add("button_highlight")
} else {
console.log('Copying not supported');
t.spanCopyLink.innerText = "Error!";
alert("Your browser does not support copying text.");
}
// Return to normal
2020-01-21 20:27:34 +01:00
setTimeout(() => {
2020-01-20 19:55:51 +01:00
t.spanCopyLink.innerText = "Copy";
t.btnCopyLink.classList.remove("button_highlight")
}, 60000);
}
2020-01-21 17:01:26 +01:00
setStats(file) {let t = this;
t.spanViews.innerText = file.views
t.spanDownloads.innerText = Math.round((file.bandwidth_used/file.size)*10)/10;
t.spanSize.innerText = formatDataVolume(file.size, 3);
2020-01-20 19:55:51 +01:00
}
}
// Called by the google recaptcha script
2020-01-21 20:27:34 +01:00
let recaptchaResponse = "";
2020-01-20 19:55:51 +01:00
function loadCaptcha(){
grecaptcha.render("captcha_popup_captcha", {
sitekey: captchaKey,
theme: "dark",
2020-01-21 20:27:34 +01:00
callback: token => {
recaptchaResponse = token;
document.getElementById("btn_download").click();
2020-01-20 19:55:51 +01:00
2020-01-21 20:27:34 +01:00
// Hide the popup
setTimeout(() => {
2020-01-20 19:55:51 +01:00
let popupDiv = document.getElementById("captcha_popup");
popupDiv.style.opacity = "0";
popupDiv.style.visibility = "hidden";
}, 1000)
}
});
}