-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
198 lines (171 loc) · 6.03 KB
/
Copy pathindex.mjs
File metadata and controls
198 lines (171 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// index.mjs .Node.js 20.x/22.x (Handler = index.handler)
import { S3Client, ListObjectsV2Command, GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
const REGION = process.env.AWS_REGION || 'us-west-2';
const BUCKET = process.env.COEQWAL_S3_BUCKET || 'coeqwal-model-run';
const s3 = new S3Client({ region: REGION });
// ---- Helpers ----------------------------------------------------
const corsHeaders = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Requested-With, Accept, Origin',
'Access-Control-Max-Age': '86400'
};
const json = (status, body) => ({
statusCode: status,
headers: corsHeaders,
body: JSON.stringify(body),
});
const redirect = (url) => ({
statusCode: 302,
headers: {
Location: url,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Requested-With, Accept, Origin',
'Access-Control-Expose-Headers': 'Location'
},
body: '',
});
const validScenario = (s) => /^[a-zA-Z]\d{4}$/.test(s || '');
// List scenarios by walking S3 prefixes: scenario/<id>/
async function listScenarioIds() {
const ids = [];
let token;
do {
const res = await s3.send(
new ListObjectsV2Command({
Bucket: BUCKET,
Prefix: 'scenario/',
Delimiter: '/',
ContinuationToken: token,
})
);
for (const cp of res.CommonPrefixes ?? []) {
// cp.Prefix like "scenario/s0020/"
const m = cp.Prefix.match(/^scenario\/([A-Za-z]\d{4})\/$/);
if (m) ids.push(m[1]);
}
token = res.IsTruncated ? res.NextContinuationToken : undefined;
} while (token);
return ids.sort();
}
// Find the ZIP key under scenario/<id>/run/*.zip (pick most recent if multiple)
async function findZipKey(id) {
const res = await s3.send(
new ListObjectsV2Command({
Bucket: BUCKET,
Prefix: `scenario/${id}/run/`,
MaxKeys: 100,
})
);
const zips = (res.Contents || []).filter((o) => o.Key?.toLowerCase().endsWith('.zip'));
if (!zips.length) return null;
zips.sort((a, b) => new Date(b.LastModified) - new Date(a.LastModified));
return zips[0].Key;
}
// Exact-key existence probe (cheap)
async function keyExists(key) {
const res = await s3.send(
new ListObjectsV2Command({
Bucket: BUCKET,
Prefix: key,
MaxKeys: 1,
})
);
return (res.Contents || []).some((o) => o.Key === key);
}
// Expected CSV keys
const csvKeysFor = (id) => ({
output: `scenario/${id}/csv/${id}_coeqwal_calsim_output.csv`,
sv: `scenario/${id}/csv/${id}_coeqwal_sv_input.csv`,
});
// Presign a GET for download with Content-Disposition to force download
async function presign(key) {
const filename = key.split('/').pop();
const cmd = new GetObjectCommand({
Bucket: BUCKET,
Key: key,
ResponseContentDisposition: `attachment; filename="${filename}"`
});
return getSignedUrl(s3, cmd, { expiresIn: 15 * 60 });
}
// ---- Router -----------------------------------------------------
export async function handler(event) {
try {
// Add debug logging to see what API Gateway is actually sending
console.log('Full event:', JSON.stringify(event, null, 2));
// More robust path detection for different API Gateway formats
const path = event.requestContext?.http?.path ||
event.requestContext?.path ||
event.path ||
event.rawPath || '';
const method = (event.requestContext?.http?.method ||
event.requestContext?.httpMethod ||
event.httpMethod || 'GET').toUpperCase();
console.log('Parsed path:', path, 'method:', method);
// Handle preflight OPTIONS requests
if (method === 'OPTIONS') {
return {
statusCode: 200,
headers: corsHeaders,
body: ''
};
}
// GET /scenario
if (method === 'GET' && path.endsWith('/scenario')) {
const ids = await listScenarioIds();
const scenarios = [];
for (const id of ids) {
const zipKey = await findZipKey(id);
const { output: outKey, sv: svKey } = csvKeysFor(id);
const [hasOut, hasSv] = await Promise.all([keyExists(outKey), keyExists(svKey)]);
scenarios.push({
scenario_id: id,
files: {
zip: zipKey
? { key: zipKey, filename: zipKey.split('/').pop() }
: null,
output_csv: hasOut
? { key: outKey, filename: outKey.split('/').pop() }
: null,
sv_csv: hasSv
? { key: svKey, filename: svKey.split('/').pop() }
: null,
},
});
}
return json(200, { scenarios });
}
// GET /download?scenario=s0020&type=zip|output|sv
if (method === 'GET' && path.endsWith('/download')) {
const qs = event.queryStringParameters || {};
const scenario = String(qs.scenario || '');
const type = String(qs.type || '').toLowerCase();
if (!validScenario(scenario)) {
return json(400, { error: 'Invalid scenario (expected like s0020)' });
}
if (!['zip', 'output', 'sv'].includes(type)) {
return json(400, { error: 'Invalid type (must be zip|output|sv)' });
}
let key;
if (type === 'zip') {
key = await findZipKey(scenario);
if (!key) return json(404, { error: 'ZIP not found' });
} else {
const { output: outKey, sv: svKey } = csvKeysFor(scenario);
key = type === 'output' ? outKey : svKey;
const exists = await keyExists(key);
if (!exists) return json(404, { error: 'CSV not found' });
}
const url = await presign(key);
return redirect(url);
}
// Fallback
return json(404, { error: 'Not Found' });
} catch (err) {
console.error(err);
return json(500, { error: 'Internal error' });
}
}