From 651cd28698ab4e63ae4da0a0cd29dbeaf4a9bd65 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sun, 19 Jan 2020 22:21:57 +0200 Subject: [PATCH] improve things --- applications/tempfiles/index.js | 34 +++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/applications/tempfiles/index.js b/applications/tempfiles/index.js index 0e39383..ac2e7c4 100644 --- a/applications/tempfiles/index.js +++ b/applications/tempfiles/index.js @@ -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] = '' + uploadedFiles[i] + '' + uploadedFiles[i] = '' + uploadedFiles[i] + '' } } @@ -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