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
This commit is contained in:
Greirson Lee-Thorp
2025-02-03 17:14:03 -08:00
parent dec54b7803
commit 79f6c53871

View File

@@ -303,12 +303,16 @@ function isValidBatchId(batchId) {
// Routes // Routes
app.post('/upload/init', async (req, res) => { app.post('/upload/init', async (req, res) => {
const { filename, fileSize } = req.body; const { filename, fileSize } = req.body;
const batchId = req.headers['x-batch-id']; let batchId = req.headers['x-batch-id'];
// Validate batch ID // For single file uploads without a batch ID, generate one
if (!batchId || !isValidBatchId(batchId)) { if (!batchId) {
log.error('Invalid or missing batch ID'); const timestamp = Date.now();
return res.status(400).json({ error: 'Invalid or missing batch ID' }); 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(/^(\.\.(\/|\\|$))+/, ''); const safeFilename = path.normalize(filename).replace(/^(\.\.(\/|\\|$))+/, '');