/* global swal, axios */ const lsKeys = { token: 'token' } const page = { // user token token: localStorage[lsKeys.token], urlPrefix: null, urlIdentifier: null, messageElement: document.querySelector('#message'), fileinfoContainer: document.querySelector('#fileinfo'), downloadBtn: document.querySelector('#downloadBtn'), playerBtn: document.querySelector('#playerBtn'), deleteBtn: document.querySelector('#deleteBtn'), uploadRoot: null, titleFormat: null, file: null } page.updateMessageBody = content => { page.messageElement.querySelector('.message-body').innerHTML = content page.messageElement.classList.remove('is-hidden') } // Handler for regular JS errors page.onError = error => { console.error(error) page.updateMessageBody(`
An error occurred!
${error.toString()}
Please check your console for more information.
`) } // Handler for Axios errors page.onAxiosError = error => { // Better Cloudflare errors const cloudflareErrors = { 520: 'Unknown Error', 521: 'Web Server Is Down', 522: 'Connection Timed Out', 523: 'Origin Is Unreachable', 524: 'A Timeout Occurred', 525: 'SSL Handshake Failed', 526: 'Invalid SSL Certificate', 527: 'Railgun Error', 530: 'Origin DNS Error' } const statusText = cloudflareErrors[error.response.status] || error.response.statusText const description = error.response.data && error.response.data.description ? error.response.data.description : '' page.updateMessageBody(`${error.response.status} ${statusText}
${description}
`) } page.deleteFile = () => { if (!page.file) return const content = document.createElement('div') content.innerHTML = 'You won\'t be able to recover this file!
' swal({ title: 'Are you sure?', content, icon: 'warning', dangerMode: true, buttons: { cancel: true, confirm: { text: 'Yes, nuke it!', closeModal: false } } }).then(proceed => { if (!proceed) return axios.post('../api/upload/delete', { id: page.file.id }).then(response => { if (!response) return if (response.data.success === false) { return swal('An error occurred!', response.data.description, 'error') } const failed = Array.isArray(response.data.failed) ? response.data.failed : [] if (failed.length) { swal('An error occurred!', 'Unable to delete this file.', 'error') } else { swal('Deleted!', 'This file has been deleted.', 'success', { buttons: false }) } }).catch(page.onAxiosError) }) } page.loadFileinfo = () => { if (!page.urlIdentifier) return axios.get(`../api/upload/get/${page.urlIdentifier}`).then(response => { if (![200, 304].includes(response.status)) { return page.onAxiosError(response) } page.file = response.data.file if (page.titleFormat) { document.title = page.titleFormat.replace(/%identifier%/g, page.file.name) } let rows = '' const keys = Object.keys(page.file) for (let i = 0; i < keys.length; i++) { const value = page.file[keys[i]] let prettyValue = '' if (value) { if (['size'].includes(keys[i])) { prettyValue = page.getPrettyBytes(value) } else if (['timestamp', 'expirydate'].includes(keys[i])) { prettyValue = page.getPrettyDate(new Date(value * 1000)) } } rows += `Fields | Values |
---|
Failed to parse upload identifier from URL.
') } page.urlIdentifier = match[1] urlPrefix += window.location.pathname.substring(0, window.location.pathname.indexOf(match[1])) page.urlPrefix = urlPrefix // eslint-disable-next-line compat/compat page.urlParams = new URLSearchParams(window.location.search) page.deleteBtn.addEventListener('click', page.deleteFile) page.loadFileinfo() })