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

206 lines
6.3 KiB
JavaScript
Raw Normal View History

2020-01-28 12:51:21 +01:00
function Toolbar(viewer) {
2021-03-04 17:10:59 +01:00
this.viewer = viewer
this.visible = false
this.sharebarVisible = false
this.currentFile = null
this.editWindow = null
this.views = 0
this.downloads = 0
this.statsWebsocket = 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")
2020-02-04 19:37:46 +01:00
this.btnToggleToolbar = document.getElementById("btn_toggle_toolbar")
2021-03-04 17:10:59 +01:00
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")
2020-02-04 19:37:46 +01:00
this.btnToggleToolbar.addEventListener("click", () => { this.toggle() })
2021-03-04 17:10:59 +01:00
this.btnDownload.addEventListener("click", () => { this.download() })
this.btnCopyLink.addEventListener("click", () => { this.copyUrl() })
this.btnShare.addEventListener("click", () => { this.toggleSharebar() })
2020-02-04 19:37:46 +01:00
}
2021-03-04 17:10:59 +01:00
Toolbar.prototype.setFile = function (file) {
2020-02-04 19:37:46 +01:00
this.currentFile = file
this.spanSize.innerText = formatDataVolume(file.size, 3)
2020-07-07 21:19:16 +02:00
this.setStats()
}
2021-03-04 17:10:59 +01:00
Toolbar.prototype.setStats = function () {
2020-07-07 21:19:16 +02:00
let size = this.currentFile.size
2020-08-28 15:59:40 +02:00
this.spanViews.innerText = "loading..."
this.spanDownloads.innerText = "loading..."
2020-07-07 21:19:16 +02:00
2020-08-28 15:59:40 +02:00
if (this.statsWebsocket !== null) {
this.statsWebsocket.close()
}
2020-07-07 21:38:54 +02:00
2020-08-28 15:59:40 +02:00
this.statsWebsocket = new WebSocket(
2021-03-04 17:10:59 +01:00
location.origin.replace(/^http/, 'ws') + "/api/file/" + this.currentFile.id + "/stats"
2020-08-28 15:59:40 +02:00
)
this.statsWebsocket.onmessage = (msg) => {
let j = JSON.parse(msg.data)
2020-08-28 17:18:02 +02:00
console.debug("WS update", j)
2020-08-28 15:59:40 +02:00
this.views = j.views
2021-03-04 17:10:59 +01:00
this.downloads = Math.round(j.bandwidth / size)
2020-07-07 21:19:16 +02:00
this.spanViews.innerText = formatThousands(this.views)
2020-08-28 15:59:40 +02:00
this.spanDownloads.innerText = formatThousands(this.downloads)
2020-08-28 17:18:02 +02:00
}
this.statsWebsocket.onerror = (err) => {
2021-03-04 17:10:59 +01:00
console.error("WS error", err)
2020-08-28 17:18:02 +02:00
this.statsWebsocket.close()
this.statsWebsocket = null
this.spanViews.innerText = "error"
this.spanDownloads.innerText = "retrying..."
window.setTimeout(() => {
if (this.statsWebsocket === null) {
this.setStats()
}
}, 5000)
2020-08-28 15:59:40 +02:00
}
2020-01-27 16:56:16 +01:00
}
2021-03-04 17:10:59 +01:00
Toolbar.prototype.toggle = function () {
2020-01-28 12:51:21 +01:00
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
2021-03-04 17:10:59 +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
}
2021-03-04 17:10:59 +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
2021-03-04 17:10:59 +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
2021-03-04 17:10:59 +01:00
Toolbar.prototype.download = function () {
if (captchaKey === "none" || captchaKey === "") {
2020-02-18 14:57:27 +01:00
console.debug("Server doesn't support captcha, starting download")
this.downloadFrame.src = this.currentFile.download_href
2020-02-04 19:37:46 +01:00
return
2020-01-20 19:55:51 +01:00
}
2020-02-18 14:57:27 +01:00
if (this.currentFile.availability === "") {
console.debug("File is available, starting download")
this.downloadFrame.src = this.currentFile.download_href
} else {
console.debug("File is not readily available, showing captcha dialog")
let showCaptcha = (title, text) => {
// Create the modal
this.captchaModal = new Modal(
document.getElementById("file_viewer"),
null, title, "500px", "auto",
)
// Clone the popup contents and insert them into the popup
let clone = document.getElementById("tpl_captcha_popup").content.cloneNode(true)
clone.querySelector(".captcha_text").innerText = text
recaptchaElement = clone.querySelector(".captcha_popup_captcha")
this.captchaModal.setBody(clone)
// Set the callback function
recaptchaCallback = token => {
// Download the file using the recaptcha token
2021-03-04 17:10:59 +01:00
this.downloadFrame.src = this.currentFile.download_href + "&recaptcha_response=" + token
2020-02-18 14:57:27 +01:00
this.captchaModal.close()
}
2020-01-27 16:56:16 +01:00
// 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-18 14:57:27 +01:00
this.captchaModal.open()
2020-01-20 19:55:51 +01:00
}
2020-02-18 14:57:27 +01:00
console.log(this.currentFile.availability)
if (this.currentFile.availability === "file_rate_limited_captcha_required") {
console.debug("Showing rate limiting captcha")
showCaptcha(
"Rate limiting enabled!",
2021-03-04 17:10:59 +01:00
"This file is using a suspicious amount of bandwidth relative " +
"to its popularity. To continue downloading this file you " +
2020-02-18 14:57:27 +01:00
"will have to prove that you're a human first.",
)
} else if (this.currentFile.availability === "virus_detected_captcha_required") {
console.debug("Showing virus captcha")
showCaptcha(
"Malware warning!",
2021-03-04 17:10:59 +01:00
"According to our scanning systems this file may contain a " +
"virus of type '" + this.currentFile.availability_name + "'. You " +
"can continue downloading this file at your own risk, but you " +
2020-02-18 14:57:27 +01:00
"will have to prove that you're a human first.",
)
2020-01-21 20:27:34 +01:00
}
2020-02-18 14:57:27 +01:00
}
2020-01-27 16:56:16 +01:00
}
2020-01-20 19:55:51 +01:00
2021-03-04 17:10:59 +01:00
Toolbar.prototype.copyUrl = function () {
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
}
// Called by the google recaptcha script
2021-03-04 17:10:59 +01:00
let recaptchaElement = null
2020-02-18 14:57:27 +01:00
let recaptchaCallback = null
2021-03-04 17:10:59 +01:00
function loadCaptcha() {
2020-02-18 14:57:27 +01:00
grecaptcha.render(recaptchaElement, {
2020-01-20 19:55:51 +01:00
sitekey: captchaKey,
theme: "dark",
2020-02-18 14:57:27 +01:00
callback: recaptchaCallback,
2020-02-04 19:37:46 +01:00
})
2020-01-20 19:55:51 +01:00
}