@@ -5,50 +5,17 @@ const fs = require("fs");
5
5
const path = require ( "path" ) ;
6
6
const extract = require ( "extract-zip" ) ;
7
7
const rateLimit = require ( "express-rate-limit" ) ;
8
- const awsServerlessExpressMiddleware = require ( "aws-serverless-express/middleware" ) ;
9
8
10
9
const app = express ( ) ;
11
10
app . set ( "trust proxy" , 1 ) ;
12
11
app . disable ( "x-powered-by" ) ;
13
12
14
- app . use ( awsServerlessExpressMiddleware . eventContext ( ) ) ;
15
-
16
13
const limiter = rateLimit ( {
17
14
windowMs : 15 * 60 * 1000 , // 15 minutes
18
- max : 100 ,
19
- // Add a custom key generator that handles Lambda events
20
- keyGenerator : ( req ) => {
21
- // Check for IP set by Lambda handler
22
- if ( req . ip ) return req . ip ;
23
-
24
- // Fallback to Express standard methods
25
- return req . ip ||
26
- ( req . headers && ( req . headers [ 'x-forwarded-for' ] || req . headers [ 'X-Forwarded-For' ] ) ) ||
27
- req . connection . remoteAddress ||
28
- '127.0.0.1' ;
29
- } ,
30
- // Skip check for health endpoints if you have any
31
- skip : ( req ) => {
32
- return req . path === '/health' || req . path === '/ping' ;
33
- } ,
34
- // Handle errors more gracefully
35
- handler : ( req , res , next , options ) => {
36
- console . log ( "Rate limit exceeded:" , req . ip ) ;
37
- res . status ( options . statusCode ) . send ( "Too many requests, please try again later." ) ;
38
- }
15
+ max : 100
39
16
} ) ;
40
17
app . use ( limiter ) ;
41
18
42
- app . use ( ( req , res , next ) => {
43
- const event = req . apiGateway ?. event ;
44
- if ( event ?. isBase64Encoded && event . body ) {
45
- const buff = Buffer . from ( event . body , "base64" ) ;
46
- req . body = buff ;
47
- req . headers [ "content-length" ] = buff . length ;
48
- }
49
- next ( ) ;
50
- } ) ;
51
-
52
19
app . use ( fileUpload ( ) ) ;
53
20
54
21
const uploadDir = "/tmp/uploads" ;
@@ -65,8 +32,15 @@ async function extractAndParseGDB(zipBuffer, zipFileName = "upload.zip") {
65
32
const zipPath = path . join ( uploadDir , zipFileName ) ;
66
33
const unzipPath = path . join ( uploadDir , path . basename ( zipFileName , ".zip" ) ) ;
67
34
35
+ // Ensure upload directory exists
36
+ if ( ! fs . existsSync ( uploadDir ) ) {
37
+ fs . mkdirSync ( uploadDir , { recursive : true } ) ;
38
+ }
39
+
40
+ // Write the buffer to a file
68
41
fs . writeFileSync ( zipPath , zipBuffer ) ;
69
42
43
+ // Extract the zip file
70
44
await extract ( zipPath , {
71
45
dir : unzipPath ,
72
46
onEntry : ( entry ) => {
@@ -77,10 +51,12 @@ async function extractAndParseGDB(zipBuffer, zipFileName = "upload.zip") {
77
51
}
78
52
} ) ;
79
53
54
+ // Find the .gdb folder
80
55
const files = fs . readdirSync ( unzipPath ) ;
81
56
const gdbFolder = files . find ( f => f . endsWith ( ".gdb" ) ) ;
82
57
if ( ! gdbFolder ) throw new Error ( "No .gdb found." ) ;
83
58
59
+ // Parse the GDB
84
60
const gdbPath = path . join ( unzipPath , gdbFolder ) ;
85
61
const dataset = gdal . open ( gdbPath ) ;
86
62
const results = [ ] ;
@@ -111,7 +87,9 @@ async function extractAndParseGDB(zipBuffer, zipFileName = "upload.zip") {
111
87
}
112
88
} ;
113
89
deleteRecursive ( unzipPath ) ;
114
- } catch ( _ ) { }
90
+ } catch ( err ) {
91
+ console . error ( "Cleanup error:" , err ) ;
92
+ }
115
93
116
94
return results ;
117
95
}
@@ -144,31 +122,5 @@ if (require.main === module) {
144
122
module . exports = {
145
123
app,
146
124
handleUpload,
147
- } ;
148
-
149
- exports . handler = async ( event ) => {
150
- try {
151
- const base64Zip = event . file ;
152
- if ( ! base64Zip ) {
153
- return {
154
- statusCode : 400 ,
155
- body : "Missing 'file' in payload"
156
- } ;
157
- }
158
-
159
- const buffer = Buffer . from ( base64Zip , "base64" ) ;
160
- const results = await extractAndParseGDB ( buffer ) ;
161
-
162
- return {
163
- statusCode : 200 ,
164
- body : JSON . stringify ( results ) ,
165
- headers : { "Content-Type" : "application/json" }
166
- } ;
167
- } catch ( err ) {
168
- console . error ( "Lambda handler error:" , err ) ;
169
- return {
170
- statusCode : 500 ,
171
- body : "Failed to process GDB: " + err . message
172
- } ;
173
- }
174
- } ;
125
+ extractAndParseGDB // Export this function to be used by the Lambda handler
126
+ } ;
0 commit comments