filesafe/public/js/home.js
Bobby Wibowo 7991a63315
Updates (please update your config.js)
NOTICE: Please update your config.js. Use config.sample.js as the template.
There were a couple of renames and restructures.

* Album zipper API route will now internally save its state when it's generating zip files, and any subsequent requests will silently be "postponed" until the first spawned task is finished. This will guarantee that there are no multiple zipping tasks for the same album. The method may seem a bit hackish though.

* All instances of console.log(error) were replaced with console.error(error). This will guarantee that any error goes to stderr instead of stdout.

* Deleting file by names will now properly remove successful files from the textarea. There was a logic flaw.

* Failure to generate thumbnails will no longer print the full stack, but instead only the error message. It will also then symlink a template image from /public/images/unavailable.png (it's only a simple image that says that it failed to generate thumbnail).
This haven't been tested in Windows machines, but it'll probably work fine.
I thought of adding a new column to files table which will store information whether the thumbnail generation is sucessful or not, but oh well, I'll go with this method for now.
2018-05-09 15:41:30 +07:00

399 lines
12 KiB
JavaScript

/* global swal, axios, Dropzone, ClipboardJS, LazyLoad */
const page = {
// user token
token: localStorage.token,
// configs from api/check
private: null,
enableUserAccounts: null,
maxFileSize: null,
chunkSize: null,
// store album id that will be used with upload requests
album: null,
albumSelect: null,
dropzone: null,
clipboardJS: null,
lazyLoad: null
}
const imageExtensions = ['.webp', '.jpg', '.jpeg', '.bmp', '.gif', '.png']
page.checkIfPublic = async () => {
const response = await axios.get('api/check')
.catch(error => {
console.log(error)
const button = document.getElementById('loginToUpload')
button.classList.remove('is-loading')
button.innerText = 'Error occurred. Reload the page?'
return swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
})
if (!response) { return }
page.private = response.data.private
page.enableUserAccounts = response.data.enableUserAccounts
page.maxFileSize = response.data.maxFileSize
page.chunkSize = response.data.chunkSize
page.preparePage()
}
page.preparePage = () => {
if (page.private) {
if (page.token) {
return page.verifyToken(page.token, true)
} else {
const button = document.getElementById('loginToUpload')
button.href = 'auth'
button.classList.remove('is-loading')
if (page.enableUserAccounts) {
button.innerText = 'Anonymous upload is disabled. Log in to page.'
} else {
button.innerText = 'Running in private mode. Log in to page.'
}
}
} else {
return page.prepareUpload()
}
}
page.verifyToken = async (token, reloadOnError) => {
if (reloadOnError === undefined) { reloadOnError = false }
const response = await axios.post('api/tokens/verify', { token })
.catch(error => {
console.log(error)
return swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
})
if (!response) { return }
if (response.data.success === false) {
await swal({
title: 'An error occurred!',
text: response.data.description,
icon: 'error'
})
if (reloadOnError) {
localStorage.removeItem('token')
location.reload()
}
return
}
localStorage.token = token
page.token = token
return page.prepareUpload()
}
page.prepareUpload = () => {
// I think this fits best here because we need to check for a valid token before we can get the albums
if (page.token) {
page.albumSelect = document.getElementById('albumSelect')
page.albumSelect.addEventListener('change', () => {
page.album = parseInt(page.albumSelect.value)
})
page.prepareAlbums()
// Display the album selection
document.getElementById('albumDiv').style.display = 'flex'
}
const div = document.createElement('div')
div.id = 'dropzone'
div.className = 'button is-danger is-unselectable'
div.innerHTML = `
<span class="icon">
<i class="icon-upload-cloud"></i>
</span>
<span>Click here or drag and drop files</span>
`
div.style.display = 'flex'
document.getElementById('maxFileSize').innerHTML = `Maximum upload size per file is ${page.maxFileSize}`
document.getElementById('loginToUpload').style.display = 'none'
if (!page.token && page.enableUserAccounts) {
document.getElementById('loginLinkText').innerHTML = 'Create an account and keep track of your uploads'
}
document.getElementById('uploadContainer').appendChild(div)
page.prepareDropzone()
}
page.prepareAlbums = async () => {
const option = document.createElement('option')
option.value = ''
option.innerHTML = 'Upload to album'
option.disabled = true
option.selected = true
page.albumSelect.appendChild(option)
const response = await axios.get('api/albums', { headers: { token: page.token } })
.catch(error => {
console.log(error)
return swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
})
if (!response) { return }
if (response.data.success === false) {
return swal('An error occurred!', response.data.description, 'error')
}
const albums = response.data.albums
// If the user doesn't have any albums we don't really need to display
// an album selection
if (albums.length === 0) { return }
// Loop through the albums and create an option for each album
for (const album of albums) {
const option = document.createElement('option')
option.value = album.id
option.innerHTML = album.name
page.albumSelect.appendChild(option)
}
}
page.prepareDropzone = () => {
const previewNode = document.querySelector('#template')
previewNode.id = ''
const previewTemplate = previewNode.parentNode.innerHTML
previewNode.parentNode.removeChild(previewNode)
page.dropzone = new Dropzone('#dropzone', {
url: 'api/upload',
paramName: 'files[]',
maxFilesize: parseInt(page.maxFileSize),
parallelUploads: 2,
uploadMultiple: false,
previewsContainer: '#uploads',
previewTemplate,
createImageThumbnails: false,
maxFiles: 1000,
autoProcessQueue: true,
headers: { token: page.token },
chunking: Boolean(page.chunkSize),
chunkSize: parseInt(page.chunkSize) * 1000000, // 1000000 B = 1 MB,
parallelChunkUploads: false, // when set to true, sometimes it often hangs with hundreds of parallel uploads
chunksUploaded: async (file, done) => {
file.previewElement.querySelector('.progress').setAttribute('value', 100)
file.previewElement.querySelector('.progress').innerHTML = '100%'
// The API supports an array of multiple files
const response = await axios.post('api/upload/finishchunks',
{
files: [{
uuid: file.upload.uuid,
original: file.name,
size: file.size,
type: file.type,
count: file.upload.totalChunkCount,
albumid: page.album
}]
},
{ headers: { token: page.token } })
.then(response => response.data)
.catch(error => {
return {
success: false,
description: error.toString()
}
})
file.previewTemplate.querySelector('.progress').style.display = 'none'
if (response.success === false) {
file.previewTemplate.querySelector('.error').innerHTML = response.description
}
if (response.files && response.files[0]) {
page.updateTemplate(file, response.files[0])
}
return done()
}
})
page.dropzone.on('addedfile', file => {
document.getElementById('uploads').style.display = 'block'
})
// Add the selected albumid, if an album is selected, as a header
page.dropzone.on('sending', (file, xhr, formData) => {
if (file.upload.chunked) { return }
if (page.album) { xhr.setRequestHeader('albumid', page.album) }
})
// Update the total progress bar
page.dropzone.on('uploadprogress', (file, progress, bytesSent) => {
if (file.upload.chunked && progress === 100) { return }
file.previewElement.querySelector('.progress').setAttribute('value', progress)
file.previewElement.querySelector('.progress').innerHTML = `${progress}%`
})
page.dropzone.on('success', (file, response) => {
if (!response) { return }
file.previewTemplate.querySelector('.progress').style.display = 'none'
if (response.success === false) {
file.previewTemplate.querySelector('.error').innerHTML = response.description
}
if (response.files && response.files[0]) {
page.updateTemplate(file, response.files[0])
}
})
page.dropzone.on('error', (file, error) => {
file.previewTemplate.querySelector('.progress').style.display = 'none'
file.previewTemplate.querySelector('.error').innerHTML = error
})
page.prepareShareX()
}
page.updateTemplate = (file, response) => {
if (!response.url) { return }
const a = file.previewTemplate.querySelector('.link > a')
const clipboard = file.previewTemplate.querySelector('.clipboard-mobile > .clipboard-js')
a.href = a.innerHTML = clipboard.dataset['clipboardText'] = response.url
clipboard.parentElement.style.display = 'block'
const name = file.previewTemplate.querySelector('.name')
name.innerHTML = file.name
const exec = /.[\w]+(\?|$)/.exec(response.url)
if (exec && exec[0] && imageExtensions.includes(exec[0].toLowerCase())) {
const img = file.previewTemplate.querySelector('img')
img.setAttribute('alt', response.name || '')
img.dataset['src'] = response.url
img.onerror = function () { this.style.display = 'none' } // hide webp in firefox and ie
page.lazyLoad.update(file.previewTemplate.querySelectorAll('img'))
}
}
page.prepareShareX = () => {
if (page.token) {
const sharexElement = document.getElementById('ShareX')
const sharexFile =
'{\r\n' +
` "Name": "${location.hostname}",\r\n` +
' "DestinationType": "ImageUploader, FileUploader",\r\n' +
' "RequestType": "POST",\r\n' +
` "RequestURL": "${location.origin}/api/upload",\r\n` +
' "FileFormName": "files[]",\r\n' +
' "Headers": {\r\n' +
` "token": "${page.token}"\r\n` +
' },\r\n' +
' "ResponseType": "Text",\r\n' +
' "URL": "$json:files[0].url$",\r\n' +
' "ThumbnailURL": "$json:files[0].url$"\r\n' +
'}'
const sharexBlob = new Blob([sharexFile], { type: 'application/octet-binary' })
sharexElement.setAttribute('href', URL.createObjectURL(sharexBlob))
sharexElement.setAttribute('download', `${location.hostname}.sxcu`)
}
}
page.createAlbum = async () => {
const div = document.createElement('div')
div.innerHTML = `
<div class="field">
<label class="label">Album name</label>
<div class="controls">
<input id="_name" class="input" type="text" placeholder="My super album">
</div>
</div>
<div class="field">
<div class="control">
<label class="checkbox">
<input id="_download" type="checkbox" checked>
Enable download
</label>
</div>
</div>
<div class="field">
<div class="control">
<label class="checkbox">
<input id="_public" type="checkbox" checked>
Enable public link
</label>
</div>
</div>
`
const value = await swal({
title: 'Create new album',
icon: 'info',
content: div,
buttons: {
cancel: true,
confirm: {
closeModal: false
}
}
})
if (!value) { return }
const name = document.getElementById('_name').value
const response = await axios.post('api/albums', {
name,
download: document.getElementById('_download').checked,
public: document.getElementById('_public').checked
}, { headers: { token: page.token } })
.catch(error => {
console.log(error)
return swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
})
if (!response) { return }
if (response.data.success === false) {
return swal('An error occurred!', response.data.description, 'error')
}
const option = document.createElement('option')
option.value = response.data.id
option.innerHTML = name
page.albumSelect.appendChild(option)
swal('Woohoo!', 'Album was created successfully', 'success')
}
// Handle image paste event
window.addEventListener('paste', event => {
const items = (event.clipboardData || event.originalEvent.clipboardData).items
for (const index in items) {
const item = items[index]
if (item.kind === 'file') {
const blob = item.getAsFile()
console.log(blob.type)
const file = new File([blob], `pasted-image.${blob.type.match(/(?:[^/]*\/)([^;]*)/)[1]}`)
file.type = blob.type
console.log(file)
page.dropzone.addFile(file)
}
}
})
window.onload = () => {
page.checkIfPublic()
page.clipboardJS = new ClipboardJS('.clipboard-js')
page.clipboardJS.on('success', () => {
return swal('Copied!', 'The link has been copied to clipboard.', 'success')
})
page.clipboardJS.on('error', event => {
console.error(event)
return swal('An error occurred!', 'There was an error when trying to copy the link to clipboard, please check the console for more information.', 'error')
})
page.lazyLoad = new LazyLoad()
}