mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-13 16:06:21 +00:00
Refactor
This commit is contained in:
parent
8a96a0df1d
commit
1db1b06a48
@ -1,10 +1,7 @@
|
|||||||
const config = require('../config.js')
|
const config = require('../config.js')
|
||||||
const db = require('knex')(config.database)
|
const db = require('knex')(config.database)
|
||||||
const randomstring = require('randomstring')
|
const randomstring = require('randomstring')
|
||||||
const path = require('path')
|
const utils = require('utilsController.js')
|
||||||
const fs = require('fs')
|
|
||||||
const ffmpeg = require('fluent-ffmpeg')
|
|
||||||
const gm = require('gm')
|
|
||||||
|
|
||||||
let albumsController = {}
|
let albumsController = {}
|
||||||
|
|
||||||
@ -31,7 +28,7 @@ albumsController.list = function(req, res, next){
|
|||||||
let ids = []
|
let ids = []
|
||||||
for (let album of albums) {
|
for (let album of albums) {
|
||||||
album.date = new Date(album.timestamp * 1000)
|
album.date = new Date(album.timestamp * 1000)
|
||||||
album.date = album.date.getFullYear() + '-' + (album.date.getMonth() + 1) + '-' + album.date.getDate() + ' ' + (album.date.getHours() < 10 ? '0' : '') + album.date.getHours() + ':' + (album.date.getMinutes() < 10 ? '0' : '') + album.date.getMinutes() + ':' + (album.date.getSeconds() < 10 ? '0' : '') + album.date.getSeconds()
|
album.date = utils.getPrettyDate(album.date) // album.date.getFullYear() + '-' + (album.date.getMonth() + 1) + '-' + album.date.getDate() + ' ' + (album.date.getHours() < 10 ? '0' : '') + album.date.getHours() + ':' + (album.date.getMinutes() < 10 ? '0' : '') + album.date.getMinutes() + ':' + (album.date.getSeconds() < 10 ? '0' : '') + album.date.getSeconds()
|
||||||
|
|
||||||
let basedomain = req.get('host')
|
let basedomain = req.get('host')
|
||||||
for (let domain of config.domains)
|
for (let domain of config.domains)
|
||||||
@ -100,8 +97,9 @@ albumsController.delete = function(req, res, next){
|
|||||||
if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
|
if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
|
||||||
|
|
||||||
let id = req.body.id
|
let id = req.body.id
|
||||||
if(id === undefined || id === '')
|
if (id === undefined || id === ''){
|
||||||
return res.json({ success: false, description: 'No album specified' })
|
return res.json({ success: false, description: 'No album specified' })
|
||||||
|
}
|
||||||
|
|
||||||
db.table('albums').where({ id: id, userid: user[0].id }).update({ enabled: 0 }).then(() => {
|
db.table('albums').where({ id: id, userid: user[0].id }).update({ enabled: 0 }).then(() => {
|
||||||
return res.json({ success: true })
|
return res.json({ success: true })
|
||||||
@ -155,52 +153,7 @@ albumsController.get = function(req, res, next){
|
|||||||
|
|
||||||
for (let file of files) {
|
for (let file of files) {
|
||||||
file.file = basedomain + '/' + file.name
|
file.file = basedomain + '/' + file.name
|
||||||
|
utils.generateThumbs(file)
|
||||||
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({
|
return res.json({
|
||||||
@ -212,7 +165,6 @@ albumsController.get = 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' }) })
|
||||||
}).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
|
module.exports = albumsController
|
@ -83,7 +83,6 @@ authController.changePassword = 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' }) })
|
||||||
})
|
})
|
||||||
}).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 = authController
|
module.exports = authController
|
@ -41,7 +41,6 @@ tokenController.change = function(req, res, next){
|
|||||||
}).then(() => {
|
}).then(() => {
|
||||||
res.json({ success: true, token: newtoken })
|
res.json({ success: true, token: newtoken })
|
||||||
}).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 = tokenController
|
module.exports = tokenController
|
@ -5,14 +5,13 @@ const randomstring = require('randomstring')
|
|||||||
const db = require('knex')(config.database)
|
const db = require('knex')(config.database)
|
||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const gm = require('gm')
|
const utils = require('utilsController.js')
|
||||||
const ffmpeg = require('fluent-ffmpeg')
|
|
||||||
|
|
||||||
let uploadsController = {}
|
let uploadsController = {}
|
||||||
|
|
||||||
const storage = multer.diskStorage({
|
const storage = multer.diskStorage({
|
||||||
destination: function(req, file, cb) {
|
destination: function(req, file, cb) {
|
||||||
cb(null, './' + config.uploads.folder + '/')
|
cb(null, path.join(__dirname, '..', config.uploads.folder))
|
||||||
},
|
},
|
||||||
filename: function(req, file, cb) {
|
filename: function(req, file, cb) {
|
||||||
cb(null, randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname))
|
cb(null, randomstring.generate(config.uploads.fileLength) + path.extname(file.originalname))
|
||||||
@ -38,11 +37,10 @@ uploadsController.upload = function(req, res, next){
|
|||||||
|
|
||||||
db.table('users').where('token', token).then((user) => {
|
db.table('users').where('token', token).then((user) => {
|
||||||
let userid
|
let userid
|
||||||
if(user.length > 0)
|
if (user.length > 0) userid = user[0].id
|
||||||
userid = user[0].id
|
|
||||||
|
|
||||||
// Check if user is trying to upload to an album
|
// Check if user is trying to upload to an album
|
||||||
let album = undefined
|
let album
|
||||||
if (userid !== undefined) {
|
if (userid !== undefined) {
|
||||||
album = req.headers.albumid
|
album = req.headers.albumid
|
||||||
if (album === undefined)
|
if (album === undefined)
|
||||||
@ -68,14 +66,14 @@ uploadsController.upload = function(req, res, next){
|
|||||||
|
|
||||||
// Check if the file exists by checking hash and size
|
// Check if the file exists by checking hash and size
|
||||||
let hash = crypto.createHash('md5')
|
let hash = crypto.createHash('md5')
|
||||||
let stream = fs.createReadStream('./' + config.uploads.folder + '/' + file.filename)
|
let stream = fs.createReadStream(path.join(__dirname, '..', config.uploads.folder, file.filename))
|
||||||
|
|
||||||
stream.on('data', function (data) {
|
stream.on('data', function (data) {
|
||||||
hash.update(data, 'utf8')
|
hash.update(data, 'utf8')
|
||||||
})
|
})
|
||||||
|
|
||||||
stream.on('end', function () {
|
stream.on('end', function () {
|
||||||
let fileHash = hash.digest('hex') // 34f7a3113803f8ed3b8fd7ce5656ebec
|
let fileHash = hash.digest('hex')
|
||||||
|
|
||||||
db.table('files')
|
db.table('files')
|
||||||
.where(function() {
|
.where(function() {
|
||||||
@ -109,7 +107,6 @@ uploadsController.upload = function(req, res, next){
|
|||||||
if (iteration === req.files.length)
|
if (iteration === req.files.length)
|
||||||
return uploadsController.processFilesForDisplay(req, res, files, existingFiles)
|
return uploadsController.processFilesForDisplay(req, res, files, existingFiles)
|
||||||
iteration++
|
iteration++
|
||||||
|
|
||||||
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -154,7 +151,7 @@ uploadsController.processFilesForDisplay = function(req, res, files, existingFil
|
|||||||
})
|
})
|
||||||
|
|
||||||
for (let file of files) {
|
for (let file of files) {
|
||||||
uploadsController.generateThumbs(file, basedomain)
|
utils.generateThumbs(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
||||||
@ -193,15 +190,14 @@ uploadsController.delete = function(req, res){
|
|||||||
|
|
||||||
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
||||||
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadsController.deleteFile = function(file) {
|
uploadsController.deleteFile = function(file) {
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
fs.stat('./' + config.uploads.folder + '/' + file, function (err, stats) {
|
fs.stat(path.join(__dirname, '..', config.uploads.folder, file), function(err, stats) {
|
||||||
if (err) { return reject(err) }
|
if (err) { return reject(err) }
|
||||||
fs.unlink('./' + config.uploads.folder + '/' + file, function(err){
|
fs.unlink(path.join(__dirname, '..', config.uploads.folder, file), function(err) {
|
||||||
if (err) { return reject(err) }
|
if (err) { return reject(err) }
|
||||||
return resolve()
|
return resolve()
|
||||||
})
|
})
|
||||||
@ -250,7 +246,7 @@ uploadsController.list = function(req, res){
|
|||||||
for (let file of files) {
|
for (let file of files) {
|
||||||
file.file = basedomain + '/' + file.name
|
file.file = basedomain + '/' + file.name
|
||||||
file.date = new Date(file.timestamp * 1000)
|
file.date = new Date(file.timestamp * 1000)
|
||||||
file.date = file.date.getFullYear() + '-' + (file.date.getMonth() + 1) + '-' + 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()
|
file.date = utils.getPrettyDate(file.date) // file.date.getFullYear() + '-' + (file.date.getMonth() + 1) + '-' + 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()
|
||||||
|
|
||||||
file.album = ''
|
file.album = ''
|
||||||
|
|
||||||
@ -264,7 +260,7 @@ uploadsController.list = function(req, res){
|
|||||||
if (file.userid !== undefined && file.userid !== null && file.userid !== '')
|
if (file.userid !== undefined && file.userid !== null && file.userid !== '')
|
||||||
userids.push(file.userid)
|
userids.push(file.userid)
|
||||||
|
|
||||||
uploadsController.generateThumbs(file, basedomain)
|
utils.generateThumbs(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we are a normal user, send response
|
// If we are a normal user, send response
|
||||||
@ -283,53 +279,7 @@ uploadsController.list = function(req, res){
|
|||||||
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
||||||
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
||||||
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadsController.generateThumbs = function(file, basedomain){
|
|
||||||
if(config.uploads.generateThumbnails !== true) return
|
|
||||||
|
|
||||||
let extensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webm', '.mp4']
|
|
||||||
for(let ext of extensions){
|
|
||||||
if(path.extname(file.name).toLowerCase() === ext){
|
|
||||||
|
|
||||||
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(path.join(__dirname, '..', config.uploads.folder, file.name))
|
|
||||||
.thumbnail({
|
|
||||||
timestamps: [0],
|
|
||||||
filename: '%b.png',
|
|
||||||
folder: path.join(__dirname, '..', config.uploads.folder, 'thumbs'),
|
|
||||||
size: '200x?'
|
|
||||||
})
|
|
||||||
.on('error', function(error) {
|
|
||||||
console.log('Error - ', error.message)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
let size = {
|
|
||||||
width: 200,
|
|
||||||
height: 200
|
|
||||||
}
|
|
||||||
|
|
||||||
gm(path.join(__dirname, '..', 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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = uploadsController
|
module.exports = uploadsController
|
||||||
|
21
lolisafe.js
21
lolisafe.js
@ -2,7 +2,7 @@ const config = require('./config.js')
|
|||||||
const api = require('./routes/api.js')
|
const api = require('./routes/api.js')
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const bodyParser = require('body-parser')
|
const bodyParser = require('body-parser')
|
||||||
const rateLimit = require('express-rate-limit')
|
const RateLimit = require('express-rate-limit')
|
||||||
const db = require('knex')(config.database)
|
const db = require('knex')(config.database)
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const safe = express()
|
const safe = express()
|
||||||
@ -16,7 +16,7 @@ fs.existsSync('./' + config.uploads.folder + '/thumbs') || fs.mkdirSync('./' + c
|
|||||||
|
|
||||||
safe.set('trust proxy', 1)
|
safe.set('trust proxy', 1)
|
||||||
|
|
||||||
let limiter = new rateLimit({ windowMs: 5000, max: 2 })
|
let limiter = new RateLimit({ windowMs: 5000, max: 2 })
|
||||||
safe.use('/api/login/', limiter)
|
safe.use('/api/login/', limiter)
|
||||||
safe.use('/api/register/', limiter)
|
safe.use('/api/register/', limiter)
|
||||||
|
|
||||||
@ -30,20 +30,17 @@ safe.get('/a/:identifier', (req, res, next) => res.sendFile('album.html', {root:
|
|||||||
|
|
||||||
for (let page of config.pages) {
|
for (let page of config.pages) {
|
||||||
let root = './pages/'
|
let root = './pages/'
|
||||||
if(fs.existsSync(`./pages/custom/${page}.html`))
|
if (fs.existsSync(`./pages/custom/${page}.html`)) {
|
||||||
root = './pages/custom/'
|
root = './pages/custom/'
|
||||||
|
}
|
||||||
if(page === 'home') safe.get('/', (req, res, next) => res.sendFile(`${page}.html`, { root: root }))
|
if (page === 'home') {
|
||||||
else safe.get(`/${page}`, (req, res, next) => res.sendFile(`${page}.html`, { root: root }))
|
safe.get('/', (req, res, next) => res.sendFile(`${page}.html`, { root: root }))
|
||||||
|
} else {
|
||||||
|
safe.get(`/${page}`, (req, res, next) => res.sendFile(`${page}.html`, { root: root }))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
safe.use((req, res, next) => res.status(404).sendFile('404.html', { root: './pages/error/' }))
|
safe.use((req, res, next) => res.status(404).sendFile('404.html', { root: './pages/error/' }))
|
||||||
safe.use((req, res, next) => res.status(500).sendFile('500.html', { root: './pages/error/' }))
|
safe.use((req, res, next) => res.status(500).sendFile('500.html', { root: './pages/error/' }))
|
||||||
|
|
||||||
safe.listen(config.port, () => console.log(`loli-safe started on port ${config.port}`))
|
safe.listen(config.port, () => console.log(`loli-safe started on port ${config.port}`))
|
||||||
|
|
||||||
safe.prepareFrontendRoutes = function(){
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user