* Better auth handling.

* Deleting a file will no longer cause the dashboard to load the very first page of uploaded files list. It will instead reload the currently viewed page.

* Updated dropzone (I guess).
This commit is contained in:
Bobby Wibowo 2018-03-25 02:47:41 +07:00
parent 83f3b36f15
commit 3fa5b24ee5
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
11 changed files with 45 additions and 26 deletions

View File

@ -2,7 +2,7 @@ module.exports = {
/*
If set to true the user will need to specify the auto-generated token
on each API call, meaning random strangers wont be able to use the service
on each API call, meaning random strangers won't be able to use the service
unless they have the token loli-safe provides you with.
If it's set to false, then upload will be public for anyone to use.
*/

View File

@ -10,6 +10,7 @@ const albumsController = {}
albumsController.list = async (req, res, next) => {
const albumDomain = config.albumDomain || config.domain
const user = await utils.authorize(req, res)
if (!user) return
const fields = ['id', 'name']
if (req.params.sidebar === undefined) {
@ -43,6 +44,7 @@ albumsController.list = async (req, res, next) => {
albumsController.create = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) return
const name = req.body.name
if (name === undefined || name === '') {
@ -74,6 +76,7 @@ albumsController.create = async (req, res, next) => {
albumsController.delete = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) return
const id = req.body.id
if (id === undefined || id === '') {
@ -86,6 +89,7 @@ albumsController.delete = async (req, res, next) => {
albumsController.rename = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) return
const id = req.body.id
if (id === undefined || id === '') {

View File

@ -71,6 +71,7 @@ authController.register = async (req, res, next) => {
authController.changePassword = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) return
let password = req.body.password
if (password === undefined) return res.json({ success: false, description: 'No password provided.' })
@ -92,6 +93,7 @@ authController.changePassword = async (req, res, next) => {
authController.getFileLengthConfig = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) return
return res.json({ success: true, fileLength: user.fileLength, config: config.uploads.fileLength })
}
@ -101,13 +103,14 @@ authController.changeFileLength = async (req, res, next) => {
}
const user = await utils.authorize(req, res)
if (!user) return
let fileLength = parseInt(req.body.fileLength)
if (fileLength === undefined) return res.json({ success: false, description: 'No file name length provided.' })
if (isNaN(fileLength)) return res.json({ success: false, description: 'File name length is not a valid number.' })
if (fileLength < config.uploads.fileLength.min || fileLength > config.uploads.fileLength.max) {
return res.json({ success: false, description: `File name length must be ${config.uploads.fileLength.min} to ${config.uploads.fileLength.max} characters` })
return res.json({ success: false, description: `File name length must be ${config.uploads.fileLength.min} to ${config.uploads.fileLength.max} characters.` })
}
if (fileLength === user.fileLength) {

View File

@ -16,13 +16,15 @@ tokenController.verify = async (req, res, next) => {
tokenController.list = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) return
return res.json({ success: true, token: user.token })
}
tokenController.change = async (req, res, next) => {
const user = await utils.authorize(req, res)
const newtoken = randomstring.generate(64)
if (!user) return
const newtoken = randomstring.generate(64)
await db.table('users').where('token', user.token).update({
token: newtoken,
timestamp: Math.floor(Date.now() / 1000)

View File

@ -48,12 +48,14 @@ const upload = multer({
}).array('files[]')
uploadsController.upload = async (req, res, next) => {
let user
if (config.private === true) {
await utils.authorize(req, res)
user = await utils.authorize(req, res)
if (!user) return
} else if (req.headers.token) {
user = await db.table('users').where('token', req.headers.token).first()
}
const token = req.headers.token || ''
const user = await db.table('users').where('token', token).first()
if (user && (user.enabled === false || user.enabled === 0)) {
return res.json({
success: false,
@ -78,7 +80,7 @@ uploadsController.upload = async (req, res, next) => {
return uploadsController.actuallyUpload(req, res, user, albumid)
}
uploadsController.actuallyUpload = async (req, res, userid, album) => {
uploadsController.actuallyUpload = async (req, res, user, album) => {
upload(req, res, async err => {
if (err) {
console.error(err)
@ -104,8 +106,8 @@ uploadsController.actuallyUpload = async (req, res, userid, album) => {
const fileHash = hash.digest('hex')
const dbFile = await db.table('files')
.where(function () {
if (userid === undefined) this.whereNull('userid')
else this.where('userid', userid.id)
if (user === undefined) this.whereNull('userid')
else this.where('userid', user.id)
})
.where({
hash: fileHash,
@ -122,7 +124,7 @@ uploadsController.actuallyUpload = async (req, res, userid, album) => {
hash: fileHash,
ip: req.ip,
albumid: album,
userid: userid !== undefined ? userid.id : null,
userid: user !== undefined ? user.id : null,
timestamp: Math.floor(Date.now() / 1000)
})
} else {
@ -184,6 +186,7 @@ uploadsController.processFilesForDisplay = async (req, res, files, existingFiles
uploadsController.delete = async (req, res) => {
const user = await utils.authorize(req, res)
if (!user) return
const id = req.body.id
if (id === undefined || id === '') {
return res.json({ success: false, description: 'No file specified.' })
@ -244,6 +247,7 @@ uploadsController.deleteFile = function (file) {
uploadsController.list = async (req, res) => {
const user = await utils.authorize(req, res)
if (!user) return
let offset = req.params.page
if (offset === undefined) offset = 0

View File

@ -41,11 +41,14 @@ utilsController.getPrettyBytes = function (num) {
utilsController.authorize = async (req, res) => {
const token = req.headers.token
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided.' })
if (token === undefined) {
res.status(401).json({ success: false, description: 'No token provided.' })
return
}
const user = await db.table('users').where('token', token).first()
if (!user) return res.status(401).json({ success: false, description: 'Invalid token.' })
return user
if (user) return user
res.status(401).json({ success: false, description: 'Invalid token.' })
}
utilsController.generateThumbs = function (file, basedomain) {

View File

@ -16,7 +16,7 @@
<link rel="stylesheet" type="text/css" href="css/dashboard.css?v=XcTZuW9fFV">
<script type="text/javascript" src="libs/sweetalert/sweetalert.min.js?v=V2RnA3Mwhh"></script>
<script type="text/javascript" src="libs/axios/axios.min.js?v=V2RnA3Mwhh"></script>
<script type="text/javascript" src="js/dashboard.js?v=XcTZuW9fFV"></script>
<script type="text/javascript" src="js/dashboard.js?v=a8gMjxPkDm"></script>
<!-- Open Graph tags -->
<meta property="og:type" content="website" />

View File

@ -15,7 +15,7 @@
<script type="text/javascript" src="libs/sweetalert/sweetalert.min.js?v=V2RnA3Mwhh"></script>
<script type="text/javascript" src="libs/dropzone/dropzone.min.js?v=V2RnA3Mwhh"></script>
<script type="text/javascript" src="libs/axios/axios.min.js?v=V2RnA3Mwhh"></script>
<script type="text/javascript" src="js/home.js?v=V2RnA3Mwhh"></script>
<script type="text/javascript" src="js/home.js?v=a8gMjxPkDm"></script>
<!-- Open Graph tags -->
<meta property="og:type" content="website" />

View File

@ -106,8 +106,8 @@ panel.getUploads = function (album = undefined, page = undefined) {
var pagination = `
<nav class="pagination is-centered">
<a class="pagination-previous" onclick="panel.getUploads(${album}, ${prevPage} )">Previous</a>
<a class="pagination-next" onclick="panel.getUploads(${album}, ${nextPage} )">Next page</a>
<a class="pagination-previous" onclick="panel.getUploads(${album}, ${prevPage})">Previous</a>
<a class="pagination-next" onclick="panel.getUploads(${album}, ${nextPage})">Next page</a>
</nav>
`
var listType = `
@ -150,7 +150,7 @@ panel.getUploads = function (album = undefined, page = undefined) {
div.innerHTML = `<a class="image" href="${item.file}" target="_blank"><h1 class="title">.${item.file.split('.').pop()}</h1></a>`
}
div.innerHTML += `
<a class="button is-small is-danger is-outlined" title="Delete album" onclick="panel.deleteFile(${item.id})">
<a class="button is-small is-danger is-outlined" title="Delete album" onclick="panel.deleteFile(${item.id}, ${album}, ${page})">
<span class="icon is-small">
<i class="fa icon-trash"></i>
</span>
@ -191,7 +191,6 @@ panel.getUploads = function (album = undefined, page = undefined) {
var tr = document.createElement('tr')
var displayAlbumOrUser = item.album
console.log(item)
if (panel.username === 'root') {
displayAlbumOrUser = ''
if (item.username !== undefined) { displayAlbumOrUser = item.username }
@ -204,7 +203,7 @@ panel.getUploads = function (album = undefined, page = undefined) {
<td>${item.size}</td>
<td>${item.date}</td>
<td>
<a class="button is-small is-danger is-outlined" title="Delete album" onclick="panel.deleteFile(${item.id})">
<a class="button is-small is-danger is-outlined" title="Delete album" onclick="panel.deleteFile(${item.id}, ${album}, ${page})">
<span class="icon is-small">
<i class="fa icon-trash"></i>
</span>
@ -229,10 +228,10 @@ panel.setFilesView = function (view, album, page) {
panel.getUploads(album, page)
}
panel.deleteFile = function (id) {
panel.deleteFile = function (id, album = undefined, page = undefined) {
swal({
title: 'Are you sure?',
text: 'You wont be able to recover the file!',
text: 'You won\'t be able to recover the file!',
icon: 'warning',
dangerMode: true,
buttons: {
@ -254,7 +253,7 @@ panel.deleteFile = function (id) {
}
swal('Deleted!', 'The file has been deleted.', 'success')
panel.getUploads()
panel.getUploads(album, page)
})
.catch(function (error) {
console.log(error)

View File

@ -153,10 +153,11 @@ upload.prepareDropzone = function () {
dropzone.on('success', (file, response) => {
// Handle the responseText here. For example, add the text to the preview element:
file.previewTemplate.querySelector('.progress').style.display = 'none'
if (response.success === false) {
var span = document.createElement('span')
span.innerHTML = response.description
span.innerHTML = response.description || response
file.previewTemplate.querySelector('.link').appendChild(span)
return
}
@ -166,7 +167,10 @@ upload.prepareDropzone = function () {
a.target = '_blank'
a.innerHTML = response.files[0].url
file.previewTemplate.querySelector('.link').appendChild(a)
})
dropzone.on('error', (file, error) => {
console.error(error)
file.previewTemplate.querySelector('.progress').style.display = 'none'
})

File diff suppressed because one or more lines are too long