Skip to content

Commit

Permalink
86c12bkmm - Climate mediator - Duplicate CSV Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantpatil1214 committed Nov 19, 2024
1 parent 8e66062 commit 878984d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 15 deletions.
20 changes: 15 additions & 5 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,24 @@ routes.post('/upload', upload.single('file'), async (req, res) => {
return res.status(400).send('Invalid file type, please upload a valid CSV file');
}
const fileUrl = saveCsvToTmp(file.buffer, file.originalname);

const uploadResult = await uploadToMinio(fileUrl,file.originalname, bucket as string);
// const tableCreated = await createTable(headers, bucket as string);
logger.info(`file created: ${file.originalname}`);

fs.unlinkSync(fileUrl);
try {
const uploadResult = await uploadToMinio(fileUrl, file.originalname, bucket as string);
// Clean up the temporary file
fs.unlinkSync(fileUrl);

return res.status(201).send('File uploaded successfully');
if (uploadResult) {
return res.status(201).send(`File ${file.originalname} uploaded in bucket ${bucket}`);
} else {
return res.status(400).send(`Object ${file.originalname} already exists in bucket ${bucket}`);
}
} catch (error) {
// Clean up the temporary file in case of error
fs.unlinkSync(fileUrl);
logger.error('Error uploading file to Minio:', error);
return res.status(500).send('Error uploading file');
}
});

export default routes;
73 changes: 63 additions & 10 deletions src/utils/minioClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,74 @@ export async function uploadToMinio(sourceFile: string, destinationObject: strin
accessKey,
secretKey
});

// Check if bucket exists, create if it doesn't
const exists = await minioClient.bucketExists(bucket);
if (!exists) {
await minioClient.makeBucket(bucket, bucketRegion);
logger.debug(`Bucket ${bucket} created in "${bucketRegion}".`);
logger.info(`Bucket ${bucket} created in "${bucketRegion}".`);
}

// Set the object metadata
const metaData = {
'Content-Type': 'text/plain',
...customMetadata
};

// Upload the file
await minioClient.fPutObject(bucket, destinationObject, sourceFile, metaData);
logger.debug(`File ${sourceFile} uploaded as object ${destinationObject} in bucket ${bucket}`);
try {
const fileExists = await checkCsvFileExists(destinationObject, bucket);
if (fileExists) {
return false;
} else {
const metaData = {
'Content-Type': 'text/plain',
...customMetadata
};

// Upload the file
await minioClient.fPutObject(bucket, destinationObject, sourceFile, metaData);
logger.info(`File ${sourceFile} uploaded as object ${destinationObject} in bucket ${bucket}`);
return true;
}
} catch (error) {
console.error('Error checking file:', error);
}
}

/**
* Checks if a CSV file exists in the specified Minio bucket
* @param {string} fileName - Name of the CSV file to check
* @param {string} bucket - Bucket name
* @returns {Promise<boolean>} - Returns true if file exists, false otherwise
*/
export async function checkCsvFileExists(fileName: string, bucket: string): Promise<boolean> {
const minioClient = new Minio.Client({
endPoint,
port,
useSSL,
accessKey,
secretKey
});

try {
// Check if bucket exists first
const bucketExists = await minioClient.bucketExists(bucket);
if (!bucketExists) {
logger.info(`Bucket ${bucket} does not exist`);
return false;
}

// 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
if (stats.metaData && stats.metaData['content-type']) {
logger.info(`File ${fileName} exists in bucket ${bucket}`);
return true;
} else {
logger.info(`File ${fileName} does not exist in bucket ${bucket}`);
return false;
}
} catch (err: any) {
if (err.code === 'NotFound') {
logger.debug(`File ${fileName} not found in bucket ${bucket}`);
return false;
}
// For any other error, log it and rethrow
logger.error(`Error checking file existence: ${err.message}`);
throw err;
}

}

0 comments on commit 878984d

Please sign in to comment.