2018-04-29 12:47:24 +00:00
/* global swal, axios, Dropzone, ClipboardJS, LazyLoad */
2017-01-14 09:16:47 +00:00
2019-09-01 19:23:16 +00:00
const lsKeys = {
token : 'token' ,
chunkSize : 'chunkSize' ,
2019-09-08 01:56:29 +00:00
parallelUploads : 'parallelUploads' ,
fileLength : 'fileLength' ,
uploadAge : 'uploadAge'
2019-09-01 19:23:16 +00:00
}
2018-10-09 19:52:41 +00:00
const page = {
2018-04-29 12:47:24 +00:00
// user token
2019-09-01 19:23:16 +00:00
token : localStorage [ lsKeys . token ] ,
2018-04-29 12:47:24 +00:00
// configs from api/check
private : null ,
enableUserAccounts : null ,
2019-08-20 02:16:34 +00:00
maxSize : null ,
2018-05-09 08:41:30 +00:00
chunkSize : null ,
2019-09-08 01:56:29 +00:00
temporaryUploadAges : null ,
fileIdentifierLength : null ,
2018-04-29 12:47:24 +00:00
// store album id that will be used with upload requests
album : null ,
2019-09-02 10:24:04 +00:00
parallelUploads : null ,
2019-09-08 01:56:29 +00:00
fileLength : null ,
uploadAge : null ,
2019-09-01 19:23:16 +00:00
maxSizeBytes : null ,
urlMaxSize : null ,
urlMaxSizeBytes : null ,
2019-09-08 01:56:29 +00:00
tabs : null ,
activeTab : null ,
2018-05-06 14:14:57 +00:00
albumSelect : null ,
2018-05-11 14:34:13 +00:00
previewTemplate : null ,
2018-05-06 14:14:57 +00:00
2018-04-29 12:47:24 +00:00
dropzone : null ,
clipboardJS : null ,
2018-10-18 13:26:40 +00:00
lazyLoad : null ,
2018-01-23 20:06:30 +00:00
2019-01-18 01:15:53 +00:00
imageExtensions : [ '.webp' , '.jpg' , '.jpeg' , '.bmp' , '.gif' , '.png' , '.svg' ]
2018-10-18 13:26:40 +00:00
}
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
page . checkIfPublic = function ( ) {
2018-10-09 19:52:41 +00:00
axios . get ( 'api/check' ) . then ( function ( response ) {
page . private = response . data . private
page . enableUserAccounts = response . data . enableUserAccounts
2019-09-01 19:23:16 +00:00
page . maxSize = parseInt ( response . data . maxSize )
page . maxSizeBytes = page . maxSize * 1e6
2019-09-08 01:56:29 +00:00
page . chunkSize = parseInt ( response . data . chunkSize )
page . temporaryUploadAges = response . data . temporaryUploadAges
page . fileIdentifierLength = response . data . fileIdentifierLength
2018-10-09 19:52:41 +00:00
page . preparePage ( )
} ) . catch ( function ( error ) {
2019-09-08 01:56:29 +00:00
console . error ( error )
document . querySelector ( '#albumDiv' ) . style . display = 'none'
document . querySelector ( '#tabs' ) . style . display = 'none'
2019-08-20 02:16:34 +00:00
const button = document . querySelector ( '#loginToUpload' )
2018-10-09 19:52:41 +00:00
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' )
} )
2018-01-23 20:06:30 +00:00
}
2018-07-14 03:42:18 +00:00
page . preparePage = function ( ) {
2018-12-18 17:01:28 +00:00
if ( page . private )
2018-04-29 12:47:24 +00:00
if ( page . token ) {
return page . verifyToken ( page . token , true )
2018-01-24 15:31:23 +00:00
} else {
2019-08-20 02:16:34 +00:00
const button = document . querySelector ( '#loginToUpload' )
2018-03-28 20:05:01 +00:00
button . href = 'auth'
2018-05-06 14:14:57 +00:00
button . classList . remove ( 'is-loading' )
2018-04-03 18:54:42 +00:00
2018-12-18 17:01:28 +00:00
if ( page . enableUserAccounts )
2018-04-29 12:47:24 +00:00
button . innerText = 'Anonymous upload is disabled. Log in to page.'
2018-12-18 17:01:28 +00:00
else
2018-04-29 12:47:24 +00:00
button . innerText = 'Running in private mode. Log in to page.'
2018-01-24 15:31:23 +00:00
}
2018-12-18 17:01:28 +00:00
else
2018-04-29 12:47:24 +00:00
return page . prepareUpload ( )
2018-01-23 20:06:30 +00:00
}
2018-07-14 03:42:18 +00:00
page . verifyToken = function ( token , reloadOnError ) {
2018-12-18 17:01:28 +00:00
if ( reloadOnError === undefined ) reloadOnError = false
2018-01-23 20:06:30 +00:00
2018-10-09 19:52:41 +00:00
axios . post ( 'api/tokens/verify' , { token } ) . then ( function ( response ) {
2018-12-18 17:01:28 +00:00
if ( response . data . success === false )
2018-10-09 19:52:41 +00:00
return swal ( {
title : 'An error occurred!' ,
text : response . data . description ,
icon : 'error'
} ) . then ( function ( ) {
2018-12-18 17:01:28 +00:00
if ( ! reloadOnError ) return
2018-10-09 19:52:41 +00:00
localStorage . removeItem ( 'token' )
location . reload ( )
} )
2018-07-14 03:42:18 +00:00
2018-10-09 19:52:41 +00:00
localStorage . token = token
page . token = token
return page . prepareUpload ( )
} ) . catch ( function ( error ) {
2019-09-08 01:56:29 +00:00
console . error ( error )
2018-10-09 19:52:41 +00:00
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
2018-01-23 20:06:30 +00:00
}
2018-07-14 03:42:18 +00:00
page . prepareUpload = function ( ) {
2018-01-23 20:06:30 +00:00
// I think this fits best here because we need to check for a valid token before we can get the albums
2018-04-29 12:47:24 +00:00
if ( page . token ) {
2019-08-20 02:16:34 +00:00
page . albumSelect = document . querySelector ( '#albumSelect' )
2018-07-14 03:42:18 +00:00
page . albumSelect . addEventListener ( 'change' , function ( ) {
2018-05-06 14:14:57 +00:00
page . album = parseInt ( page . albumSelect . value )
2019-09-08 01:56:29 +00:00
// Re-generate ShareX config file
if ( typeof page . prepareShareX === 'function' )
page . prepareShareX ( )
2018-01-23 20:06:30 +00:00
} )
2018-05-06 14:14:57 +00:00
page . prepareAlbums ( )
// Display the album selection
2019-08-20 02:16:34 +00:00
document . querySelector ( '#albumDiv' ) . style . display = 'flex'
2018-01-23 20:06:30 +00:00
}
2019-09-02 10:24:04 +00:00
page . prepareUploadConfig ( )
2019-09-01 19:23:16 +00:00
document . querySelector ( '#maxSize' ) . innerHTML = ` Maximum upload size per file is ${ page . getPrettyBytes ( page . maxSizeBytes ) } `
2019-08-20 02:16:34 +00:00
document . querySelector ( '#loginToUpload' ) . style . display = 'none'
2018-01-23 20:06:30 +00:00
2018-12-18 17:01:28 +00:00
if ( ! page . token && page . enableUserAccounts )
2019-08-20 02:16:34 +00:00
document . querySelector ( '#loginLinkText' ) . innerHTML = 'Create an account and keep track of your uploads'
2018-01-23 20:06:30 +00:00
2018-10-09 19:52:41 +00:00
const previewNode = document . querySelector ( '#tpl' )
2018-05-11 14:34:13 +00:00
page . previewTemplate = previewNode . innerHTML
previewNode . parentNode . removeChild ( previewNode )
2018-01-23 20:06:30 +00:00
2018-04-29 12:47:24 +00:00
page . prepareDropzone ( )
2018-05-11 14:34:13 +00:00
2019-09-08 01:56:29 +00:00
// Generate ShareX config file
if ( typeof page . prepareShareX === 'function' )
page . prepareShareX ( )
2019-09-02 10:24:04 +00:00
const urlMaxSize = document . querySelector ( '#urlMaxSize' )
if ( urlMaxSize ) {
page . urlMaxSize = parseInt ( urlMaxSize . innerHTML )
page . urlMaxSizeBytes = page . urlMaxSize * 1e6
urlMaxSize . innerHTML = page . getPrettyBytes ( page . urlMaxSizeBytes )
2019-08-20 02:16:34 +00:00
document . querySelector ( '#uploadUrls' ) . addEventListener ( 'click' , function ( ) {
2018-05-11 14:34:13 +00:00
page . uploadUrls ( this )
} )
2018-12-18 17:41:42 +00:00
}
2019-09-02 10:24:04 +00:00
const tabs = document . querySelector ( '#tabs' )
2019-09-08 01:56:29 +00:00
page . tabs = tabs . querySelectorAll ( 'li' )
for ( let i = 0 ; i < page . tabs . length ; i ++ )
page . tabs [ i ] . addEventListener ( 'click' , function ( ) {
2019-09-02 10:24:04 +00:00
page . setActiveTab ( this . dataset . id )
} )
page . setActiveTab ( 'tab-files' )
2019-09-08 01:56:29 +00:00
tabs . style . display = 'flex'
2018-01-23 20:06:30 +00:00
}
2018-07-14 03:42:18 +00:00
page . prepareAlbums = function ( ) {
2018-10-09 19:52:41 +00:00
const option = document . createElement ( 'option' )
2018-05-06 14:14:57 +00:00
option . value = ''
option . innerHTML = 'Upload to album'
option . selected = true
page . albumSelect . appendChild ( option )
2018-10-09 19:52:41 +00:00
axios . get ( 'api/albums' , {
headers : {
token : page . token
}
} ) . then ( function ( response ) {
2018-12-18 17:01:28 +00:00
if ( response . data . success === false )
2018-10-09 19:52:41 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-05-06 14:14:57 +00:00
2018-10-09 19:52:41 +00:00
// If the user doesn't have any albums we don't really need to display
// an album selection
2018-12-18 17:01:28 +00:00
if ( ! response . data . albums . length ) return
2018-10-09 19:52:41 +00:00
// Loop through the albums and create an option for each album
for ( let i = 0 ; i < response . data . albums . length ; i ++ ) {
const album = response . data . albums [ i ]
const option = document . createElement ( 'option' )
option . value = album . id
option . innerHTML = album . name
page . albumSelect . appendChild ( option )
}
} ) . catch ( function ( error ) {
2019-09-08 01:56:29 +00:00
console . error ( error )
2019-07-03 15:56:45 +00:00
const description = error . response . data && error . response . data . description
? error . response . data . description
: 'There was an error with the request, please check the console for more information.'
return swal ( ` ${ error . response . status } ${ error . response . statusText } ` , description , 'error' )
2018-10-09 19:52:41 +00:00
} )
2018-05-06 14:14:57 +00:00
}
2019-09-08 01:56:29 +00:00
page . setActiveTab = function ( tabId ) {
if ( tabId === page . activeTab ) return
for ( let i = 0 ; i < page . tabs . length ; i ++ ) {
const id = page . tabs [ i ] . dataset . id
if ( id === tabId ) {
page . tabs [ i ] . classList . add ( 'is-active' )
document . querySelector ( ` # ${ id } ` ) . style . display = 'block'
2018-05-11 14:34:13 +00:00
} else {
2019-09-08 01:56:29 +00:00
page . tabs [ i ] . classList . remove ( 'is-active' )
document . querySelector ( ` # ${ id } ` ) . style . display = 'none'
2018-05-11 14:34:13 +00:00
}
}
2019-09-08 01:56:29 +00:00
page . activeTab = tabId
2018-05-11 14:34:13 +00:00
}
2018-07-14 03:42:18 +00:00
page . prepareDropzone = function ( ) {
2019-08-20 02:16:34 +00:00
const tabDiv = document . querySelector ( '#tab-files' )
2018-10-09 19:52:41 +00:00
const div = document . createElement ( 'div' )
2018-05-11 14:34:13 +00:00
div . className = 'control is-expanded'
2018-10-09 19:52:41 +00:00
div . innerHTML = `
< div id = "dropzone" class = "button is-danger is-fullwidth is-unselectable" >
< span class = "icon" >
< i class = "icon-upload-cloud" > < / i >
< / s p a n >
2019-09-02 10:24:04 +00:00
< span > Click here or drag & drop files < / s p a n >
2018-10-09 19:52:41 +00:00
< / d i v >
`
2018-10-18 13:26:40 +00:00
tabDiv . querySelector ( '.dz-container' ) . appendChild ( div )
2018-09-04 15:49:37 +00:00
2018-10-18 13:26:40 +00:00
const previewsContainer = tabDiv . querySelector ( '#tab-files .field.uploads' )
2019-08-20 02:16:34 +00:00
2019-09-08 01:56:29 +00:00
page . dropzone = new Dropzone ( document . body , {
2018-01-24 15:31:23 +00:00
url : 'api/upload' ,
2018-01-23 20:06:30 +00:00
paramName : 'files[]' ,
2019-09-08 01:56:29 +00:00
clickable : tabDiv . querySelector ( '#dropzone' ) ,
2019-09-01 19:23:16 +00:00
maxFilesize : page . maxSizeBytes / 1024 / 1024 , // this option expects MiB
2019-09-02 10:24:04 +00:00
parallelUploads : page . parallelUploads ,
2018-01-23 20:06:30 +00:00
uploadMultiple : false ,
2018-10-09 19:52:41 +00:00
previewsContainer ,
2018-05-11 14:34:13 +00:00
previewTemplate : page . previewTemplate ,
2018-01-23 20:06:30 +00:00
createImageThumbnails : false ,
autoProcessQueue : true ,
2018-04-29 12:47:24 +00:00
headers : { token : page . token } ,
2018-05-09 08:41:30 +00:00
chunking : Boolean ( page . chunkSize ) ,
2019-09-02 10:24:04 +00:00
chunkSize : page . chunkSize * 1e6 , // the option below expects Bytes
2019-08-20 02:16:34 +00:00
parallelChunkUploads : false , // when set to true, it often hangs with hundreds of parallel uploads
2018-10-09 19:52:41 +00:00
chunksUploaded ( file , done ) {
2018-03-28 11:36:28 +00:00
file . previewElement . querySelector ( '.progress' ) . setAttribute ( 'value' , 100 )
2018-04-03 18:54:42 +00:00
file . previewElement . querySelector ( '.progress' ) . innerHTML = '100%'
2018-03-28 11:36:28 +00:00
2018-10-09 19:52:41 +00:00
return axios . post ( 'api/upload/finishchunks' , {
2019-08-20 02:16:34 +00:00
// This API supports an array of multiple files
2018-10-09 19:52:41 +00:00
files : [ {
uuid : file . upload . uuid ,
original : file . name ,
type : file . type ,
2019-09-08 01:56:29 +00:00
albumid : page . album ,
filelength : page . fileLength ,
age : page . uploadAge
2018-10-09 19:52:41 +00:00
} ]
} , {
2019-09-08 01:56:29 +00:00
headers : { token : page . token }
} ) . catch ( function ( error ) {
if ( error . response . data ) return error . response
return {
data : {
success : false ,
description : error . toString ( )
}
2018-10-09 19:52:41 +00:00
}
} ) . then ( function ( response ) {
file . previewElement . querySelector ( '.progress' ) . style . display = 'none'
2018-07-14 03:42:18 +00:00
2018-12-18 17:01:28 +00:00
if ( response . data . success === false )
2018-10-09 19:52:41 +00:00
file . previewElement . querySelector ( '.error' ) . innerHTML = response . data . description
2018-07-14 03:42:18 +00:00
2018-12-18 17:01:28 +00:00
if ( response . data . files && response . data . files [ 0 ] )
2018-10-09 19:52:41 +00:00
page . updateTemplate ( file , response . data . files [ 0 ] )
2018-12-18 17:01:28 +00:00
2018-10-09 19:52:41 +00:00
return done ( )
} )
2018-01-23 20:06:30 +00:00
}
} )
2018-07-14 03:42:18 +00:00
page . dropzone . on ( 'addedfile' , function ( file ) {
2019-09-08 01:56:29 +00:00
// Set active tab to file uploads
page . setActiveTab ( 'tab-files' )
// Add file entry
2019-08-20 02:16:34 +00:00
tabDiv . querySelector ( '.uploads' ) . style . display = 'block'
2018-05-11 14:34:13 +00:00
file . previewElement . querySelector ( '.name' ) . innerHTML = file . name
2018-03-28 11:36:28 +00:00
} )
2018-07-14 03:42:18 +00:00
page . dropzone . on ( 'sending' , function ( file , xhr ) {
2018-12-18 17:01:28 +00:00
if ( file . upload . chunked ) return
2019-09-08 01:56:29 +00:00
// Add headers if not uploading chunks
if ( page . album !== null ) xhr . setRequestHeader ( 'albumid' , page . album )
if ( page . fileLength !== null ) xhr . setRequestHeader ( 'filelength' , page . fileLength )
if ( page . uploadAge !== null ) xhr . setRequestHeader ( 'age' , page . uploadAge )
2018-03-28 11:36:28 +00:00
} )
2018-01-23 20:06:30 +00:00
// Update the total progress bar
2018-07-14 03:42:18 +00:00
page . dropzone . on ( 'uploadprogress' , function ( file , progress ) {
2019-08-20 02:16:34 +00:00
// For some reason, chunked uploads fire 100% progress event
// for each chunk's successful uploads
2018-12-18 17:01:28 +00:00
if ( file . upload . chunked && progress === 100 ) return
2018-01-23 20:06:30 +00:00
file . previewElement . querySelector ( '.progress' ) . setAttribute ( 'value' , progress )
2018-10-09 19:52:41 +00:00
file . previewElement . querySelector ( '.progress' ) . innerHTML = ` ${ progress } % `
2018-01-23 20:06:30 +00:00
} )
2018-07-14 03:42:18 +00:00
page . dropzone . on ( 'success' , function ( file , response ) {
2018-12-18 17:01:28 +00:00
if ( ! response ) return
2018-05-11 14:34:13 +00:00
file . previewElement . querySelector ( '.progress' ) . style . display = 'none'
2018-01-23 20:06:30 +00:00
2018-12-18 17:01:28 +00:00
if ( response . success === false )
2018-05-11 14:34:13 +00:00
file . previewElement . querySelector ( '.error' ) . innerHTML = response . description
2018-01-23 20:06:30 +00:00
2018-12-18 17:01:28 +00:00
if ( response . files && response . files [ 0 ] )
2018-04-29 12:47:24 +00:00
page . updateTemplate ( file , response . files [ 0 ] )
2018-03-24 19:47:41 +00:00
} )
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
page . dropzone . on ( 'error' , function ( file , error ) {
2019-09-08 01:56:29 +00:00
// Clean up file size errors
2019-08-20 02:16:34 +00:00
if ( ( typeof error === 'string' && /^File is too big/ . test ( error ) ) ||
2019-09-08 01:56:29 +00:00
( typeof error === 'object' && /File too large/ . test ( error . description ) ) )
2019-09-01 19:23:16 +00:00
error = ` File too large ( ${ page . getPrettyBytes ( file . size ) } ). `
2019-09-08 01:56:29 +00:00
2019-04-18 09:06:14 +00:00
page . updateTemplateIcon ( file . previewElement , 'icon-block' )
2018-05-11 14:34:13 +00:00
file . previewElement . querySelector ( '.progress' ) . style . display = 'none'
file . previewElement . querySelector ( '.name' ) . innerHTML = file . name
2018-11-28 17:52:12 +00:00
file . previewElement . querySelector ( '.error' ) . innerHTML = error . description || error
2018-01-23 20:06:30 +00:00
} )
}
2018-07-14 03:42:18 +00:00
page . uploadUrls = function ( button ) {
2019-08-20 02:16:34 +00:00
const tabDiv = document . querySelector ( '#tab-urls' )
2018-12-18 17:01:28 +00:00
if ( ! tabDiv ) return
2018-05-11 14:34:13 +00:00
2018-12-18 17:01:28 +00:00
if ( button . classList . contains ( 'is-loading' ) ) return
2018-05-11 14:34:13 +00:00
button . classList . add ( 'is-loading' )
2018-09-07 15:02:04 +00:00
function done ( error ) {
2018-12-18 17:01:28 +00:00
if ( error ) swal ( 'An error occurred!' , error , 'error' )
2018-07-14 03:42:18 +00:00
button . classList . remove ( 'is-loading' )
}
2018-09-07 15:02:04 +00:00
function run ( ) {
2019-09-08 01:56:29 +00:00
const headers = {
token : page . token ,
albumid : page . album ,
age : page . uploadAge ,
filelength : page . fileLength
}
2019-08-20 02:16:34 +00:00
const previewsContainer = tabDiv . querySelector ( '.uploads' )
const urls = document . querySelector ( '#urls' ) . value
2018-05-12 22:13:26 +00:00
. split ( /\r?\n/ )
2018-12-18 17:41:42 +00:00
. filter ( function ( url ) {
return url . trim ( ) . length
} )
2019-08-20 02:16:34 +00:00
document . querySelector ( '#urls' ) . value = urls . join ( '\n' )
2018-05-12 22:13:26 +00:00
2018-12-18 17:01:28 +00:00
if ( ! urls . length )
2018-05-12 22:13:26 +00:00
// eslint-disable-next-line prefer-promise-reject-errors
2018-07-14 03:42:18 +00:00
return done ( 'You have not entered any URLs.' )
2018-05-12 22:13:26 +00:00
2019-08-20 02:16:34 +00:00
tabDiv . querySelector ( '.uploads' ) . style . display = 'block'
2018-10-09 19:52:41 +00:00
const files = urls . map ( function ( url ) {
const previewTemplate = document . createElement ( 'template' )
2018-05-11 14:34:13 +00:00
previewTemplate . innerHTML = page . previewTemplate . trim ( )
2018-10-09 19:52:41 +00:00
const previewElement = previewTemplate . content . firstChild
2018-05-11 15:10:49 +00:00
previewElement . querySelector ( '.name' ) . innerHTML = url
previewsContainer . appendChild ( previewElement )
2019-09-08 01:56:29 +00:00
return { url , previewElement }
2018-05-11 14:34:13 +00:00
} )
2018-09-07 15:02:04 +00:00
function post ( i ) {
2019-09-08 01:56:29 +00:00
if ( i === files . length )
return done ( )
2018-07-14 03:42:18 +00:00
2018-09-07 15:02:04 +00:00
function posted ( result ) {
2019-09-08 01:56:29 +00:00
files [ i ] . previewElement . querySelector ( '.progress' ) . style . display = 'none'
2019-04-18 09:06:14 +00:00
if ( result . success ) {
2019-09-08 01:56:29 +00:00
page . updateTemplate ( files [ i ] , result . files [ 0 ] )
2019-04-18 09:06:14 +00:00
} else {
2019-09-08 01:56:29 +00:00
page . updateTemplateIcon ( files [ i ] . previewElement , 'icon-block' )
files [ i ] . previewElement . querySelector ( '.error' ) . innerHTML = result . description
2019-04-18 09:06:14 +00:00
}
2018-07-14 03:42:18 +00:00
return post ( i + 1 )
}
2019-09-08 01:56:29 +00:00
// Animate progress bar
files [ i ] . previewElement . querySelector ( '.progress' ) . removeAttribute ( 'value' )
axios . post ( 'api/upload' , { urls : [ files [ i ] . url ] } , { headers } ) . then ( function ( response ) {
2018-10-09 19:52:41 +00:00
return posted ( response . data )
} ) . catch ( function ( error ) {
return posted ( {
success : false ,
2018-12-08 17:55:04 +00:00
description : error . response ? error . response . data . description : error . toString ( )
2018-05-11 14:34:13 +00:00
} )
2018-10-09 19:52:41 +00:00
} )
2018-05-11 14:34:13 +00:00
}
2018-07-14 03:42:18 +00:00
return post ( 0 )
}
return run ( )
2018-05-11 14:34:13 +00:00
}
2019-04-18 09:06:14 +00:00
page . updateTemplateIcon = function ( templateElement , iconClass ) {
const iconElement = templateElement . querySelector ( '.icon' )
if ( ! iconElement ) return
iconElement . classList . add ( iconClass )
iconElement . style . display = ''
}
2018-07-14 03:42:18 +00:00
page . updateTemplate = function ( file , response ) {
2018-12-18 17:01:28 +00:00
if ( ! response . url ) return
2018-04-29 12:47:24 +00:00
2018-10-09 19:52:41 +00:00
const a = file . previewElement . querySelector ( '.link > a' )
const clipboard = file . previewElement . querySelector ( '.clipboard-mobile > .clipboard-js' )
2019-08-20 02:16:34 +00:00
a . href = a . innerHTML = clipboard . dataset . clipboardText = response . url
2018-04-29 12:47:24 +00:00
clipboard . parentElement . style . display = 'block'
2018-03-28 20:05:01 +00:00
2018-10-09 19:52:41 +00:00
const exec = /.[\w]+(\?|$)/ . exec ( response . url )
2018-10-18 13:26:40 +00:00
if ( exec && exec [ 0 ] && page . imageExtensions . includes ( exec [ 0 ] . toLowerCase ( ) ) ) {
2018-10-09 19:52:41 +00:00
const img = file . previewElement . querySelector ( 'img' )
2018-04-29 12:47:24 +00:00
img . setAttribute ( 'alt' , response . name || '' )
2019-08-20 02:16:34 +00:00
img . dataset . src = response . url
2019-04-18 09:06:14 +00:00
img . style . display = ''
2018-12-18 17:41:42 +00:00
img . onerror = function ( ) {
2019-04-18 09:06:14 +00:00
// Hide image elements that fail to load
// Consequently include WEBP in browsers that do not have WEBP support (Firefox/IE)
2018-12-18 17:41:42 +00:00
this . style . display = 'none'
2019-04-18 09:06:14 +00:00
file . previewElement . querySelector ( '.icon' ) . style . display = ''
2018-12-18 17:41:42 +00:00
}
2018-10-18 13:40:47 +00:00
page . lazyLoad . update ( file . previewElement . querySelectorAll ( 'img' ) )
2019-04-18 09:06:14 +00:00
} else {
page . updateTemplateIcon ( file . previewElement , 'icon-doc-inv' )
2018-03-28 11:36:28 +00:00
}
2019-09-08 01:56:29 +00:00
if ( response . expirydate ) {
const expiryDate = file . previewElement . querySelector ( '.expiry-date' )
expiryDate . innerHTML = ` Expiry date: ${ page . getPrettyDate ( new Date ( response . expirydate * 1000 ) ) } `
expiryDate . style . display = 'block'
}
2018-03-28 11:36:28 +00:00
}
2018-07-14 03:42:18 +00:00
page . createAlbum = function ( ) {
2018-10-09 19:52:41 +00:00
const div = document . createElement ( 'div' )
div . innerHTML = `
< div class = "field" >
< div class = "controls" >
2018-12-13 09:09:46 +00:00
< input id = "swalName" class = "input" type = "text" placeholder = "Name" >
< / d i v >
< / d i v >
< div class = "field" >
< div class = "control" >
< textarea id = "swalDescription" class = "textarea" placeholder = "Description" rows = "2" > < / t e x t a r e a >
2018-10-09 19:52:41 +00:00
< / d i v >
< / d i v >
< div class = "field" >
< div class = "control" >
< label class = "checkbox" >
< input id = "swalDownload" type = "checkbox" checked >
Enable download
< / l a b e l >
< / d i v >
< / d i v >
< div class = "field" >
< div class = "control" >
< label class = "checkbox" >
< input id = "swalPublic" type = "checkbox" checked >
Enable public link
< / l a b e l >
< / d i v >
< / d i v >
`
2018-09-04 15:49:37 +00:00
2018-07-14 03:42:18 +00:00
swal ( {
2018-05-06 14:14:57 +00:00
title : 'Create new album' ,
icon : 'info' ,
content : div ,
buttons : {
cancel : true ,
confirm : {
closeModal : false
}
}
2018-10-09 19:52:41 +00:00
} ) . then ( function ( value ) {
2018-12-18 17:01:28 +00:00
if ( ! value ) return
2018-10-09 19:52:41 +00:00
2019-09-08 01:56:29 +00:00
const name = document . querySelector ( '#swalName' ) . value . trim ( )
2018-10-09 19:52:41 +00:00
axios . post ( 'api/albums' , {
name ,
2019-09-08 01:56:29 +00:00
description : document . querySelector ( '#swalDescription' ) . value . trim ( ) ,
2019-08-20 02:16:34 +00:00
download : document . querySelector ( '#swalDownload' ) . checked ,
public : document . querySelector ( '#swalPublic' ) . checked
2018-10-09 19:52:41 +00:00
} , {
headers : {
token : page . token
}
} ) . then ( function ( response ) {
2018-12-18 17:01:28 +00:00
if ( response . data . success === false )
2018-10-09 19:52:41 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
const option = document . createElement ( 'option' )
2019-08-20 02:16:34 +00:00
page . albumSelect . appendChild ( option )
2018-10-09 19:52:41 +00:00
option . value = response . data . id
option . innerHTML = name
2019-08-20 02:16:34 +00:00
option . selected = true
2018-10-09 19:52:41 +00:00
2019-09-02 10:24:04 +00:00
swal ( 'Woohoo!' , 'Album was created successfully.' , 'success' )
2018-10-09 19:52:41 +00:00
} ) . catch ( function ( error ) {
2019-09-08 01:56:29 +00:00
console . error ( error )
2018-10-09 19:52:41 +00:00
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-07-14 03:42:18 +00:00
} )
2018-10-09 19:52:41 +00:00
} )
2018-05-06 14:14:57 +00:00
}
2019-09-02 10:24:04 +00:00
page . prepareUploadConfig = function ( ) {
const fallback = {
2019-09-08 01:56:29 +00:00
chunkSize : page . chunkSize ,
2019-09-02 10:24:04 +00:00
parallelUploads : 2
}
2019-09-08 01:56:29 +00:00
page . chunkSize = parseInt ( localStorage [ lsKeys . chunkSize ] ) || fallback . chunkSize
page . parallelUploads = parseInt ( localStorage [ lsKeys . parallelUploads ] ) || fallback . parallelUploads
2019-09-02 10:24:04 +00:00
document . querySelector ( '#chunkSize' ) . value = page . chunkSize
document . querySelector ( '#parallelUploads' ) . value = page . parallelUploads
2019-09-08 01:56:29 +00:00
const numConfig = {
chunkSize : { min : 1 , max : 95 } ,
parallelUploads : { min : 1 , max : Number . MAX _SAFE _INTEGER }
}
document . querySelector ( '#chunkSizeDiv .help' ) . innerHTML =
` Default is ${ fallback . chunkSize } MB. Max is ${ numConfig . chunkSize . max } . `
document . querySelector ( '#parallelUploadsDiv .help' ) . innerHTML =
` Default is ${ fallback . parallelUploads } . `
const fileLengthDiv = document . querySelector ( '#fileLengthDiv' )
if ( page . fileIdentifierLength && fileLengthDiv ) {
const element = document . querySelector ( '#fileLength' )
const stored = parseInt ( localStorage [ lsKeys . fileLength ] )
fallback . fileLength = page . fileIdentifierLength . default
let helpText = ` Default is ${ page . fileIdentifierLength . default } . `
const range = typeof page . fileIdentifierLength . min === 'number' &&
typeof page . fileIdentifierLength . max === 'number'
if ( range ) {
helpText += ` Min is ${ page . fileIdentifierLength . min } . Max is ${ page . fileIdentifierLength . max } `
numConfig . fileLength = {
min : page . fileIdentifierLength . min ,
max : page . fileIdentifierLength . max
}
}
if ( page . fileIdentifierLength . force ) {
helpText += ' This option is currently disabled.'
element . disabled = true
}
if ( page . fileIdentifierLength . force ||
isNaN ( stored ) ||
! range ||
stored < page . fileIdentifierLength . min ||
stored > page . fileIdentifierLength . max ) {
element . value = fallback . fileLength
page . fileLength = null
} else {
element . value = stored
page . fileLength = stored
}
fileLengthDiv . style . display = 'block'
fileLengthDiv . querySelector ( '.help' ) . innerHTML = helpText
}
Object . keys ( numConfig ) . forEach ( function ( key ) {
document . querySelector ( ` # ${ key } ` ) . setAttribute ( 'min' , numConfig [ key ] . min )
document . querySelector ( ` # ${ key } ` ) . setAttribute ( 'max' , numConfig [ key ] . max )
} )
const uploadAgeDiv = document . querySelector ( '#uploadAgeDiv' )
if ( Array . isArray ( page . temporaryUploadAges ) && page . temporaryUploadAges . length && uploadAgeDiv ) {
const element = document . querySelector ( '#uploadAge' )
const stored = parseFloat ( localStorage [ lsKeys . uploadAge ] )
for ( let i = 0 ; i < page . temporaryUploadAges . length ; i ++ ) {
const age = page . temporaryUploadAges [ i ]
const option = document . createElement ( 'option' )
option . value = i === 0 ? 'default' : age
option . innerHTML = page . getPrettyUploadAge ( age ) +
( i === 0 ? ' (default)' : '' )
element . appendChild ( option )
if ( age === stored ) {
element . value = option . value
page . uploadAge = stored
}
}
uploadAgeDiv . style . display = 'block'
}
2019-09-02 10:24:04 +00:00
const tabContent = document . querySelector ( '#tab-config' )
const form = tabContent . querySelector ( 'form' )
form . addEventListener ( 'submit' , function ( event ) {
event . preventDefault ( )
} )
const siBytes = localStorage [ lsKeys . siBytes ] !== '0'
if ( ! siBytes ) document . querySelector ( '#siBytes' ) . value = '0'
document . querySelector ( '#saveConfig' ) . addEventListener ( 'click' , function ( ) {
2019-09-08 01:56:29 +00:00
if ( ! form . checkValidity ( ) )
return
const prefKeys = [ 'siBytes' , 'uploadAge' ]
2019-09-02 10:24:04 +00:00
for ( let i = 0 ; i < prefKeys . length ; i ++ ) {
const value = form . elements [ prefKeys [ i ] ] . value
2019-09-08 01:56:29 +00:00
if ( value !== 'default' && value !== fallback [ prefKeys [ i ] ] )
2019-09-02 10:24:04 +00:00
localStorage [ lsKeys [ prefKeys [ i ] ] ] = value
2019-09-08 01:56:29 +00:00
else
localStorage . removeItem ( lsKeys [ prefKeys [ i ] ] )
2019-09-02 10:24:04 +00:00
}
2019-09-08 01:56:29 +00:00
const numKeys = Object . keys ( numConfig )
2019-09-02 10:24:04 +00:00
for ( let i = 0 ; i < numKeys . length ; i ++ ) {
2019-09-08 01:56:29 +00:00
const parsed = parseInt ( form . elements [ numKeys [ i ] ] . value ) || 0
const value = Math . min ( Math . max ( parsed , numConfig [ numKeys [ i ] ] . min ) , numConfig [ numKeys [ i ] ] . max )
2019-09-02 10:24:04 +00:00
if ( value > 0 && value !== fallback [ numKeys [ i ] ] )
localStorage [ lsKeys [ numKeys [ i ] ] ] = value
else
localStorage . removeItem ( lsKeys [ numKeys [ i ] ] )
}
swal ( {
title : 'Woohoo!' ,
2019-09-08 01:56:29 +00:00
text : 'Configuration saved into this browser.' ,
2019-09-02 10:24:04 +00:00
icon : 'success'
} ) . then ( function ( ) {
location . reload ( )
} )
} )
}
2019-09-08 01:56:29 +00:00
page . getPrettyUploadAge = function ( hours ) {
if ( hours === 0 ) {
return 'Permanent'
} else if ( hours < 1 ) {
const minutes = hours * 60
return ` ${ minutes } minute ${ minutes === 1 ? '' : 's' } `
} else if ( hours > 24 ) {
const days = hours / 24
return ` ${ days } day ${ days === 1 ? '' : 's' } `
} else {
return ` ${ hours } hour ${ hours === 1 ? '' : 's' } `
}
}
2018-01-23 18:00:55 +00:00
// Handle image paste event
2018-07-14 03:42:18 +00:00
window . addEventListener ( 'paste' , function ( event ) {
2018-10-09 19:52:41 +00:00
const items = ( event . clipboardData || event . originalEvent . clipboardData ) . items
2019-09-01 19:23:16 +00:00
const index = Object . keys ( items )
for ( let i = 0 ; i < index . length ; i ++ ) {
const item = items [ index [ i ] ]
2018-01-23 20:06:30 +00:00
if ( item . kind === 'file' ) {
2018-10-09 19:52:41 +00:00
const blob = item . getAsFile ( )
const file = new File ( [ blob ] , ` pasted-image. ${ blob . type . match ( /(?:[^/]*\/)([^;]*)/ ) [ 1 ] } ` )
2018-01-23 20:06:30 +00:00
file . type = blob . type
2018-04-29 12:47:24 +00:00
page . dropzone . addFile ( file )
2018-01-23 20:06:30 +00:00
}
}
} )
2018-07-14 03:42:18 +00:00
window . onload = function ( ) {
2018-04-29 12:47:24 +00:00
page . checkIfPublic ( )
2018-03-28 20:05:01 +00:00
2018-04-29 12:47:24 +00:00
page . clipboardJS = new ClipboardJS ( '.clipboard-js' )
2018-03-28 20:05:01 +00:00
2018-07-14 03:42:18 +00:00
page . clipboardJS . on ( 'success' , function ( ) {
2018-03-28 20:05:01 +00:00
return swal ( 'Copied!' , 'The link has been copied to clipboard.' , 'success' )
} )
2018-07-14 03:42:18 +00:00
page . clipboardJS . on ( 'error' , function ( event ) {
2018-03-28 20:05:01 +00:00
console . error ( event )
2018-03-30 02:39:53 +00:00
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' )
2018-03-28 20:05:01 +00:00
} )
2018-04-29 12:47:24 +00:00
2018-10-18 13:26:40 +00:00
page . lazyLoad = new LazyLoad ( {
elements _selector : '.field.uploads img'
} )
2018-07-17 03:21:04 +00:00
2019-08-20 02:16:34 +00:00
document . querySelector ( '#createAlbum' ) . addEventListener ( 'click' , function ( ) {
2018-07-17 03:21:04 +00:00
page . createAlbum ( )
} )
2018-01-23 20:06:30 +00:00
}