Browse Source

Shit ton of things on this update

Pitu 7 years ago
parent
commit
84ff2241ba

+ 39 - 11
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

+ 1 - 1
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)
 			})
 		})
 

+ 8 - 5
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(() => {

+ 7 - 5
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>

+ 6 - 5
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>

BIN
public/.DS_Store


+ 1 - 0
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% {

BIN
public/images/logo_smol.png


+ 138 - 17
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);
 }

+ 12 - 5
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;
 			}
 

+ 1 - 0
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))