diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9dc6a71 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +[*] +end_of_line = lf +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore index 2b04ba1..5c95e05 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,4 @@ pages/custom/** migrate.js yarn.lock package-lock.json -.vscode/ \ No newline at end of file +.vscode/ diff --git a/LICENSE b/LICENSE index e6b240d..4aac64d 100644 --- a/LICENSE +++ b/LICENSE @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/config.sample.js b/config.sample.js index cf8ee4a..b32441f 100644 --- a/config.sample.js +++ b/config.sample.js @@ -81,4 +81,4 @@ module.exports = { connection: { filename: './database/db' }, useNullAsDefault: true } -} +}; diff --git a/lolisafe.js b/lolisafe.js index cf8aab6..f4c3315 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -12,11 +12,11 @@ const safe = express(); require('./database/db.js')(db); -fs.existsSync('./pages/custom' ) || fs.mkdirSync('./pages/custom'); -fs.existsSync('./' + config.logsFolder) || fs.mkdirSync('./' + config.logsFolder); -fs.existsSync('./' + config.uploads.folder) || fs.mkdirSync('./' + config.uploads.folder); -fs.existsSync('./' + config.uploads.folder + '/thumbs') || fs.mkdirSync('./' + config.uploads.folder + '/thumbs'); -fs.existsSync('./' + config.uploads.folder + '/zips') || fs.mkdirSync('./' + config.uploads.folder + '/zips') +fs.existsSync('./pages/custom') || fs.mkdirSync('./pages/custom'); +fs.existsSync(`./${config.logsFolder}`) || fs.mkdirSync(`./${ config.logsFolder}`); +fs.existsSync(`./${config.uploads.folder}`) || fs.mkdirSync(`./${config.uploads.folder}`); +fs.existsSync(`./${config.uploads.folder}/thumbs`) || fs.mkdirSync(`./${config.uploads.folder }/thumbs`); +fs.existsSync(`./${config.uploads.folder }/zips`) || fs.mkdirSync(`./${config.uploads.folder}/zips`); safe.use(helmet()); safe.set('trust proxy', 1); diff --git a/pages/auth.html b/pages/auth.html index 3b56ca1..85a38f4 100644 --- a/pages/auth.html +++ b/pages/auth.html @@ -38,16 +38,17 @@ - + + - -
diff --git a/pages/dashboard.html b/pages/dashboard.html index b864ab6..9739dee 100644 --- a/pages/dashboard.html +++ b/pages/dashboard.html @@ -38,7 +38,90 @@ - + + diff --git a/public/css/style.css b/public/css/style.css index ac3959d..8f5e857 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -41,7 +41,7 @@ section#home div#dropzone { padding-left: .75em; padding-right: .75em; text-align: center; - cursor: pointer; + cursor: pointer; } section#home div#uploads, section#home p#tokenContainer, section#home a#panel { display: none; } @@ -110,3 +110,40 @@ section#dashboard div#table div.column a img { width:200px; } text-align: center; margin-bottom: 10px; } + +/** Based on KDE Breeze Dark **/ + +.hero { + background-color: #232629; + color: #eff0f1; +} + +.title { + color: #eff0f1; +} + +.subtitle { + color: #bdc3c7; +} + +.subtitle strong { + color: #bdc3c7; +} + +a { + color: #2980b9; +} + +a:hover { + color: #3daee9; +} + +section#home h3#links span { + color: #4d4d4d; +} + +section#home #b { + width: 200px; + height: 200px; + border-radius: 100%; +} diff --git a/public/js/auth.js b/public/js/auth.js index ecc7c7c..56eea32 100644 --- a/public/js/auth.js +++ b/public/js/auth.js @@ -1,56 +1,44 @@ var page = {}; -page.do = function(dest){ - +page.do = function(dest) { var user = document.getElementById('user').value; var pass = document.getElementById('pass').value; - if(user === undefined || user === null || user === '') - return swal('Error', 'You need to specify a username', 'error'); - if(pass === undefined || pass === null || pass === '') - return swal('Error', 'You need to specify a username', 'error'); + if (user === undefined || user === null || user === '') { return swal('Error', 'You need to specify a username', 'error'); } + if (pass === undefined || pass === null || pass === '') { return swal('Error', 'You need to specify a username', 'error'); } - axios.post('/api/' + dest, { + axios.post(`/api/${dest}`, { username: user, password: pass }) - .then(function (response) { + .then(response => { + if (response.data.success === false) { return swal('Error', response.data.description, 'error'); } - if(response.data.success === false) - return swal('Error', response.data.description, 'error'); - - localStorage.token = response.data.token; - window.location = '/dashboard'; + localStorage.token = response.data.token; + window.location = '/dashboard'; + }) + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; - }) - .catch(function (error) { - return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); - console.log(error); - }); -} - -page.verify = function(){ +page.verify = function() { page.token = localStorage.token; - if(page.token === undefined) return; + if (page.token === undefined) return; - axios.post('/api/tokens/verify', { - token: page.token - }) - .then(function (response) { + axios.post('/api/tokens/verify', { token: page.token }) + .then(response => { + if (response.data.success === false) { return swal('Error', response.data.description, 'error'); } - if(response.data.success === false) - return swal('Error', response.data.description, 'error'); - - window.location = '/dashboard'; + window.location = '/dashboard'; + }) + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; - }) - .catch(function (error) { - return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); - console.log(error); - }); - -} - -window.onload = function () { +window.onload = function() { page.verify(); -} \ No newline at end of file +}; diff --git a/public/js/dashboard.js b/public/js/dashboard.js index 7309908..3238f7b 100644 --- a/public/js/dashboard.js +++ b/public/js/dashboard.js @@ -1,104 +1,95 @@ -let panel = {} +let panel = {}; panel.page; panel.username; panel.token = localStorage.token; panel.filesView = localStorage.filesView; -panel.preparePage = function(){ - if(!panel.token) return window.location = '/auth'; +panel.preparePage = function() { + if (!panel.token) return window.location = '/auth'; panel.verifyToken(panel.token, true); -} +}; -panel.verifyToken = function(token, reloadOnError){ - if(reloadOnError === undefined) - reloadOnError = false; +panel.verifyToken = function(token, reloadOnError) { + if (reloadOnError === undefined) { reloadOnError = false; } - axios.post('/api/tokens/verify', { - token: token - }) - .then(function (response) { + axios.post('/api/tokens/verify', { token: token }) + .then(response => { + if (response.data.success === false) { + swal({ + title: 'An error ocurred', + text: response.data.description, + type: 'error' + }, () => { + if (reloadOnError) { + localStorage.removeItem('token'); + location.location = '/auth'; + } + }); + return; + } - if(response.data.success === false){ - swal({ - title: "An error ocurred", - text: response.data.description, - type: "error" - }, function(){ - if(reloadOnError){ - localStorage.removeItem("token"); - location.location = '/auth'; - } - }) - return; - } + axios.defaults.headers.common.token = token; + localStorage.token = token; + panel.token = token; + panel.username = response.data.username; + return panel.prepareDashboard(); + }) + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; - axios.defaults.headers.common['token'] = token; - localStorage.token = token; - panel.token = token; - panel.username = response.data.username; - return panel.prepareDashboard(); - - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - console.log(error); - }); - -} - -panel.prepareDashboard = function(){ +panel.prepareDashboard = function() { panel.page = document.getElementById('page'); document.getElementById('auth').style.display = 'none'; document.getElementById('dashboard').style.display = 'block'; - document.getElementById('itemUploads').addEventListener('click', function(){ + document.getElementById('itemUploads').addEventListener('click', function() { panel.setActiveMenu(this); }); - document.getElementById('itemManageGallery').addEventListener('click', function(){ + document.getElementById('itemManageGallery').addEventListener('click', function() { panel.setActiveMenu(this); }); - document.getElementById('itemTokens').addEventListener('click', function(){ + document.getElementById('itemTokens').addEventListener('click', function() { panel.setActiveMenu(this); }); - document.getElementById('itemPassword').addEventListener('click', function(){ + document.getElementById('itemPassword').addEventListener('click', function() { panel.setActiveMenu(this); }); document.getElementById('itemLogout').innerHTML = `Logout ( ${panel.username} )`; panel.getAlbumsSidebar(); -} +}; -panel.logout = function(){ - localStorage.removeItem("token"); +panel.logout = function() { + localStorage.removeItem('token'); location.reload('/'); -} +}; -panel.getUploads = function(album = undefined, page = undefined){ +panel.getUploads = function(album = undefined, page = undefined) { + if (page === undefined) page = 0; - if(page === undefined) page = 0; + let url = `/api/uploads/${page}`; + if (album !== undefined) { url = `/api/album/${album}/${page}`; } - let url = '/api/uploads/' + page - if(album !== undefined) - url = '/api/album/' + album + '/' + page - - axios.get(url).then(function (response) { - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else return swal("An error ocurred", response.data.description, "error"); + axios.get(url).then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else return swal('An error ocurred', response.data.description, 'error'); } - + var prevPage = 0; var nextPage = page + 1; - if(response.data.files.length < 25) - nextPage = page; - - if(page > 0) prevPage = page - 1; + if (response.data.files.length < 25) { nextPage = page; } + + if (page > 0) prevPage = page - 1; panel.page.innerHTML = ''; var container = document.createElement('div'); @@ -120,10 +111,9 @@ panel.getUploads = function(album = undefined, page = undefined){
-
` - - if(panel.filesView === 'thumbs'){ + `; + if (panel.filesView === 'thumbs') { container.innerHTML = ` ${pagination}
@@ -137,23 +127,15 @@ panel.getUploads = function(album = undefined, page = undefined){ panel.page.appendChild(container); var table = document.getElementById('table'); - for(var item of response.data.files){ - + for (var item of response.data.files) { var div = document.createElement('div'); - div.className = "column is-2"; - if(item.thumb !== undefined) - div.innerHTML = ``; - else - div.innerHTML = `

.${item.file.split('.').pop()}

`; + div.className = 'column is-2'; + if (item.thumb !== undefined) { div.innerHTML = ``; } else { div.innerHTML = `

.${item.file.split('.').pop()}

`; } table.appendChild(div); - } - - }else{ - + } else { var albumOrUser = 'Album'; - if(panel.username === 'root') - albumOrUser = 'User'; + if (panel.username === 'root') { albumOrUser = 'User'; } container.innerHTML = ` ${pagination} @@ -178,17 +160,15 @@ panel.getUploads = function(album = undefined, page = undefined){ panel.page.appendChild(container); var table = document.getElementById('table'); - for(var item of response.data.files){ - + for (var item of response.data.files) { var tr = document.createElement('tr'); var displayAlbumOrUser = item.album; - if(panel.username === 'root'){ + if (panel.username === 'root') { displayAlbumOrUser = ''; - if(item.username !== undefined) - displayAlbumOrUser = item.username; + if (item.username !== undefined) { displayAlbumOrUser = item.username; } } - + tr.innerHTML = ` ${item.file} @@ -208,66 +188,57 @@ panel.getUploads = function(album = undefined, page = undefined){ } } }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - console.log(error); - }); + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; -} - -panel.setFilesView = function(view, album, page){ +panel.setFilesView = function(view, album, page) { localStorage.filesView = view; panel.filesView = view; panel.getUploads(album, page); -} +}; -panel.deleteFile = function(id){ +panel.deleteFile = function(id) { swal({ - title: "Are you sure?", - text: "You wont be able to recover the file!", - type: "warning", + title: 'Are you sure?', + text: 'You wont be able to recover the file!', + type: 'warning', showCancelButton: true, - confirmButtonColor: "#ff3860", - confirmButtonText: "Yes, delete it!", + confirmButtonColor: '#ff3860', + confirmButtonText: 'Yes, delete it!', closeOnConfirm: false }, - function(){ - - axios.post('/api/upload/delete', { - id: id - }) - .then(function (response) { - - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else return swal("An error ocurred", response.data.description, "error"); + () => { + axios.post('/api/upload/delete', { id: id }) + .then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else return swal('An error ocurred', response.data.description, 'error'); } - swal("Deleted!", "The file has been deleted.", "success"); + swal('Deleted!', 'The file has been deleted.', 'success'); panel.getUploads(); - return; - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); console.log(error); }); - - } + } ); -} +}; -panel.getAlbums = function(){ - - axios.get('/api/albums').then(function (response) { - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else return swal("An error ocurred", response.data.description, "error"); +panel.getAlbums = function() { + axios.get('/api/albums').then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else return swal('An error ocurred', response.data.description, 'error'); } panel.page.innerHTML = ''; var container = document.createElement('div'); - container.className = "container"; + container.className = 'container'; container.innerHTML = `

Create new album

@@ -295,8 +266,7 @@ panel.getAlbums = function(){ panel.page.appendChild(container); var table = document.getElementById('table'); - for(var item of response.data.albums){ - + for (var item of response.data.albums) { var tr = document.createElement('tr'); tr.innerHTML = ` @@ -322,182 +292,153 @@ panel.getAlbums = function(){ table.appendChild(tr); } - document.getElementById('submitAlbum').addEventListener('click', function(){ + document.getElementById('submitAlbum').addEventListener('click', () => { panel.submitAlbum(); }); - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - console.log(error); - }); + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; -} - -panel.renameAlbum = function(id){ - +panel.renameAlbum = function(id) { swal({ - title: "Rename album", - text: "New name you want to give the album:", - type: "input", + title: 'Rename album', + text: 'New name you want to give the album:', + type: 'input', showCancelButton: true, closeOnConfirm: false, - animation: "slide-from-top", - inputPlaceholder: "My super album" - },function(inputValue){ + animation: 'slide-from-top', + inputPlaceholder: 'My super album' + }, inputValue => { if (inputValue === false) return false; - if (inputValue === "") { - swal.showInputError("You need to write something!"); - return false + if (inputValue === '') { + swal.showInputError('You need to write something!'); + return false; } - + axios.post('/api/albums/rename', { id: id, name: inputValue }) - .then(function (response) { - - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else if(response.data.description === 'Name already in use') swal.showInputError("That name is already in use!"); - else swal("An error ocurred", response.data.description, "error"); - return; - } - - swal("Success!", "Your album was renamed to: " + inputValue, "success"); - panel.getAlbumsSidebar(); - panel.getAlbums(); - return; - - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - console.log(error); - }); - - }); - -} - -panel.deleteAlbum = function(id){ - swal({ - title: "Are you sure?", - text: "This won't delete your files, only the album!", - type: "warning", - showCancelButton: true, - confirmButtonColor: "#ff3860", - confirmButtonText: "Yes, delete it!", - closeOnConfirm: false - }, - function(){ - - axios.post('/api/albums/delete', { - id: id - }) - .then(function (response) { - - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else return swal("An error ocurred", response.data.description, "error"); + .then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else if (response.data.description === 'Name already in use') swal.showInputError('That name is already in use!'); + else swal('An error ocurred', response.data.description, 'error'); + return; } - swal("Deleted!", "Your album has been deleted.", "success"); + swal('Success!', `Your album was renamed to: ${inputValue}`, 'success'); panel.getAlbumsSidebar(); panel.getAlbums(); - return; - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); console.log(error); }); - - } - ); - -} - -panel.submitAlbum = function(){ - - axios.post('/api/albums', { - name: document.getElementById('albumName').value - }) - .then(function (response) { - - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else return swal("An error ocurred", response.data.description, "error"); - } - - swal("Woohoo!", "Album was added successfully", "success"); - panel.getAlbumsSidebar(); - panel.getAlbums(); - return; - - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - console.log(error); }); +}; -} +panel.deleteAlbum = function(id) { + swal({ + title: 'Are you sure?', + text: "This won't delete your files, only the album!", + type: 'warning', + showCancelButton: true, + confirmButtonColor: '#ff3860', + confirmButtonText: 'Yes, delete it!', + closeOnConfirm: false + }, + () => { + axios.post('/api/albums/delete', { id: id }) + .then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else return swal('An error ocurred', response.data.description, 'error'); + } -panel.getAlbumsSidebar = function(){ - - axios.get('/api/albums/sidebar') - .then(function (response) { - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else return swal("An error ocurred", response.data.description, "error"); - } - - var albumsContainer = document.getElementById('albumsContainer'); - albumsContainer.innerHTML = ''; - - if(response.data.albums === undefined) return; - - for(var album of response.data.albums){ - - li = document.createElement('li'); - a = document.createElement('a'); - a.id = album.id; - a.innerHTML = album.name; - - a.addEventListener('click', function(){ - panel.getAlbum(this); + swal('Deleted!', 'Your album has been deleted.', 'success'); + panel.getAlbumsSidebar(); + panel.getAlbums(); + }) + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); }); + } + ); +}; - li.appendChild(a); - albumsContainer.appendChild(li); - } +panel.submitAlbum = function() { + axios.post('/api/albums', { name: document.getElementById('albumName').value }) + .then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else return swal('An error ocurred', response.data.description, 'error'); + } + swal('Woohoo!', 'Album was added successfully', 'success'); + panel.getAlbumsSidebar(); + panel.getAlbums(); + }) + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - console.log(error); - }); +panel.getAlbumsSidebar = function() { + axios.get('/api/albums/sidebar') + .then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else return swal('An error ocurred', response.data.description, 'error'); + } -} + var albumsContainer = document.getElementById('albumsContainer'); + albumsContainer.innerHTML = ''; -panel.getAlbum = function(item){ + if (response.data.albums === undefined) return; + + for (var album of response.data.albums) { + li = document.createElement('li'); + a = document.createElement('a'); + a.id = album.id; + a.innerHTML = album.name; + + a.addEventListener('click', function() { + panel.getAlbum(this); + }); + + li.appendChild(a); + albumsContainer.appendChild(li); + } + }) + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; + +panel.getAlbum = function(item) { panel.setActiveMenu(item); panel.getUploads(item.id); -} - -panel.changeToken = function(){ +}; +panel.changeToken = function() { axios.get('/api/tokens') - .then(function (response) { - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else return swal("An error ocurred", response.data.description, "error"); - } + .then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else return swal('An error ocurred', response.data.description, 'error'); + } - panel.page.innerHTML = ''; - var container = document.createElement('div'); - container.className = "container"; - container.innerHTML = ` + panel.page.innerHTML = ''; + var container = document.createElement('div'); + container.className = 'container'; + container.innerHTML = `

Manage your token

@@ -507,52 +448,45 @@ panel.changeToken = function(){

`; - panel.page.appendChild(container); + panel.page.appendChild(container); - document.getElementById('getNewToken').addEventListener('click', function(){ - panel.getNewToken(); - }); - - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - console.log(error); - }); - -} - -panel.getNewToken = function(){ - - axios.post('/api/tokens/change') - .then(function (response) { - - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else return swal("An error ocurred", response.data.description, "error"); - } - - swal({ - title: "Woohoo!", - text: 'Your token was changed successfully.', - type: "success" - }, function(){ - localStorage.token = response.data.token; - location.reload(); + document.getElementById('getNewToken').addEventListener('click', () => { + panel.getNewToken(); + }); }) + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - console.log(error); - }); +panel.getNewToken = function() { + axios.post('/api/tokens/change') + .then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else return swal('An error ocurred', response.data.description, 'error'); + } -} - -panel.changePassword = function(){ + swal({ + title: 'Woohoo!', + text: 'Your token was changed successfully.', + type: 'success' + }, () => { + localStorage.token = response.data.token; + location.reload(); + }); + }) + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; +panel.changePassword = function() { panel.page.innerHTML = ''; var container = document.createElement('div'); - container.className = "container"; + container.className = 'container'; container.innerHTML = `

Change your password

@@ -569,56 +503,51 @@ panel.changePassword = function(){ panel.page.appendChild(container); - document.getElementById('sendChangePassword').addEventListener('click', function(){ + document.getElementById('sendChangePassword').addEventListener('click', () => { if (document.getElementById('password').value === document.getElementById('passwordConfirm').value) { panel.sendNewPassword(document.getElementById('password').value); } else { swal({ - title: "Password mismatch!", - text: 'Your passwords do not match, please try again.', - type: "error" - }, function() { + title: 'Password mismatch!', + text: 'Your passwords do not match, please try again.', + type: 'error' + }, () => { panel.changePassword(); }); } }); -} +}; -panel.sendNewPassword = function(pass){ +panel.sendNewPassword = function(pass) { + axios.post('/api/password/change', { password: pass }) + .then(response => { + if (response.data.success === false) { + if (response.data.description === 'No token provided') return panel.verifyToken(panel.token); + else return swal('An error ocurred', response.data.description, 'error'); + } - axios.post('/api/password/change', {password: pass}) - .then(function (response) { - - if(response.data.success === false){ - if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); - else return swal("An error ocurred", response.data.description, "error"); - } - - swal({ - title: "Woohoo!", - text: 'Your password was changed successfully.', - type: "success" - }, function(){ - location.reload(); + swal({ + title: 'Woohoo!', + text: 'Your password was changed successfully.', + type: 'success' + }, () => { + location.reload(); + }); }) + .catch(error => { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); +}; - }) - .catch(function (error) { - return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - console.log(error); - }); - -} - -panel.setActiveMenu = function(item){ +panel.setActiveMenu = function(item) { var menu = document.getElementById('menu'); var items = menu.getElementsByTagName('a'); - for(var i = 0; i < items.length; i++) - items[i].className = ""; + for (var i = 0; i < items.length; i++) { items[i].className = ''; } item.className = 'is-active'; -} +}; -window.onload = function () { +window.onload = function() { panel.preparePage(); -} +}; diff --git a/public/js/home.js b/public/js/home.js index 9fcef05..d932aec 100644 --- a/public/js/home.js +++ b/public/js/home.js @@ -3,95 +3,89 @@ var upload = {}; upload.isPrivate = true; upload.token = localStorage.token; upload.maxFileSize; -// add the album var to the upload so we can store the album id in there +// Add the album var to the upload so we can store the album id in there upload.album; upload.myDropzone; -upload.checkIfPublic = function(){ +upload.checkIfPublic = function() { axios.get('/api/check') - .then(function (response) { - upload.isPrivate= response.data.private; - upload.maxFileSize = response.data.maxFileSize; - upload.preparePage(); + .then(response => { + upload.isPrivate = response.data.private; + upload.maxFileSize = response.data.maxFileSize; + upload.preparePage(); }) - .catch(function (error) { - swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); + .catch(error => { + swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); return console.log(error); }); -} +}; -upload.preparePage = function(){ - if(!upload.isPrivate) return upload.prepareUpload(); - if(!upload.token) return document.getElementById('loginToUpload').style.display = 'inline-flex'; +upload.preparePage = function() { + if (!upload.isPrivate) return upload.prepareUpload(); + if (!upload.token) return document.getElementById('loginToUpload').style.display = 'inline-flex'; upload.verifyToken(upload.token, true); -} +}; -upload.verifyToken = function(token, reloadOnError){ - if(reloadOnError === undefined) - reloadOnError = false; - - axios.post('/api/tokens/verify', { - token: token - }) - .then(function (response) { +upload.verifyToken = function(token, reloadOnError) { + if (reloadOnError === undefined) { reloadOnError = false; } - if(response.data.success === false){ + axios.post('/api/tokens/verify', { token: token }) + .then(response => { + if (response.data.success === false) { swal({ - title: "An error ocurred", - text: response.data.description, - type: "error" - }, function(){ - if(reloadOnError){ - localStorage.removeItem("token"); - location.reload(); - } - }) - return; + title: 'An error ocurred', + text: response.data.description, + type: 'error' + }, () => { + if (reloadOnError) { + localStorage.removeItem('token'); + location.reload(); + } + }); + return; } localStorage.token = token; - upload.token = token; - return upload.prepareUpload(); - + upload.token = token; + return upload.prepareUpload(); }) - .catch(function (error) { - swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); + .catch(error => { + swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); return console.log(error); }); +}; -} - -upload.prepareUpload = function(){ +upload.prepareUpload = function() { // I think this fits best here because we need to check for a valid token before we can get the albums if (upload.token) { var select = document.getElementById('albumSelect'); - - select.addEventListener('change', function() { + + select.addEventListener('change', () => { upload.album = select.value; }); - axios.get('/api/albums', { headers: { token: upload.token }}) - .then(function(res) { - var albums = res.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 (var i = 0; i < albums.length; i++) { - var opt = document.createElement('option'); - opt.value = albums[i].id; - opt.innerHTML = albums[i].name; - select.appendChild(opt); - } - // display the album selection - document.getElementById('albumDiv').style.display = 'block'; - }) - .catch(function(e) { - swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); - return console.log(e); - }) + axios.get('/api/albums', { headers: { token: upload.token } }) + .then(res => { + var albums = res.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 (var i = 0; i < albums.length; i++) { + var opt = document.createElement('option'); + opt.value = albums[i].id; + opt.innerHTML = albums[i].name; + select.appendChild(opt); + } + // Display the album selection + document.getElementById('albumDiv').style.display = 'block'; + }) + .catch(e => { + swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + return console.log(e); + }); } div = document.createElement('div'); @@ -99,25 +93,23 @@ upload.prepareUpload = function(){ div.innerHTML = 'Click here or drag and drop files'; div.style.display = 'flex'; - document.getElementById('maxFileSize').innerHTML = 'Maximum upload size per file is ' + upload.maxFileSize; + document.getElementById('maxFileSize').innerHTML = `Maximum upload size per file is ${upload.maxFileSize}`; document.getElementById('loginToUpload').style.display = 'none'; - - if(upload.token === undefined) - document.getElementById('loginLinkText').innerHTML = 'Create an account and keep track of your uploads'; + + if (upload.token === undefined) { document.getElementById('loginLinkText').innerHTML = 'Create an account and keep track of your uploads'; } document.getElementById('uploadContainer').appendChild(div); - + upload.prepareDropzone(); +}; -} - -upload.prepareDropzone = function(){ +upload.prepareDropzone = function() { var previewNode = document.querySelector('#template'); previewNode.id = ''; var previewTemplate = previewNode.parentNode.innerHTML; previewNode.parentNode.removeChild(previewNode); - var dropzone = new Dropzone('div#dropzone', { + var dropzone = new Dropzone('div#dropzone', { url: '/api/upload', paramName: 'files[]', maxFilesize: upload.maxFileSize.slice(0, -2), @@ -128,34 +120,31 @@ upload.prepareDropzone = function(){ createImageThumbnails: false, maxFiles: 1000, autoProcessQueue: true, - headers: { - 'token': upload.token - }, + headers: { token: upload.token }, init: function() { upload.myDropzone = this; - this.on('addedfile', function(file) { + this.on('addedfile', file => { document.getElementById('uploads').style.display = 'block'; }); - // add the selected albumid, if an album is selected, as a header - this.on('sending', function(file, xhr) { + // Add the selected albumid, if an album is selected, as a header + this.on('sending', (file, xhr) => { if (upload.album) { - xhr.setRequestHeader('albumid', upload.album) + xhr.setRequestHeader('albumid', upload.album); } }); } }); // Update the total progress bar - dropzone.on('uploadprogress', function(file, progress) { + dropzone.on('uploadprogress', (file, progress) => { file.previewElement.querySelector('.progress').setAttribute('value', progress); - file.previewElement.querySelector('.progress').innerHTML = progress + '%'; + file.previewElement.querySelector('.progress').innerHTML = `${progress}%`; }); - dropzone.on('success', function(file, response) { - + dropzone.on('success', (file, response) => { // Handle the responseText here. For example, add the text to the preview element: - if(response.success === false){ + if (response.success === false) { var span = document.createElement('span'); span.innerHTML = response.description; file.previewTemplate.querySelector('.link').appendChild(span); @@ -167,45 +156,44 @@ upload.prepareDropzone = function(){ a.target = '_blank'; a.innerHTML = response.files[0].url; file.previewTemplate.querySelector('.link').appendChild(a); - + file.previewTemplate.querySelector('.progress').style.display = 'none'; - }); upload.prepareShareX(); -} +}; -upload.prepareShareX = function(){ +upload.prepareShareX = function() { if (upload.token) { - var sharex_element = document.getElementById("ShareX"); - var sharex_file = "{\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\": \"" + upload.token + "\"\r\n\ + var sharex_element = document.getElementById('ShareX'); + var sharex_file = `{\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": "${upload.token}"\r\n\ },\r\n\ - \"ResponseType\": \"Text\",\r\n\ - \"URL\": \"$json:files[0].url$\",\r\n\ - \"ThumbnailURL\": \"$json:files[0].url$\"\r\n\ -}"; - var sharex_blob = new Blob([sharex_file], {type: "application/octet-binary"}); - sharex_element.setAttribute("href", URL.createObjectURL(sharex_blob)) - sharex_element.setAttribute("download", location.hostname + ".sxcu"); + "ResponseType": "Text",\r\n\ + "URL": "$json:files[0].url$",\r\n\ + "ThumbnailURL": "$json:files[0].url$"\r\n\ +}`; + var sharex_blob = new Blob([sharex_file], { type: 'application/octet-binary' }); + sharex_element.setAttribute('href', URL.createObjectURL(sharex_blob)); + sharex_element.setAttribute('download', `${location.hostname}.sxcu`); } -} +}; -//Handle image paste event -window.addEventListener('paste', function(event) { +// Handle image paste event +window.addEventListener('paste', event => { var items = (event.clipboardData || event.originalEvent.clipboardData).items; for (index in items) { var item = items[index]; if (item.kind === 'file') { var blob = item.getAsFile(); console.log(blob.type); - var file = new File([blob], "pasted-image."+blob.type.match(/(?:[^\/]*\/)([^;]*)/)[1]); + var file = new File([blob], `pasted-image.${blob.type.match(/(?:[^\/]*\/)([^;]*)/)[1]}`); file.type = blob.type; console.log(file); upload.myDropzone.addFile(file); @@ -213,7 +201,7 @@ window.addEventListener('paste', function(event) { } }); -window.onload = function () { +window.onload = function() { upload.checkIfPublic(); }; diff --git a/views/album.handlebars b/views/album.handlebars index 7754913..dfe4172 100644 --- a/views/album.handlebars +++ b/views/album.handlebars @@ -15,7 +15,7 @@ - +