1- const express = require ( 'express' )
2- const serverless = require ( 'serverless-http' )
3- const AWS = require ( 'aws-sdk' )
4- const xlsx = require ( 'xlsx' )
5- require ( 'dotenv' ) . config ( )
6- const AUTH_TOKEN = process . env . AUTH_TOKEN
1+ const AWS = require ( 'aws-sdk' ) ;
2+ const xlsx = require ( 'xlsx' ) ;
3+ require ( 'dotenv' ) . config ( ) ;
4+ const AUTH_TOKEN = process . env . AUTH_TOKEN ;
75
8- const app = express ( )
9- app . use ( express . json ( ) )
10-
11- const router = express . Router ( )
12- router . use ( ( req , res , next ) => {
13- const token = req . get ( 'x-auth-token' )
14- if ( ! ! token && token === AUTH_TOKEN ) {
15- next ( )
16- } else {
17- res . status ( 401 ) . json ( { message : 'Invalid auth token' } )
18- }
19- } )
20-
21- router . post ( '/jsonToExcel' , async ( req , res ) => {
22- const { s3FilePublic, s3Region, s3Bucket, s3KeyId, s3SecretKey, s3Path } = req . body . config
23- const jsonData = req . body . excel
24- const excelData = convertJsonToExcel ( jsonData )
25- AWS . config . update ( { accessKeyId : s3KeyId , secretAccessKey : s3SecretKey , region : s3Region , signatureVersion : 'v4' } )
26- const s3 = new AWS . S3 ( )
27- const dataset = {
28- Bucket : s3Bucket ,
29- Key : s3Path ,
30- Body : excelData ,
31- ContentType : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ,
32- ACL : ! ! s3FilePublic ? 'public-read' : 'private' ,
33- }
34- const response = await s3 . upload ( dataset ) . promise ( )
35- return res . json ( response . Location )
36- } )
37-
38- function convertJsonToExcel ( jsonData ) {
6+ const convertJsonToExcel = ( jsonData ) => {
397 const workbook = xlsx . utils . book_new ( ) ;
408
419 Object . keys ( jsonData ) . forEach ( ( sheetName ) => {
@@ -44,22 +12,44 @@ function convertJsonToExcel(jsonData) {
4412 } ) ;
4513
4614 return xlsx . write ( workbook , { type : 'buffer' , bookType : 'xlsx' } ) ;
47- }
15+ } ;
4816
49- app . use ( '/api' , router )
17+ const handler = async ( event ) => {
18+ const { config, excel } = JSON . parse ( event . body ) ;
19+ const { s3FilePublic, s3Region, s3Bucket, s3KeyId, s3SecretKey, s3Path } = config ;
20+
21+ if ( ! AUTH_TOKEN || event . headers [ 'x-auth-token' ] !== AUTH_TOKEN ) {
22+ return {
23+ statusCode : 401 ,
24+ body : JSON . stringify ( { message : 'Invalid auth token' } ) ,
25+ } ;
26+ }
5027
51- const startServer = async ( ) => {
52- app . listen ( 3000 , ( ) => {
53- console . log ( 'listening on port 3000!' )
54- } )
55- }
56- startServer ( )
28+ const excelData = await convertJsonToExcel ( excel ) ;
5729
58- //lambda handling
59- const handler = serverless ( app )
30+ AWS . config . update ( { accessKeyId : s3KeyId , secretAccessKey : s3SecretKey , region : s3Region , signatureVersion : 'v4' } ) ;
31+ const s3 = new AWS . S3 ( ) ;
6032
61- exports . handler = async ( event , context , callback ) => {
62- const response = handler ( event , context , callback )
63- return response
64- }
33+ const dataset = {
34+ Bucket : s3Bucket ,
35+ Key : s3Path ,
36+ Body : excelData ,
37+ ContentType : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ,
38+ ACL : s3FilePublic ? 'public-read' : 'private' ,
39+ } ;
40+
41+ try {
42+ const response = await s3 . upload ( dataset ) . promise ( ) ;
43+ return {
44+ statusCode : 200 ,
45+ body : JSON . stringify ( response . Location ) ,
46+ } ;
47+ } catch ( error ) {
48+ return {
49+ statusCode : 500 ,
50+ body : JSON . stringify ( { message : 'Error uploading file to S3' , error } ) ,
51+ } ;
52+ }
53+ } ;
6554
55+ exports . handler = handler ;
0 commit comments