Browse Source

Made the album viewer route SSR so crawlers dont have issues 🎉

Pitu 6 years ago
parent
commit
6b72453d4a
3 changed files with 135 additions and 1 deletions
  1. 8 1
      lolisafe.js
  2. 61 0
      routes/album.js
  3. 66 0
      views/album.handlebars

+ 8 - 1
lolisafe.js

@@ -1,11 +1,13 @@
 const config = require('./config.js')
 const api = require('./routes/api.js')
+const album = require('./routes/album.js')
 const express = require('express')
 const helmet = require('helmet')
 const bodyParser = require('body-parser')
 const RateLimit = require('express-rate-limit')
 const db = require('knex')(config.database)
 const fs = require('fs')
+const exphbs  = require('express-handlebars')
 const safe = express()
 
 require('./database/db.js')(db)
@@ -18,6 +20,10 @@ fs.existsSync('./' + config.uploads.folder + '/thumbs') || fs.mkdirSync('./' + c
 safe.use(helmet())
 safe.set('trust proxy', 1)
 
+safe.engine('handlebars', exphbs({defaultLayout: 'main'}))
+safe.set('view engine', 'handlebars')
+safe.enable('view cache')
+
 let limiter = new RateLimit({ windowMs: 5000, max: 2 })
 safe.use('/api/login/', limiter)
 safe.use('/api/register/', limiter)
@@ -27,8 +33,9 @@ safe.use(bodyParser.json())
 
 safe.use('/', express.static('./uploads'))
 safe.use('/', express.static('./public'))
+safe.use('/', album)
 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/'

+ 61 - 0
routes/album.js

@@ -0,0 +1,61 @@
+const config = require('../config.js')
+const routes = require('express').Router()
+const db = require('knex')(config.database)
+const path = require('path')
+const utils = require('../controllers/utilsController.js')
+
+routes.get('/a/:identifier', (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 thumb = ''
+			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
+
+				let ext = path.extname(file.name).toLowerCase()
+				if (utils.extensions.includes(ext)) {
+					file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png'
+
+					/*
+						If thumbnail for album is still not set, do it.
+						A potential improvement would be to let the user upload a specific image as an album cover
+						since embedding the first image could potentially result in nsfw content when pasting links.
+					*/
+
+					if (thumb === '') {
+						thumb = file.thumb
+					}
+
+					file.thumb = `<img src="${file.thumb}"/>`
+				} else {
+					file.thumb = `<h1 class="title">.${ext}</h1>`
+				}
+			}
+
+			return res.render('album', {
+				layout: false,
+				title: title,
+				count: files.length,
+				thumb: files[0].
+				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 = routes

+ 66 - 0
views/album.handlebars

@@ -0,0 +1,66 @@
+<!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="{{ title }} | {{ count }} files" />
+		<meta property="og:description" content="lolisafe.moe | A small safe worth protecting." />
+		<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="{{ title }} | {{ count }} files">
+		<meta name="twitter:description" content="lolisafe.moe | A small safe worth protecting.">
+		<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>{{ title }}</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;'>{{ title }}</h1>
+					<h1 class="subtitle" id='count'> {{ count }} files</h1>
+					<hr>
+				</div>
+			</div>
+			<div class="hero-body">
+				<div class="container" id='container'>
+					<div class="columns is-multiline is-mobile" id="table">
+						{{#each files}}
+						<div class="column is-2">
+							<a href="{{this.file}}" target="_blank">{{{this.thumb}}}</a>
+						</div>
+						{{/each}}
+					</div>
+				</div>
+			</div>
+		</section>
+
+	</body>
+</html>