diff --git a/src/routes/index.ts b/src/routes/index.ts index 747131b..913378c 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -52,24 +52,40 @@ routes.post('/upload', upload.single('file'), async (req, res) => { if (!file) { logger.error('No file uploaded'); - return res.status(400).send('No file uploaded'); + return res.status(400).json({ + status: 'error', + code: 'FILE_MISSING', + message: 'No file uploaded' + }); } if (!bucket) { logger.error('No bucket provided'); - return res.status(400).send('No bucket provided'); + return res.status(400).json({ + status: 'error', + code: 'BUCKET_MISSING', + message: 'No bucket provided' + }); } if (!isValidFileType(file)) { logger.error(`Invalid file type: ${file.mimetype}`); - return res.status(400).send('Invalid file type. Please upload either a CSV or JSON file'); + return res.status(400).json({ + status: 'error', + code: 'INVALID_FILE_TYPE', + message: 'Invalid file type. Please upload either a CSV or JSON file' + }); } // For CSV files, validate headers if (file.mimetype === 'text/csv') { const headers = getCsvHeaders(file.buffer); if (!headers) { - return res.status(400).send('Invalid CSV file format'); + return res.status(400).json({ + status: 'error', + code: 'INVALID_CSV_FORMAT', + message: 'Invalid CSV file format' + }); } const fileUrl = saveCsvToTmp(file.buffer, file.originalname); try { @@ -77,27 +93,42 @@ routes.post('/upload', upload.single('file'), async (req, res) => { // Clean up the temporary file fs.unlinkSync(fileUrl); - if (uploadResult) { - return res.status(201).send(`File ${file.originalname} uploaded in bucket ${bucket}`); + if (uploadResult.success) { + return res.status(201).json({ + status: 'success', + code: 'UPLOAD_SUCCESS', + message: uploadResult.message + }); } else { - return res.status(400).send(`Object ${file.originalname} already exists in bucket ${bucket}`); + return res.status(400).json({ + status: 'error', + code: 'UPLOAD_FAILED', + message: uploadResult.message + }); } - } catch (error) { - // Clean up the temporary file in case of error - fs.unlinkSync(fileUrl); + } catch (error: unknown) { logger.error('Error uploading file to Minio:', error); - return res.status(500).send('Error uploading file'); + return res.status(500).json({ + status: 'error', + code: 'INTERNAL_SERVER_ERROR', + message: error instanceof Error ? error.message : 'Unknown error' + }); } } else if (file.mimetype === 'application/json') { if (!validateJsonFile(file.buffer)) { - return res.status(400).send('Invalid JSON file format'); + return res.status(400).json({ + status: 'error', + code: 'INVALID_JSON_FORMAT', + message: 'Invalid JSON file format' + }); } - return res.status(200).send('JSON file is valid - Future implementation'); - } else { - return res.status(400).send('Invalid file type. Please upload either a CSV or JSON file'); + return res.status(200).json({ + status: 'success', + code: 'JSON_VALID', + message: 'JSON file is valid - Future implementation' + }); } - }); export default routes; diff --git a/src/utils/minioClient.ts b/src/utils/minioClient.ts index d3ade97..7596528 100644 --- a/src/utils/minioClient.ts +++ b/src/utils/minioClient.ts @@ -27,11 +27,10 @@ export async function uploadToMinio(sourceFile: string, destinationObject: strin logger.info(`Bucket ${bucket} created in "${bucketRegion}".`); } - try { - const fileExists = await checkFileExists(destinationObject, bucket, fileType); - if (fileExists) { - return false; + const fileCheck = await checkFileExists(destinationObject, bucket, fileType); + if (fileCheck.exists) { + return { success: false, message: fileCheck.message }; } else { const metaData = { 'Content-Type': fileType, @@ -42,10 +41,11 @@ export async function uploadToMinio(sourceFile: string, destinationObject: strin // Upload the file await minioClient.fPutObject(bucket, destinationObject, sourceFile, metaData); logger.info(`File ${sourceFile} uploaded as object ${destinationObject} in bucket ${bucket}`); - return true; + return {success: true, message: `File ${sourceFile} uploaded as object ${destinationObject} in bucket ${bucket}`}; } } catch (error) { console.error('Error checking file:', error); + return {success: false, message: `Error uploading file: ${error}`}; } } @@ -55,7 +55,7 @@ export async function uploadToMinio(sourceFile: string, destinationObject: strin * @param {string} bucket - Bucket name * @returns {Promise} - Returns true if file exists, false otherwise */ -export async function checkFileExists(fileName: string, bucket: string, fileType: string): Promise { +export async function checkFileExists(fileName: string, bucket: string, fileType: string): Promise<{ exists: boolean, message: string }> { const minioClient = new Minio.Client({ endPoint, port, @@ -68,27 +68,36 @@ export async function checkFileExists(fileName: string, bucket: string, fileType // Check if bucket exists first const bucketExists = await minioClient.bucketExists(bucket); if (!bucketExists) { - logger.info(`Bucket ${bucket} does not exist`); - return false; + return { + exists: false, + message: `Bucket ${bucket} does not exist` + }; } // Get object stats to check if file exists - const stats = await minioClient.statObject(bucket, fileName); // Optionally verify it's a CSV file by checking Content-Type + const stats = await minioClient.statObject(bucket, fileName); if (stats.metaData && stats.metaData['content-type'] === fileType) { - logger.info(`File ${fileName} exists in bucket ${bucket}`); - return true; + return { + exists: true, + message: `File ${fileName} exists in bucket ${bucket}` + }; } else { - logger.info(`File ${fileName} does not exist in bucket ${bucket}`); - return false; + return { + exists: false, + message: `File ${fileName} does not exist in bucket ${bucket}` + }; } } catch (err: any) { if (err.code === 'NotFound') { - logger.debug(`File ${fileName} not found in bucket ${bucket}`); - return false; + return { + exists: false, + message: `File ${fileName} not found in bucket ${bucket}` + }; } - // For any other error, log it and rethrow - logger.error(`Error checking file existence: ${err.message}`); - throw err; + // For any other error, return error message + return { + exists: false, + message: `Error checking file existence: ${err.message}` + }; } - }