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

177 lines
5.5 KiB
JavaScript
Raw Normal View History

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