Browse Source

Admin panel, pomf-standarization and stuff

kanadeko 7 years ago
parent
commit
55e2d17636
7 changed files with 158 additions and 39 deletions
  1. 2 2
      config.sample.js
  2. 41 4
      controllers/uploadController.js
  3. 5 0
      database/db.js
  4. 8 16
      pages/panel.html
  5. 74 0
      public/js/panel.js
  6. 23 13
      public/js/upload.js
  7. 5 4
      routes/api.js

+ 2 - 2
config.sample.js

@@ -18,9 +18,9 @@ module.exports = {
 	// Uploads config
 	uploads: {
 
-		// If prefix is set, it will be appended at the end of basedomain.
+		// If prefix is set, it will be appended at the end of basedomain. Remember to finish it with `/`
 		// Ex: https://i.kanacchi.moe/prefix/k4n4.png
-		// Leave blank to use the basedomain
+		// Leave blank to use the basedomain.
 		prefix: '',
 
 		// Folder where images should be stored

+ 41 - 4
controllers/uploadController.js

@@ -31,20 +31,57 @@ uploadsController.upload = function(req, res, next){
 	upload(req, res, function (err) {
 		if (err) {
 			console.error(err)
-			return res.json({ error: err })
+			return res.json({ 
+				success: false,
+				errorcode: '',
+				description: err
+			})
 		}
 
 		db.table('files').insert({
 			file: req.file.filename,
-			galleryid: gallery
+			original: req.file.originalname,
+			type: req.file.mimetype,
+			size: req.file.size,
+			ip: req.ip,
+			galleryid: gallery,
+			created_at: Math.floor(Date.now() / 1000)
 		}).then(() => {
 			return res.json({
-				'url': config.basedomain + req.file.filename
+				success: true,
+				files: [
+					{
+						name: req.file.filename,
+						size: req.file.size,
+						url: config.basedomain + req.file.filename
+					}
+				]
 			})
 		})
-		
 	})
 
 }
 
+uploadsController.list = function(req, res){
+
+	if(config.TOKEN !== '')
+		if(req.headers.auth !== config.TOKEN)
+			return res.status(401).send('not-authorized')
+
+	db.table('files').then((files) => {
+
+		for(let file of files){
+			file.file = config.basedomain + config.uploads.prefix + file.file
+			file.ext = file.file.split('.').pop()
+
+			file.date = new Date(file.created_at * 1000)
+			file.date = file.date.getFullYear() + '-' + file.date.getMonth() + '-' + file.date.getDate() + ' ' + (file.date.getHours() < 10 ? '0' : '') + file.date.getHours() + ':' + (file.date.getMinutes() < 10 ? '0' : '') + file.date.getMinutes() + ':' + (file.date.getSeconds() < 10 ? '0' : '') + file.date.getSeconds()
+			
+
+		}
+
+		return res.json(files)
+	})
+}
+
 module.exports = uploadsController

+ 5 - 0
database/db.js

@@ -11,7 +11,12 @@ let init = function(db){
 	db.schema.createTableIfNotExists('files', function (table) {
 		table.increments()
 		table.string('file')
+		table.string('original')
+		table.string('type')
+		table.string('size')
+		table.string('ip')
 		table.integer('galleryid')
+		table.timestamps()
 	}).then(() => {})
 
 }

+ 8 - 16
pages/panel.html

@@ -4,8 +4,7 @@
         <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="/css/style.css">
-        <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>
+        <script type="text/javascript" src="/js/panel.js"></script>
     </head>
     <body>
         <section class="section">
@@ -16,34 +15,27 @@
                 <div class="columns">
                     <div class="column is-3">
                         <aside class="menu">
-                            <p class="menu-label">
-                                General
-                            </p>
+                            <p class="menu-label">General</p>
                             <ul class="menu-list">
                                 <li><a href="/">Frontpage</a></li>
-                                <li><a>Uploads</a></li>
+                                <li><a id='itemUploads'>Uploads</a></li>
                             </ul>
-                            <p class="menu-label">
-                                Galleries
-                            </p>
+                            <p class="menu-label">Galleries</p>
                             <ul class="menu-list">
-                                <li><a>Manage your galleries</a></li>
+                                <li><a id='itemManageGallery'>Manage your galleries</a></li>
                                 <li>
-                                    <a class="is-active">Galleries</a>
-                                    <ul>
+                                    <ul id='galleryContainer'>
                                         <li><a>Gallery 1</a></li>
                                         <li><a>Gallery 2</a></li>
                                         <li><a>Gallery 3</a></li>
                                     </ul>
                                 </li>
                             </ul>
-                            <p class="menu-label">
-                                Administration
-                            </p>
+                            <!--<p class="menu-label">Administration</p>
                             <ul class="menu-list">
                                 <li><a>Change your token</a></li>
                                 <li><a>Some other fancy stuff</a></li>
-                            </ul>
+                            </ul>-->
                         </aside>
                     </div>
                     <div class="column has-text-centered" id='page'>

+ 74 - 0
public/js/panel.js

@@ -0,0 +1,74 @@
+window.onload = function () {
+
+	if(!localStorage.token) 
+		return;
+
+	var page = document.getElementById('page');
+
+	prepareMenu();
+
+	function prepareMenu(){
+		document.getElementById('itemUploads').addEventListener('click', function(){
+			getUploads();
+		});
+
+		document.getElementById('itemManageGallery').addEventListener('click', function(){
+			getGalleries();
+		});
+	}
+
+	function getUploads(){
+		page.innerHTML = '';
+		var xhr = new XMLHttpRequest();
+
+		xhr.onreadystatechange = function() {
+			if(xhr.readyState == XMLHttpRequest.DONE){
+				if(xhr.responseText !== 'not-authorized'){
+					var json = JSON.parse(xhr.responseText);
+
+					var container = document.createElement('div');
+					container.innerHTML = `
+						<table class="table">
+					  		<thead>
+					    		<tr>
+								      <th>File</th>
+								      <th>Gallery</th>
+								      <th>Date</th>
+					    		</tr>
+					  		</thead>
+					  		<tbody id="table">
+					  		</tbody>
+					  	</table>`;
+					page.appendChild(container);
+
+					var table = document.getElementById('table');
+
+					for(var item of json){
+
+						var tr = document.createElement('tr');
+						tr.innerHTML = `
+							<tr>
+						    	<th><a href="${item.file}" target="_blank">${item.file}</a></th>
+						      	<th>${item.gallery}</th>
+						      	<td>${item.date}</td>
+						    </tr>
+						    `;
+
+						table.appendChild(tr);
+					}
+				}
+			}
+		}
+		xhr.open('GET', '/api/uploads', true);
+		xhr.setRequestHeader('auth', localStorage.token);
+		xhr.send(null);
+	}
+
+	function getContent(item, value){
+		let endpoint;
+		if(item === 'uploads') endpoint = '/api/uploads'
+		if(item === 'galleries') endpoint = '/api/uploads'
+
+	}
+
+}

+ 23 - 13
public/js/upload.js

@@ -21,8 +21,8 @@ window.onload = function () {
 
 		if(!localStorage.token){
 			document.getElementById('tokenContainer').style.display = 'flex'
-			document.getElementById("tokenSubmit").addEventListener("click", function(){
-				getInfo(document.getElementById("token").value)
+			document.getElementById('tokenSubmit').addEventListener('click', function(){
+				getInfo(document.getElementById('token').value)
 			});
 		}else{
 			getInfo(localStorage.token);
@@ -32,8 +32,8 @@ window.onload = function () {
 
 	function prepareDropzone(){
 
-		var previewNode = document.querySelector("#template");
-		previewNode.id = "";
+		var previewNode = document.querySelector('#template');
+		previewNode.id = '';
 		var previewTemplate = previewNode.parentNode.innerHTML;
 		previewNode.parentNode.removeChild(previewNode);
 
@@ -52,26 +52,36 @@ window.onload = function () {
         		'auth': localStorage.token
     		},
     		init: function() {
-    			this.on("addedfile", function(file) { 
+    			this.on('addedfile', function(file) { 
     				document.getElementById('uploads').style.display = 'block';
     			});
   			}
 		});
 
 		// Update the total progress bar
-		dropzone.on("uploadprogress", function(file, progress) {
-			file.previewElement.querySelector(".progress").style.width = progress + "%";
+		dropzone.on('uploadprogress', function(file, progress) {
+			file.previewElement.querySelector('.progress').style.width = progress + '%';
 		});
 
-		dropzone.on("success", function(file, response) {
+		dropzone.on('success', function(file, response) {
+
 			// Handle the responseText here. For example, add the text to the preview element:
+
+			if(response.success === false){
+				var span = document.createElement('span');
+				span.innerHTML = response.description;
+				file.previewTemplate.querySelector('.link').appendChild(span);
+				return;
+			}
+
 			a = document.createElement('a');
-			a.href = response.url;
+			a.href = response.files[0].url;
 			a.target = '_blank';
-			a.innerHTML = response.url;
-
-			file.previewTemplate.querySelector(".progress").style.display = 'none';
-			file.previewTemplate.querySelector(".link").appendChild(a);
+			a.innerHTML = response.files[0].url;
+			file.previewTemplate.querySelector('.link').appendChild(a);
+			
+			file.previewTemplate.querySelector('.progress').style.display = 'none';
+			
 		});
 
 	}

+ 5 - 4
routes/api.js

@@ -3,7 +3,7 @@ const routes = require('express').Router()
 const uploadController = require('../controllers/uploadController')
 const galleryController = require('../controllers/galleryController')
 
-routes.get('/check', (req, res, next) => {
+routes.get ('/check', (req, res, next) => {
 	if(config.TOKEN === '')
 		return res.json({token: false})
 	return res.json({token: true})
@@ -20,8 +20,9 @@ routes.get('/info', (req, res, next) => {
 	})
 })
 
-routes.post('/upload', (req, res, next) => uploadController.upload(req, res, next))
-routes.get('/gallery', (req, res, next) => galleryController.list(req, res, next))
-routes.get('/gallery/test', (req, res, next) => galleryController.test(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  ('/gallery', (req, res, next) => galleryController.list(req, res, next))
+routes.get  ('/gallery/test', (req, res, next) => galleryController.test(req, res, next))
 
 module.exports = routes