-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-upload.js
More file actions
603 lines (537 loc) · 17.2 KB
/
file-upload.js
File metadata and controls
603 lines (537 loc) · 17.2 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
const { createPresignedPost } = require('@aws-sdk/s3-presigned-post');
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
const {
S3Client, ListObjectsCommand, GetObjectCommand, HeadObjectCommand
} = require('@aws-sdk/client-s3');
const path = require('path');
const db = require('database-util');
const ingestBucket = process.env.INGEST_BUCKET;
const region = process.env.REGION;
const cueAPIToken = process.env.CUE_API_TOKEN;
const cueRootUrl = process.env.CUE_ROOT_URL;
const cueCollection = process.env.CUE_COLLECTION;
const useCUEUpload = process.env.USE_CUE_UPLOAD;
const categoryEnums = ['documentation', 'sample'];
async function legacyGenerateUploadUrl(params) {
const { key, checksumValue, fileType } = params;
const checksumAlgo = 'SHA256';
if (!fileType) return ({ error: 'invalid file type' });
const s3Client = new S3Client({
region
});
const payload = {
Bucket: ingestBucket,
Key: key,
Conditions: [
{ 'x-amz-meta-checksumalgorithm': checksumAlgo },
{ 'x-amz-meta-checksumvalue': checksumValue },
{ 'x-amz-checksum-sha256': checksumValue }
],
Fields: {
'x-amz-meta-checksumalgorithm': checksumAlgo,
'x-amz-meta-checksumvalue': checksumValue,
'x-amz-checksum-sha256': checksumValue
},
Expires: 60
};
try {
const resp = await createPresignedPost(s3Client, payload);
return (resp);
} catch (err) {
console.error(err);
return ({ error: 'Error generating upload url' });
}
}
async function cuePostQuery(params) {
const { endpoint, payload } = params;
const response = await fetch(`${cueRootUrl}${endpoint}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${cueAPIToken}`
},
body: JSON.stringify(payload)
});
const responseText = await response.text();
try {
return JSON.parse(responseText);
} catch (err) {
console.error(err);
// eslint-disable-next-line no-console
console.log(responseText);
return ({ error: 'Error parsing CUE API response.' });
}
}
async function generateUploadUrl(params) {
if (useCUEUpload?.toLowerCase?.() === 'false') return legacyGenerateUploadUrl(params);
const {
key, checksumValue, fileType, fileSize
} = params;
if (!fileType) return ({ error: 'invalid file type' });
try {
const response = await cuePostQuery({
endpoint: '/v2/upload/multipart/start',
payload: {
collection_name: cueCollection,
file_name: path.basename(key),
file_size_bytes: fileSize,
checksum: checksumValue,
collection_path: path.dirname(key),
content_type: fileType
}
});
return {
...(response.error ? {} : { collection_path: path.dirname(key) }),
...response
};
} catch (err) {
console.error(err);
return ({ error: 'Error getting upload url' });
}
}
async function getFormUrlKeyMethod(event, user) {
const {
file_name: fileName,
file_category: fileCategory,
submission_id: submissionId
} = event;
const userInfo = await db.user.findById({ id: user });
const groupIds = userInfo.user_groups.map((group) => group.id);
if (!fileCategory || !categoryEnums.includes(fileCategory)) {
return ({ error: 'Invalid file category' });
}
if (submissionId) {
const {
daac_id: daacId,
contributor_ids: contributorIds
} = await db.submission.findById({ id: submissionId, user_id: userInfo.id });
if (!contributorIds) return ({ error: 'Submission not found' });
const userDaacs = groupIds.length > 0 ? await db.daac.getIds({ group_ids: groupIds }) : [];
const userDaacIds = userDaacs.map((daac) => daac.id);
if (contributorIds.includes(user)
|| userInfo.user_privileges.includes('ADMIN')
|| userDaacIds.includes(daacId)
) {
return ({ key: `${submissionId}/${fileCategory}/${user}/${fileName}` });
}
}
// return `${fileCategory}/${user}/${fileName}`
return ({ error: 'Not Implemented' });
}
async function getGroupUploadKeyMethod(event, user) {
const {
file_name: fileName,
prefix
} = event;
const { group_id: groupId } = event;
const userInfo = await db.user.findById({ id: user });
const groupIds = userInfo.user_groups.map((group) => group.id);
const rootGroupId = '4daa6b22-f015-4ce2-8dac-8b3510004fca';
const groupShortName = (await db.group.findById({ id: groupId })).short_name;
if (!groupShortName) {
return ({ error: 'Invalid Group' });
}
if (!(userInfo.user_privileges.includes('ADMIN') || groupIds.includes(rootGroupId))
&& !(groupIds.includes(groupId) && userInfo.user_privileges.includes('GROUP_UPLOAD'))) {
return ({ error: 'Not Authorized' });
}
const key = prefix ? `group/${groupShortName}/${prefix.replace(/^\/?/, '').replace(/\/?$/, '')}/${fileName}` : `group/${groupShortName}/${fileName}`;
return ({ key });
}
async function getAttachmentUploadKeyMethod(event, user) {
const {
file_name: fileName,
conversation_id: conversationId
} = event;
const userInfo = await db.user.findById({ id: user });
if (!userInfo.user_privileges.includes('ADMIN') && !userInfo.user_privileges.includes('NOTE_REPLY')) {
return ({ error: 'Not Authorized' });
}
// Verify conversation is real & the user has permissions!
let response = {};
if (userInfo.user_privileges.includes('ADMIN')
|| userInfo.user_groups.some((group) => group.short_name === 'root_group')) {
response = await db.note.readConversation({
admin: true,
conversation_id: conversationId
});
} else if (userInfo.user_privileges.includes('REQUEST_DAACREAD')) {
response = await db.note.readConversation({
user_id: user,
daac: true,
conversation_id: conversationId
});
} else {
response = await db.note.readConversation({
user_id: user,
conversation_id: conversationId
});
}
if (response.error) {
return ({ error: 'Conversation not found' });
}
const key = `drafts/${conversationId}/${user}/${fileName}`;
return ({ key });
}
async function getUploadStepKeyMethod(event, user) {
const {
file_name: fileName,
file_category: fileCategory,
destination: uploadDestination,
submission_id: submissionId
} = event;
if (!fileName || !fileCategory || !uploadDestination || !submissionId) {
return ({ error: 'Invlaid parameters' });
}
// Verify submission is real!
const submissionResp = await db.submission.findById({ id: submissionId, user_id: user });
if (submissionResp.error) {
return ({ error: 'Submission not found' });
}
const key = `${uploadDestination.replace(/^\/?/, '').replace(/\/?$/, '')}/${submissionId}/${fileCategory}/${user}/${fileName}`;
return ({ key });
}
const keyMethods = {
form: getFormUrlKeyMethod,
group: getGroupUploadKeyMethod,
attachment: getAttachmentUploadKeyMethod,
step: getUploadStepKeyMethod
};
async function getUploadUrlMethod(event, user) {
const {
upload_type: uploadType,
file_type: fileType,
checksum_value: checksumValue,
file_size_bytes: fileSize
} = event;
if (!(uploadType && Object.keys(keyMethods).includes(uploadType))) {
return ({ error: 'Invalid upload type' });
}
// Get the key for the type of url needed. The key method will perform the needed validations
const uploadKeyGenerator = keyMethods[uploadType];
const keyResult = await uploadKeyGenerator(event, user);
if (keyResult && keyResult.error) {
return ({ error: keyResult.error });
}
return generateUploadUrl({
key: keyResult.key,
checksumValue,
fileType,
fileSize
});
}
async function getChecksumTag(key, s3Client) {
const payload = {
Bucket: ingestBucket,
Key: key,
ChecksumMode: 'ENABLED'
};
try {
const headCmd = new HeadObjectCommand(payload);
const headResp = await s3Client.send(headCmd);
const fileId = headResp?.Metadata?.fileid || null;
return {
headResp,
fileId
};
} catch (err) {
console.error('Error retrieving S3 response:', err);
return {
error: true,
message: 'Error getting S3 respons'
};
}
}
function getFileCategory(s3Key) {
const categoryRegex = /\/(?<category>documentation|sample)\//;
const matches = s3Key.match(categoryRegex);
if (matches) {
return matches.groups.category;
}
return 'unknown';
}
async function processFile(item, s3Client) {
const response = item.ChecksumAlgorithm ? (await getChecksumTag(item.Key, s3Client)) : null;
const { fileId, ChecksumSHA256 } = response;
const fileMetaData = {
key: item.Key,
size: item.Size,
lastModified: item.LastModified,
file_name: item.Key.split('/').pop(),
category: getFileCategory(item.Key),
...(ChecksumSHA256 && { ChecksumSHA256 }),
...(fileId && { fileId })
};
return fileMetaData;
}
async function updateUploadFiles(fileId) {
let respData = [];
try {
respData = await cuePostQuery({
endpoint: '/v2/files/list',
payload: {
file_ids: [fileId],
apiKey: cueAPIToken
}
});
const data = Array.isArray(respData?.items) ? respData.items[0] : respData;
const {
name: fileName, size_bytes: fileSize, collection_path: collectionPth, status
} = data;
const category = getFileCategory(collectionPth);
return {
file_id: fileId,
file_name: fileName,
size: fileSize,
category,
status
};
} catch (err) {
console.error('Error processing file_id:', fileId);
}
return {};
}
async function listFilesMethod(event, user) {
let rawResponse;
const { submission_id: submissionId } = event;
const userInfo = await db.user.findById({ id: user });
const groupIds = userInfo.user_groups.map((group) => group.id);
const userDaacs = groupIds.length > 0 ? await db.daac.getIds({ group_ids: groupIds }) : [];
const userDaacIds = userDaacs.map((daac) => daac.id);
const uploadResponse = await db.submission.getTempUploadFiles({ submissionId });
const uploadResp = uploadResponse.map(({ lastmodified, ...rest }) => ({
...rest,
lastModified: lastmodified || rest.lastModified
}));
const {
daac_id: daacId,
contributor_ids: contributorIds
} = await db.submission.findById({ id: submissionId, user_id: userInfo.id });
if (contributorIds.includes(user)
|| userInfo.user_privileges.includes('ADMIN')
|| userDaacIds.includes(daacId)
) {
const s3Client = new S3Client({ region });
const command = new ListObjectsCommand({ Bucket: ingestBucket, Prefix: `${submissionId}` });
try {
rawResponse = await s3Client.send(command);
} catch (err) {
console.error(err);
}
const response = [];
if (rawResponse.Contents) {
for (let i = 0; i < rawResponse.Contents.length; i += 1) {
response.push(await processFile(rawResponse.Contents[i], s3Client));
}
}
const fileIds = response.map((item) => item.fileId);
const filteredUploadResp = uploadResp.filter(
(dbFile) => !fileIds.includes(dbFile.file_id)
);
const commonFileIds = uploadResp
.map((dbFile) => dbFile.file_id)
.filter((id) => fileIds.includes(id));
await db.submission.deleteTempUploadFilesByIds({ fileIds: commonFileIds });
const finalFiles = [...response, ...filteredUploadResp];
return finalFiles;
}
return ({ error: 'Not Authorized' });
}
async function createTempUploadFileMethod(event) {
const ret = await updateUploadFiles(event.fileId);
const result = await db.submission.createTempUploadFile({
file_id: ret.file_id,
submission_id: event.submissionId,
file_name: ret.file_name,
category: ret.category,
size: ret.size,
status: ret.status
});
return result;
}
async function listStepFilesMethod(event, user) {
let rawResponse;
const { submission_id: submissionId } = event;
const userInfo = await db.user.findById({ id: user });
const {
contributor_ids: contributorIds,
step_data: stepData
} = await db.submission.findById({ id: submissionId, user_id: userInfo.id });
const {
upload_destination: prefix
} = await db.upload.findUploadStepById({ id: stepData.upload_step_id });
if (contributorIds.includes(user)
|| userInfo.user_privileges.includes('ADMIN')
) {
if (prefix) {
const s3Client = new S3Client({ region });
const command = new ListObjectsCommand({ Bucket: ingestBucket, Prefix: `${prefix}/${submissionId}` });
try {
rawResponse = await s3Client.send(command);
} catch (err) {
console.error(err);
return ({ error: 'Error listing files' });
}
if (rawResponse.Contents) {
const response = [];
for (let i = 0; i < rawResponse.Contents.length; i += 1) {
response.push(await processFile(rawResponse.Contents[i], s3Client));
}
return response;
}
return ([]);
}
return ([]);
}
return ({ error: 'Not Authorized' });
}
async function getAttachmentDownloadUrlMethod(event, user, s3Client) {
// TODO - Write a proper query to check if user has permissions to view note instead of relying
// on larger find query
const noteId = event.key.split('/')[1];
const { id } = await db.note.findById({ id: noteId, user_id: user });
if (id) {
const payload = {
Bucket: ingestBucket,
Key: event.key
};
try {
const command = new GetObjectCommand(payload);
return getSignedUrl(s3Client, command, { expiresIn: 60 });
} catch (err) {
console.error(err);
return ({ error: 'Failed to download' });
}
}
return ({ error: 'Not Authorized' });
}
async function getDownloadUrlMethod(event, user) {
const { key } = event;
const s3Client = new S3Client({
region
});
if (key.split('/')[0] === 'attachments') return getAttachmentDownloadUrlMethod(event, user, s3Client);
const submissionId = key.split('/')[0];
const userInfo = await db.user.findById({ id: user });
const groupIds = userInfo.user_groups.map((group) => group.id);
const userDaacs = groupIds.length > 0 ? await db.daac.getIds({ group_ids: groupIds }) : [];
const userDaacIds = userDaacs.map((daac) => daac.id);
const {
daac_id: daacId
} = await db.submission.findById({ id: submissionId, user_id: userInfo.id });
if (userInfo.user_privileges.includes('ADMIN')
|| userDaacIds.includes(daacId)
) {
const payload = {
Bucket: ingestBucket,
Key: key
};
try {
const command = new GetObjectCommand(payload);
return getSignedUrl(s3Client, command, { expiresIn: 60 });
} catch (err) {
console.error(err);
return ({ error: 'Failed to upload' });
}
}
return ({ error: 'Not Authorized' });
}
async function getUploadStepMethod(event) {
const { upload_step_id: uploadStepId } = event;
return db.upload.findUploadStepById({ id: uploadStepId });
}
async function completeUploadMethod(event) {
const {
final_file_size: fileSize,
upload_id: uploadId,
parts: rawParts
} = event;
// -------------------------------
// SAFELY NORMALIZE incoming parts
// -------------------------------
let parts = rawParts;
if (typeof parts === 'string') {
// try to convert it manually
try {
// Convert = to : so JSON.parse can work
const fixed = parts
.replace(/=/g, ':')
.replace(/([A-Za-z0-9_]+):/g, '"$1":')
.replace(/:(\w+)/g, ':"$1"');
parts = JSON.parse(fixed);
} catch (err) {
console.error('Could not auto-fix parts:', parts);
return { error: "Invalid 'parts' format" };
}
}
// Normalize each part
const normalizedParts = parts.map((p) => {
let etag = p.ETag || '';
// Strip surrounding quotes
etag = etag.replace(/^"+|"+$/g, '');
etag = `"${etag}"`;
return {
PartNumber: Number(p.PartNumber),
ETag: etag
};
});
const commonPayload = {
file_id: event.file_id,
collection_name: cueCollection,
file_name: event.file_name,
collection_path: event.collection_path,
content_type: event.content_type,
checksum: event.checksum
};
if (Number(fileSize) < 0) return { error: 'Invalid file_size_types size.' };
const response = await cuePostQuery({
endpoint: '/v2/upload/multipart/complete',
payload: {
...commonPayload,
...{
upload_id: uploadId,
parts: normalizedParts,
final_file_size: fileSize
}
}
});
return response;
}
async function getPartUrlMethod(event) {
delete event.operation;
delete event.context;
let response;
try {
response = await cuePostQuery({
endpoint: '/v2/upload/multipart/get-part-url',
payload: {
...event,
...{
file_id: event.file_id,
upload_id: event.upload_id,
part_number: event.part_number
}
}
});
} catch (err) {
console.error({ error: 'Error getting part url.' });
}
return response;
}
const operations = {
getUrl: getUploadUrlMethod,
listFiles: listFilesMethod,
createTempUploadFile: createTempUploadFileMethod,
listStepFiles: listStepFilesMethod,
getDownloadUrl: getDownloadUrlMethod,
getUploadStep: getUploadStepMethod,
completeUpload: completeUploadMethod,
getPartUrl: getPartUrlMethod
};
async function handler(event) {
console.info(`[EVENT]\n${JSON.stringify(event)}`);
const user = event.context.user_id;
const operation = operations[event.operation];
const data = await operation(event, user);
return data;
}
exports.handler = handler;