Browse Source

Added public album sharing :ok_hand:

Pitu 7 years ago
parent
commit
e05a7c751e
6 changed files with 180 additions and 1 deletions
  1. 85 1
      controllers/albumsController.js
  2. 1 0
      database/db.js
  3. 1 0
      lolisafe.js
  4. 59 0
      pages/album.html
  5. 33 0
      public/js/album.js
  6. 1 0
      routes/api.js

+ 85 - 1
controllers/albumsController.js

@@ -1,5 +1,10 @@
 const config = require('../config.js')
 const db = require('knex')(config.database)
+const randomstring = require('randomstring')
+const path = require('path')
+const fs = require('fs')
+const ffmpeg = require('fluent-ffmpeg')
+const gm = require('gm')
 
 let albumsController = {}
 
@@ -67,6 +72,7 @@ albumsController.create = function(req, res, next){
 				name: name, 
 				enabled: 1,
 				userid: user[0].id,
+				identifier: randomstring.generate(8),
 				timestamp: Math.floor(Date.now() / 1000) 
 			}).then(() => {
 				return res.json({ success: true })	
@@ -74,7 +80,6 @@ albumsController.create = function(req, res, next){
 		}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
 	}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
 
-	
 }
 
 albumsController.delete = function(req, res, next){
@@ -120,4 +125,83 @@ albumsController.rename = function(req, res, next){
 
 }
 
+albumsController.get = function(req, res, next){
+	let identifier = req.params.identifier
+	if(identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' })
+
+	db.table('albums')
+	.where('identifier', identifier)
+	.then((albums) => {
+		if(albums.length === 0) return res.json({ success: false, description: 'Album not found' })
+
+		let title = albums[0].name
+		db.table('files').select('name').where('albumid', albums[0].id).orderBy('id', 'DESC').then((files) => {
+			
+			let basedomain = req.get('host')
+			for(let domain of config.domains)
+				if(domain.host === req.get('host'))
+					if(domain.hasOwnProperty('resolve'))
+						basedomain = domain.resolve
+
+			for(let file of files){
+				file.file = basedomain + '/' + file.name
+
+				if(config.uploads.generateThumbnails === true){
+
+					let extensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webm', '.mp4']
+					for(let ext of extensions){
+						if(path.extname(file.name) === ext){
+
+							file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png'
+
+							let thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs') + '/' + file.name.slice(0, -ext.length) + '.png'
+							fs.access(thumbname, function(err) {
+								if (err && err.code === 'ENOENT') {
+									// File doesnt exist
+
+									if (ext === '.webm' || ext === '.mp4') {
+										ffmpeg('./' + config.uploads.folder + '/' + file.name)
+											.thumbnail({
+												timestamps: [0],
+												filename: '%b.png',
+												folder: './' + config.uploads.folder + '/thumbs',
+												size: '200x?'
+											})
+											.on('error', function(error) {
+												console.log('Error - ', error.message)
+											})
+									}
+									else {
+										let size = {
+											width: 200,
+											height: 200
+										}
+
+										gm('./' + config.uploads.folder + '/' + file.name)
+											.resize(size.width, size.height + '>')
+											.gravity('Center')
+											.extent(size.width, size.height)
+											.background('transparent')
+											.write(thumbname, function (error) {
+												if (error) console.log('Error - ', error)
+											})
+									}
+								}
+							})
+						}
+					}
+				}
+			}
+
+			return res.json({
+				success: true,
+				title: title,
+				files
+			})
+
+		}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
+	}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
+
+}
+
 module.exports = albumsController

+ 1 - 0
database/db.js

@@ -5,6 +5,7 @@ let init = function(db){
 		table.increments()
 		table.integer('userid')
 		table.string('name')
+		table.string('identifier')
 		table.integer('enabled')
 		table.integer('timestamp')
 	}).then(() => {})

+ 1 - 0
lolisafe.js

@@ -26,6 +26,7 @@ safe.use(bodyParser.json())
 safe.use('/', express.static('./uploads'))
 safe.use('/', express.static('./public'))
 safe.use('/api', api)
+safe.get('/a/:identifier', (req, res, next) => res.sendFile('album.html', {root: './pages/'}))
 
 for(let page of config.pages){
 	let root = './pages/'

+ 59 - 0
pages/album.html

@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta name="description" content="A pomf-like file uploading service that doesn't suck.">
+        <meta name="keywords" content="upload,lolisafe,file,images,hosting">
+        <meta name="viewport" content="width=device-width, initial-scale=1.0">
+        <meta http-equiv="X-UA-Compatible" content="IE=edge">
+
+        <link rel="apple-touch-icon" sizes="180x180" href="https://lolisafe.moe/images/icons/apple-touch-icon.png?v=XBreOJMe24">
+        <link rel="icon" type="image/png" href="https://lolisafe.moe/images/icons/favicon-32x32.png?v=XBreOJMe24" sizes="32x32">
+        <link rel="icon" type="image/png" href="https://lolisafe.moe/images/icons/favicon-16x16.png?v=XBreOJMe24" sizes="16x16">
+        <link rel="manifest" href="https://lolisafe.moe/images/icons/manifest.json?v=XBreOJMe24">
+        <link rel="mask-icon" href="https://lolisafe.moe/images/icons/safari-pinned-tab.svg?v=XBreOJMe24" color="#5bbad5">
+        <link rel="shortcut icon" href="https://lolisafe.moe/images/icons/favicon.ico?v=XBreOJMe24">
+        <meta name="apple-mobile-web-app-title" content="lolisafe">
+        <meta name="application-name" content="lolisafe">
+        <meta name="msapplication-config" content="https://lolisafe.moe/images/icons/browserconfig.xml?v=XBreOJMe24">
+        <meta name="theme-color" content="#ffffff">
+
+        <meta property="og:url" content="https://lolisafe.moe" />
+        <meta property="og:type" content="website" />
+        <meta property="og:title" content="lolisafe.moe | A small safe worth protecting." />
+        <meta property="og:description" content="A pomf-like file uploading service that doesn't suck." />
+        <meta property="og:image" content="http://lolisafe.moe/images/logo_square.png" />
+        <meta property="og:image:secure_url" content="https://lolisafe.moe/images/logo_square.png" />
+
+        <meta name="twitter:card" content="summary">
+        <meta name="twitter:title" content="lolisafe.moe | A small safe worth protecting.">
+        <meta name="twitter:description" content="A pomf-like file uploading service that doesn't suck.">
+        <meta name="twitter:image" content="https://listen.moe/files/images/logo_square.png">
+        <meta name="twitter:image:src" content="https://lolisafe.moe/images/logo_square.png">
+
+        <title>lolisafe - A small safe worth protecting.</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/axios/0.15.3/axios.min.js"></script>
+        <script type="text/javascript" src="/js/album.js"></script>
+    </head>
+
+    <body>
+
+        <section class="hero is-fullheight">
+            <div class="hero-head">
+                <div class="container">
+                    <h1 class="title" id='title' style='margin-top: 1.5rem;'></h1>
+                    <hr>
+                </div>
+            </div>
+            <div class="hero-body">
+                <div class="container" id='container'>
+                    
+                </div>
+            </div>
+        </section>
+
+    </body>
+</html>

+ 33 - 0
public/js/album.js

@@ -0,0 +1,33 @@
+var album = {};
+
+album.get = function(album){
+	axios.get('/api/album/get/' + album)
+  	.then(function (response) {
+    	document.getElementById('title').innerHTML = response.data.title;
+
+    	var container = document.createElement('div');
+    	container.innerHTML = `<div class="columns is-multiline is-mobile" id="table"></div>`
+    	document.getElementById('container').appendChild(container);
+    	
+    	var table = document.getElementById('table');
+		for(var item of response.data.files){
+			var div = document.createElement('div');
+			div.className = "column is-2";
+			if(item.thumb !== undefined)
+				div.innerHTML = `<a href="${item.file}" target="_blank"><img src="${item.thumb}"/></a>`;
+			else
+				div.innerHTML = `<a href="${item.file}" target="_blank"><h1 class="title">.${item.file.split('.').pop()}</h1></a>`;
+			table.appendChild(div);
+		}
+  	})
+  	.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 () {
+	var identifier = document.location.pathname;
+	identifier = identifier.substring(3, identifier.length);
+	album.get(identifier);
+};

+ 1 - 0
routes/api.js

@@ -22,6 +22,7 @@ routes.post ('/upload', (req, res, next) => uploadController.upload(req, res, ne
 routes.post ('/upload/delete', (req, res, next) => uploadController.delete(req, res, next))
 routes.post ('/upload/:albumid', (req, res, next) => uploadController.upload(req, res, next))
 
+routes.get  ('/album/get/:identifier', (req, res, next) => albumsController.get(req, res, next))
 routes.get  ('/album/:id', (req, res, next) => uploadController.list(req, res, next))
 routes.get  ('/album/:id/:page', (req, res, next) => uploadController.list(req, res, next))