mirror of
https://github.com/DumbWareio/DumbDrop.git
synced 2025-10-23 07:41:58 +00:00
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:
14
server.js
14
server.js
@@ -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(/^(\.\.(\/|\\|$))+/, '');
|
||||||
|
Reference in New Issue
Block a user