-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.js
63 lines (61 loc) · 2.38 KB
/
aws.js
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
const admin = require('firebase-admin')
const serviceAccount = require('service-account-file.json')
const aws = require('aws-sdk')
const s3 = new aws.S3({ apiVersion: '2006-03-01' })
const mkdirp = require('mkdirp')
const os = require('os')
const path = require('path')
const fs = require('fs').promises
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://shmooze-5069b-default-rtdb.firebaseio.com/",
storageBucket: "shmooze-5069b.appspot.com/"
})
exports.lambdaHandler = async (event, context) => {
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '))
if (!key.endsWith('.ts')) {
return null
}
const awsBucket = event.Records[0].s3.bucket.name
const params = {
Bucket: awsBucket,
Key: key,
}
const { Body, ContentType } = await s3.getObject(params).promise().catch((error) => console.log(error))
if (Body === undefined) {
return null
}
const fileName = (key.split('/').pop())
const pieces = fileName.split('_')
const isSoloRecording = pieces.length > 5
if (isSoloRecording) {
const shmoozeId = pieces[1]
const speakerId = pieces[5]
const inviteSnapshot = await admin.firestore().collection('mailRoom').doc(shmoozeId).get().catch((error) => console.log(error))
if (inviteSnapshot === undefined || !inviteSnapshot.exists) {
return null
}
const tempLocalFile = path.join(os.tmpdir(), fileName)
const tempLocalDir = path.dirname(tempLocalFile)
await mkdirp(tempLocalDir).catch((error) => console.log(error))
await fs.writeFile(tempLocalFile, Body).catch((error) => console.log(error))
const gcpBucket = admin.storage().bucket()
await gcpBucket.upload(tempLocalFile, { destination: `shmoozes/${shmoozeId}/users/${speakerId}/${fileName}` }).catch((error) => console.log(error))
await fs.unlink(tempLocalFile).catch((error) => console.log(error))
}
else {
const contentType = 'video/MP2T'
if (contentType === ContentType) {
return null
}
const params = {
Bucket: awsBucket,
Key: key,
Body: Body,
ContentType: contentType,
ACL: 'public-read'
}
await s3.putObject(params).promise().catch((error) => console.log(error))
}
return null
}