From 79f6c5387163f62ae2296e5463509801b012ab24 Mon Sep 17 00:00:00 2001 From: Greirson Lee-Thorp Date: Mon, 3 Feb 2025 17:14:03 -0800 Subject: [PATCH] feat: improve single file upload batch ID generation - Add automatic batch ID generation for single file uploads - Generate unique batch ID using timestamp and random string - Enhance batch ID validation to handle single file and multi-file upload scenarios - Improve error handling for batch ID format validation --- server.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index fae67e0..a7667c2 100644 --- a/server.js +++ b/server.js @@ -303,12 +303,16 @@ function isValidBatchId(batchId) { // Routes app.post('/upload/init', async (req, res) => { const { filename, fileSize } = req.body; - const batchId = req.headers['x-batch-id']; + let batchId = req.headers['x-batch-id']; - // Validate batch ID - if (!batchId || !isValidBatchId(batchId)) { - log.error('Invalid or missing batch ID'); - return res.status(400).json({ error: 'Invalid or missing batch ID' }); + // For single file uploads without a batch ID, generate one + if (!batchId) { + const timestamp = Date.now(); + const randomStr = crypto.randomBytes(4).toString('hex').substring(0, 9); + batchId = `${timestamp}-${randomStr}`; + } else if (!isValidBatchId(batchId)) { + log.error('Invalid batch ID format'); + return res.status(400).json({ error: 'Invalid batch ID format' }); } const safeFilename = path.normalize(filename).replace(/^(\.\.(\/|\\|$))+/, '');