From 84ff2241ba81fc6a1a2201074d30f971dad8a0de Mon Sep 17 00:00:00 2001 From: Pitu Date: Wed, 18 Jan 2017 02:40:14 -0300 Subject: Shit ton of things on this update --- controllers/albumsController.js | 50 ++++++++++--- controllers/uploadController.js | 2 +- database/db.js | 13 ++-- pages/home.html | 12 ++-- pages/panel.html | 11 +-- public/.DS_Store | Bin 0 -> 8196 bytes public/css/style.css | 1 + public/images/logo_smol.png | Bin 0 -> 59261 bytes public/js/panel.js | 155 +++++++++++++++++++++++++++++++++++----- public/js/upload.js | 17 +++-- routes/api.js | 1 + 11 files changed, 213 insertions(+), 49 deletions(-) create mode 100644 public/.DS_Store create mode 100644 public/images/logo_smol.png diff --git a/controllers/albumsController.js b/controllers/albumsController.js index 7639b3d..1f2d695 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -8,25 +8,53 @@ albumsController.list = function(req, res, next){ if(req.headers.auth !== config.adminToken) return res.status(401).send('not-authorized') - db.table('albums').select('id', 'name').then((albums) => { - return res.json({ albums }) + let fields = ['id', 'name'] + + if(req.headers.extended !== undefined) + fields.push('timestamp') + + db.table('albums').select(fields).then((albums) => { + + if(req.headers.extended === undefined) + return res.json({ success: true, albums }) + + let ids = [] + for(let album of albums) + ids.push(album.id) + + db.table('files').whereIn('albumid', ids).select('albumid').then((files) => { + + let albumsCount = {} + + for(let id of ids) albumsCount[id] = 0 + for(let file of files) albumsCount[file.albumid] += 1 + for(let album of albums) album.files = albumsCount[album.id] + + return res.json({ success: true, albums }) + }) }) } -albumsController.test = function(req, res, next){ +albumsController.create = function(req, res, next){ if(req.headers.auth !== config.adminToken) return res.status(401).send('not-authorized') - let testdata = [ - {name: 'Test 1'}, - {name: 'Test 2'}, - {name: 'Test 3'}, - {name: 'Test 4'}, - {name: 'Test 5'} - ] + let name = req.headers.name + if(name === undefined || name === '') + return res.json({ success: false, description: 'No album name specified' }) + + db.table('albums').where('name', name).then((album) => { + if(album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' }) - db.table('albums').insert(testdata).then(() => {}) + db.table('albums').insert({ + name: name, + timestamp: Math.floor(Date.now() / 1000) + }).then(() => { + return res.json({ success: true }) + }) + }) } + module.exports = albumsController \ No newline at end of file diff --git a/controllers/uploadController.js b/controllers/uploadController.js index 120a52b..cb97eaa 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -46,7 +46,7 @@ uploadsController.upload = function(req, res, next){ size: file.size, ip: req.ip, albumid: album, - created_at: Math.floor(Date.now() / 1000) + timestamp: Math.floor(Date.now() / 1000) }) }) diff --git a/database/db.js b/database/db.js index 6c4889b..b22ce51 100644 --- a/database/db.js +++ b/database/db.js @@ -5,7 +5,7 @@ let init = function(db, config){ db.schema.createTableIfNotExists('albums', function (table) { table.increments() table.string('name') - table.timestamps() + table.integer('timestamp') }).then(() => {}) db.schema.createTableIfNotExists('files', function (table) { @@ -16,13 +16,13 @@ let init = function(db, config){ table.string('size') table.string('ip') table.integer('albumid') - table.timestamps() + table.integer('timestamp') }).then(() => {}) db.schema.createTableIfNotExists('tokens', function (table) { table.string('name') table.string('value') - table.timestamps() + table.integer('timestamp') }).then(() => { // == Generate a 1 time token == // @@ -32,16 +32,19 @@ let init = function(db, config){ // This is the first launch of the app let clientToken = require('randomstring').generate() let adminToken = require('randomstring').generate() + let now = Math.floor(Date.now() / 1000) db.table('tokens').insert( [ { name: 'client', - value: clientToken + value: clientToken, + timestamp: now }, { name: 'admin', - value: adminToken + value: adminToken, + timestamp: now } ] ).then(() => { diff --git a/pages/home.html b/pages/home.html index 1ec947b..85e2493 100644 --- a/pages/home.html +++ b/pages/home.html @@ -3,7 +3,9 @@ loli-safe - A self hosted upload service + + @@ -13,7 +15,7 @@

- +

loli-safe

A modern self-hosted file upload service

@@ -32,10 +34,6 @@
- -
@@ -51,6 +49,10 @@
+ +
diff --git a/pages/panel.html b/pages/panel.html index 8565264..3a4dbf5 100644 --- a/pages/panel.html +++ b/pages/panel.html @@ -3,12 +3,15 @@ loli-safe - A self hosted upload service + +
+

@@ -22,6 +25,7 @@

+
@@ -42,11 +46,7 @@ @@ -60,6 +60,7 @@ +
\ No newline at end of file diff --git a/public/.DS_Store b/public/.DS_Store new file mode 100644 index 0000000..40de987 Binary files /dev/null and b/public/.DS_Store differ diff --git a/public/css/style.css b/public/css/style.css index 44f46bf..5204460 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -52,6 +52,7 @@ section#home img.logo { height: 200px; margin-top: 20px; } section#home .dz-preview .dz-details { display: flex; } section#home .dz-preview .dz-details .dz-size, section#home .dz-preview .dz-details .dz-filename { flex: 1; } section#home .dz-preview img, section#home .dz-preview .dz-success-mark, section#home .dz-preview .dz-error-mark { display: none; } +section#home div#uploads { margin-bottom: 25px; } @keyframes floatUp { 0% { diff --git a/public/images/logo_smol.png b/public/images/logo_smol.png new file mode 100644 index 0000000..94b6797 Binary files /dev/null and b/public/images/logo_smol.png differ diff --git a/public/js/panel.js b/public/js/panel.js index 14f6237..94b5b1c 100644 --- a/public/js/panel.js +++ b/public/js/panel.js @@ -22,11 +22,18 @@ panel.verifyToken = function(token, reloadOnError = false){ var json = JSON.parse(xhr.responseText); if(json.success === false){ - alert(json.description); - if(reloadOnError){ - localStorage.removeItem("admintoken"); - location.reload(); - } + + swal({ + title: "An error ocurred", + text: json.description, + type: "error" + }, function(){ + if(reloadOnError){ + localStorage.removeItem("admintoken"); + location.reload(); + } + }) + return; } @@ -52,22 +59,27 @@ panel.prepareDashboard = function(){ }); document.getElementById('itemManageGallery').addEventListener('click', function(){ - panel.getGalleries(); + panel.getAlbums(); }); + + panel.getAlbumsSidebar(); } panel.getUploads = function(){ - page.innerHTML = ''; + panel.page.innerHTML = ''; var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE){ if(xhr.responseText === 'not-authorized') - return notAuthorized(); + return panel.verifyToken(panel.token); var json = JSON.parse(xhr.responseText); - + console.log(json); + if(json.success === false) + return swal("An error ocurred", json.description, "error"); + var container = document.createElement('div'); container.innerHTML = ` @@ -81,7 +93,7 @@ panel.getUploads = function(){
`; - page.appendChild(container); + panel.page.appendChild(container); var table = document.getElementById('table'); @@ -92,7 +104,7 @@ panel.getUploads = function(){ ${item.file} ${item.album} - ${item.date} + ${item.timestamp} `; @@ -106,25 +118,134 @@ panel.getUploads = function(){ xhr.send(null); } -panel.getGalleries = function(){ +panel.getAlbums = function(){ + panel.page.innerHTML = ''; var xhr = new XMLHttpRequest(); + var container = document.createElement('div'); + container.className = "container"; + container.innerHTML = ` +

Create new album

+ +

+ + Submit +

+ +

List of albums

+ + + + + + + + + + + +
NameFilesCreated At
`; + xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { + if(xhr.responseText === 'not-authorized') + return panel.verifyToken(panel.token); + var json = JSON.parse(xhr.responseText); + console.log(json); if(json.success === false) - return alert(json.description); + return swal("An error ocurred", json.description, "error"); + panel.page.appendChild(container); + var table = document.getElementById('table'); + for(var item of json.albums){ - localStorage.admintoken = token; - panel.token = token; - return panel.prepareDashboard(); + var tr = document.createElement('tr'); + tr.innerHTML = ` + + ${item.name} + ${item.files} + ${item.timestamp} + + `; + + table.appendChild(tr); + } + document.getElementById('submitAlbum').addEventListener('click', function(){ + panel.submitAlbum(); + }); + } } - xhr.open('GET', '/api/galleries', true); + + xhr.open('GET', '/api/albums', true); + xhr.setRequestHeader('auth', panel.token); + xhr.setRequestHeader('extended', ''); + xhr.send(null); +} + +panel.submitAlbum = function(){ + + var xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = function() { + if (xhr.readyState == XMLHttpRequest.DONE) { + + if(xhr.responseText === 'not-authorized') + return panel.verifyToken(panel.token); + + var json = JSON.parse(xhr.responseText); + if(json.success === false) + return swal("An error ocurred", json.description, "error"); + + swal("Woohoo!", "Album was added successfully", "success"); + panel.getAlbumsSidebar(); + panel.getAlbums(); + return; + } + } + + xhr.open('POST', '/api/albums', true); + xhr.setRequestHeader('auth', panel.token); + xhr.setRequestHeader('name', document.getElementById('albumName').value); + xhr.send(null); + +} + +panel.getAlbumsSidebar = function(){ + var xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = function() { + if (xhr.readyState == XMLHttpRequest.DONE) { + + if(xhr.responseText === 'not-authorized') + return panel.verifyToken(panel.token); + + var json = JSON.parse(xhr.responseText); + console.log(json); + if(json.success === false) + return swal("An error ocurred", json.description, "error"); + + var albumsContainer = document.getElementById('albumsContainer'); + albumsContainer.innerHTML = ''; + + if(json.albums === undefined) return; + + for(var album of json.albums){ + li = document.createElement('li'); + a = document.createElement('a'); + a.innerHTML = album.name; + + li.appendChild(a); + albumsContainer.appendChild(li); + } + } + } + + xhr.open('GET', '/api/albums', true); xhr.setRequestHeader('auth', panel.token); xhr.send(null); } diff --git a/public/js/upload.js b/public/js/upload.js index 3672077..0341ecc 100644 --- a/public/js/upload.js +++ b/public/js/upload.js @@ -37,11 +37,18 @@ upload.verifyToken = function(token, reloadOnError = false){ var json = JSON.parse(xhr.responseText); if(json.success === false){ - alert(json.description); - if(reloadOnError){ - localStorage.removeItem("token"); - location.reload(); - } + + swal({ + title: "An error ocurred", + text: json.description, + type: "error" + }, function(){ + if(reloadOnError){ + localStorage.removeItem("token"); + location.reload(); + } + }) + return; } diff --git a/routes/api.js b/routes/api.js index ca9c7c0..523af72 100644 --- a/routes/api.js +++ b/routes/api.js @@ -14,6 +14,7 @@ routes.get ('/check', (req, res, next) => { routes.get ('/uploads', (req, res, next) => uploadController.list(req, res)) routes.post ('/upload', (req, res, next) => uploadController.upload(req, res, next)) routes.get ('/albums', (req, res, next) => albumsController.list(req, res, next)) +routes.post ('/albums', (req, res, next) => albumsController.create(req, res, next)) routes.get ('/albums/test', (req, res, next) => albumsController.test(req, res, next)) routes.get ('/token/verify', (req, res, next) => tokenController.verify(req, res)) -- cgit v1.2.3