Added check for duplicate ID's (closes #75)

pull/77/head
tycrek 3 years ago
parent b801ae43a5
commit 29f7f25f38
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -64,10 +64,28 @@ router.post('/', (req, res, next) => {
// Fix spaces in originalname
req.file.originalname = req.file.originalname.replace(/\s/g, spaceReplace === '!' ? '' : spaceReplace);
// Save the file information
const resourceId = generateId(generator, resourceIdSize, req.headers['x-ass-gfycat'] || gfyIdSize, req.file.originalname);
// Generate a unique resource ID
let resourceId;
// Function to call to generate a fresh ID. Used for multiple attempts in case an ID is already taken
const gen = () => generateId(generator, resourceIdSize, req.headers['x-ass-gfycat'] || gfyIdSize, req.file.originalname);
// Called by a promise, this will recursively resolve itself until a unique ID is found
// TODO: Add max attempts in case all available ID's are taken
function genCheckId(resolve, reject) {
let uniqueId = gen();
data.has(uniqueId)
.then((exists) => (log.debug('ID check', exists ? 'Taken' : 'Available'), exists ? genCheckId(resolve, reject) : resolve(uniqueId)))
.catch(reject);
}
new Promise((resolve, reject) => genCheckId(resolve, reject))
.then((uniqueId) => {
resourceId = uniqueId;
log.debug('Saving data', data.name);
data.put(resourceId.split('.')[0], req.file).then(() => {
})
.then(() => data.put(resourceId.split('.')[0], req.file))
.then(() => {
// Log the upload
const logInfo = `${req.file.originalname} (${req.file.mimetype}, ${formatBytes(req.file.size)})`;
log.success('File uploaded', logInfo, `uploaded by ${users[req.token] ? users[req.token].username : '<token-only>'}`);

Loading…
Cancel
Save