UNPKG

pink-bears

Version:

Intelligent rate limiting middleware with MongoDB integration and caching for Node.js applications

81 lines (75 loc) 2.76 kB
const AWS = require('./index'); const csvtojson = require('csvtojson'); const { emitEvent } = require('../../errorEmitter'); const { errorConstants } = require('../../constants'); const S3 = new AWS.S3(); class File { //read file from S3 async read(key, status, traceId, batch) { const Status = require("./status"); try { const params = { Bucket: process.env.bucketName, Key: key }; const data = await S3.getObject(params).promise(); const csvString = data.Body.toString('utf-8'); const json = await csvtojson({flatKeys:true}).fromString(csvString); if (batch) { if (json.length > 200000) { return { "Error": "Data exceeding the max_limit 200000" } } } else { if (json.length > 10000) { return { "Error": "Data exceeding the max_limit 10000" } } } if (status) { const $Status = new Status(); await $Status.setTotalCount(json.length, traceId); } return json; } catch (error) { console.log("Error while reading the value ", error); await emitEvent(errorConstants.file, error); throw error; } } async uploadToS3(key, content, fileType) { try { const isCSV = (fileType === 'csv'); if (isCSV) { content = "File, Row, Contact, Remarks\n" + content; } const params = { Bucket: process.env.errorLogs, Key: key + '/errors', Body: content, ContentType: isCSV ? 'text/csv' : 'text/plain', ContentDisposition: 'attachment;', } const result = await S3.upload(params).promise(); return true; } catch (error) { console.log("Error while writing data into S3", error); await emitEvent(errorConstants.file, error); throw error; } } async findObject(key) { try { const headResult = await S3.headObject({ Bucket: process.env.errorLogs, Key: key }).promise(); return true; // key exists } catch (error) { if (error.code === 'NotFound') { return false; // key does not exist } else { console.log("Error occurred while checking if the object already exists in S3", error); await emitEvent(errorConstants.file, error); throw error; // other error occurred } } } }; module.exports = File;