aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPitu <[email protected]>2017-01-18 02:40:14 -0300
committerPitu <[email protected]>2017-01-18 02:40:14 -0300
commit84ff2241ba81fc6a1a2201074d30f971dad8a0de (patch)
tree8ec712f118fc7e553f170640e3c86760bbd2a2ec
parentBetter static routes (diff)
downloadhost.fuwn.me-84ff2241ba81fc6a1a2201074d30f971dad8a0de.tar.xz
host.fuwn.me-84ff2241ba81fc6a1a2201074d30f971dad8a0de.zip
Shit ton of things on this update
-rw-r--r--controllers/albumsController.js50
-rw-r--r--controllers/uploadController.js2
-rw-r--r--database/db.js13
-rw-r--r--pages/home.html12
-rw-r--r--pages/panel.html11
-rw-r--r--public/.DS_Storebin0 -> 8196 bytes
-rw-r--r--public/css/style.css1
-rw-r--r--public/images/logo_smol.pngbin0 -> 59261 bytes
-rw-r--r--public/js/panel.js155
-rw-r--r--public/js/upload.js17
-rw-r--r--routes/api.js1
11 files changed, 213 insertions, 49 deletions
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 @@
<head>
<title>loli-safe - A self hosted upload service</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.3.0/css/bulma.min.css">
+ <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
<link rel="stylesheet" type="text/css" href="/css/style.css">
+ <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.3.0/min/dropzone.min.js"></script>
<script type="text/javascript" src="/js/upload.js"></script>
</head>
@@ -13,7 +15,7 @@
<div class="hero-body">
<div class="container">
<p id="b">
- <img class='logo' src="/images/logo.png">
+ <img class='logo' src="/images/logo_smol.png">
</p>
<h1 class="title">loli-safe</h1>
<h2 class="subtitle">A <strong>modern</strong> self-hosted file upload service</h2>
@@ -32,10 +34,6 @@
<div class="column is-hidden-mobile"></div>
</div>
- <h3 id="links">
- <a href="https://github.com/kanadeko/loli-safe" target="_blank" class="is-danger">View on Github</a><span>|</span><a href="https://chrome.google.com/webstore/detail/loli-safe-uploader/enkkmplljfjppcdaancckgilmgoiofnj/related" target="_blank" class="is-danger">Chrome extension</a><span>|</span><a href="/panel" target="_blank" class="is-danger">Dashboard</a>
- </h3>
-
<div id="uploads">
<div id="template" class="columns">
<div class="column">
@@ -51,6 +49,10 @@
</div>
</div>
</div>
+
+ <h3 id="links">
+ <a href="https://github.com/kanadeko/loli-safe" target="_blank" class="is-danger">View on Github</a><span>|</span><a href="https://chrome.google.com/webstore/detail/loli-safe-uploader/enkkmplljfjppcdaancckgilmgoiofnj/related" target="_blank" class="is-danger">Chrome extension</a><span>|</span><a href="/panel" target="_blank" class="is-danger">Dashboard</a>
+ </h3>
</div>
</div>
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 @@
<head>
<title>loli-safe - A self hosted upload service</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.3.0/css/bulma.min.css">
+ <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
<link rel="stylesheet" type="text/css" href="/css/style.css">
+ <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<script type="text/javascript" src="/js/panel.js"></script>
</head>
<body>
<section id='auth' class="hero is-light is-fullheight">
+
<div class="hero-body">
<div class="container">
<h1 class="title">
@@ -22,6 +25,7 @@
</h2>
</div>
</div>
+
</section>
<section id='dashboard' class="section">
@@ -42,11 +46,7 @@
<ul class="menu-list">
<li><a id='itemManageGallery'>Manage your albums</a></li>
<li>
- <ul id='galleryContainer'>
- <li><a>Album 1</a></li>
- <li><a>Album 2</a></li>
- <li><a>Album 3</a></li>
- </ul>
+ <ul id='albumsContainer'></ul>
</li>
</ul>
<p class="menu-label">Administration</p>
@@ -60,6 +60,7 @@
</div>
</div>
</div>
+
</section>
</body>
</html> \ No newline at end of file
diff --git a/public/.DS_Store b/public/.DS_Store
new file mode 100644
index 0000000..40de987
--- /dev/null
+++ b/public/.DS_Store
Binary files 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
--- /dev/null
+++ b/public/images/logo_smol.png
Binary files 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 = `
<table class="table">
@@ -81,7 +93,7 @@ panel.getUploads = function(){
<tbody id="table">
</tbody>
</table>`;
- page.appendChild(container);
+ panel.page.appendChild(container);
var table = document.getElementById('table');
@@ -92,7 +104,7 @@ panel.getUploads = function(){
<tr>
<th><a href="${item.file}" target="_blank">${item.file}</a></th>
<th>${item.album}</th>
- <td>${item.date}</td>
+ <td>${item.timestamp}</td>
</tr>
`;
@@ -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 = `
+ <h2 class="subtitle">Create new album</h2>
+
+ <p class="control has-addons has-addons-centered">
+ <input id="albumName" class="input" type="text" placeholder="Name">
+ <a id="submitAlbum" class="button is-primary">Submit</a>
+ </p>
+
+ <h2 class="subtitle">List of albums</h2>
+
+ <table class="table">
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Files</th>
+ <th>Created At</th>
+ </tr>
+ </thead>
+ <tbody id="table">
+ </tbody>
+ </table>`;
+
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 = `
+ <tr>
+ <th>${item.name}</th>
+ <th>${item.files}</th>
+ <td>${item.timestamp}</td>
+ </tr>
+ `;
+
+ 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))