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
1 changed files with 20 additions and 14 deletions

View File

@ -169,8 +169,9 @@ async function init () {
if (uploadedFiles.length === 0) return res.status(400).send('No files were uploaded')
let tagify = fields && fields['tagify']
if (tagify != null) {
let a = uploadedFiles.length > 1 ? ' target="_blank"' : ''
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
// Get a hash that isnt in use
let use
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
let existing = await db.get('SELECT hash FROM Short WHERE url = ?', url)
if (existing) {
use = existing.hash
} else {
// Get a hash that isnt in use
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 reqRaw = false
if (!reqRaw && ua && (ua.match(/curl\//i) != null || ua.match(/wget\//i) != null)) reqRaw = true