improve things

This commit is contained in:
Evert Prants 2020-01-19 22:21:57 +02:00
parent fbfd89fc12
commit 651cd28698
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

View File

@ -169,8 +169,9 @@ async function init () {
if (uploadedFiles.length === 0) return res.status(400).send('No files were uploaded') if (uploadedFiles.length === 0) return res.status(400).send('No files were uploaded')
let tagify = fields && fields['tagify'] let tagify = fields && fields['tagify']
if (tagify != null) { if (tagify != null) {
let a = uploadedFiles.length > 1 ? ' target="_blank"' : ''
for (let i in uploadedFiles) { for (let i in uploadedFiles) {
uploadedFiles[i] = '<a href="' + uploadedFiles[i] + '" target="_blank">' + uploadedFiles[i] + '</a>' uploadedFiles[i] = '<a href="' + uploadedFiles[i] + '"' + a + '>' + uploadedFiles[i] + '</a>'
} }
} }
@ -198,23 +199,28 @@ async function init () {
} }
let db = await dbPromise let db = await dbPromise
// Get a hash that isnt in use
let use let use
for (let i = 0; i < 8; i++) { let existing = await db.get('SELECT hash FROM Short WHERE url = ?', url)
let add = Math.floor(i / 2)
let hash = crypto.randomBytes((config.shortener.bytes || 2) + add).toString('hex') if (existing) {
let exists = await db.get('SELECT timeat FROM Short WHERE hash = ?', hash) use = existing.hash
if (!exists) { } else {
use = hash // Get a hash that isnt in use
break for (let i = 0; i < 8; i++) {
let add = Math.floor(i / 2)
let hash = crypto.randomBytes((config.shortener.bytes || 2) + add).toString('hex')
let exists = await db.get('SELECT timeat FROM Short WHERE hash = ?', hash)
if (!exists) {
use = hash
break
}
} }
if (!use) throw new Error('Server could not find a proper hash for some reason')
await db.run('INSERT INTO Short (url,hash,timeat,ip) VALUES (?,?,?,?)', url, use, Date.now(), ip)
} }
if (!use) throw new Error('Server could not find a proper hash for some reason')
await db.run('INSERT INTO Short (url,hash,timeat,ip) VALUES (?,?,?,?)', url, use, Date.now(), ip)
let ua = req.get('User-Agent') let ua = req.get('User-Agent')
let reqRaw = false let reqRaw = false
if (!reqRaw && ua && (ua.match(/curl\//i) != null || ua.match(/wget\//i) != null)) reqRaw = true if (!reqRaw && ua && (ua.match(/curl\//i) != null || ua.match(/wget\//i) != null)) reqRaw = true