-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (65 loc) · 2.4 KB
/
index.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
64
65
66
67
68
69
70
71
import axios from 'axios'
import LambdaEnvVars from 'lambda-env-vars'
import {checkForExisting, epubStore, epubExplode, getBuffer} from './src/epubParsers'
import {resultHandler} from './src/responseHandlers'
const fileNameRegex = /[0-9]+[.]{1}epub[.]{1}(?:no|)images/
const lambdaEnvVarsClient = new LambdaEnvVars()
var record, fileName, dateUpdated, putParams, handleResp, records, headParams, kinesis
exports.handler = (event, context, callback) => {
records = event['Records']
for(var i = 0; i < records.length; i++){
record = records[i]
let payload = JSON.parse(new Buffer.from(record.kinesis.data, 'base64').toString('ascii'))
let url = payload['url']
let fileNameMatch = fileNameRegex.exec(url)
if (!fileNameMatch){
handleResp = {
"status": 500,
"code": "Regex Failure",
"message": "Failed to extract file from url " + url
}
resultHandler(handleResp)
continue
}
let fileName = fileNameMatch[0]
let itemID = payload['id']
let updated = new Date(payload['updated'])
checkForExisting(fileName, updated).then((status) => {
axios({
method: 'get',
url: url,
responseType: 'stream'
})
.then((response) => {
epubExplode(fileName, itemID, updated, response)
getBuffer(response.data).then((buffer) => {
epubStore(fileName, itemID, updated, 'archive', buffer)
})
.catch((error) => {
handleResp = {
"status": 500,
"code": "Stream-to-Buffer Error",
"message": error
}
resultHandler(handleResp)
})
})
.catch((error) => {
handleResp = {
"status": error.response.status,
"code": "Axios Failure",
"message":error.response.data
}
resultHandler(handleResp)
})
})
.catch((err) => {
handleResp = {
"status": 200,
"code": "existing",
"message": "Found existing, up-to-date ePub"
}
resultHandler(handleResp)
})
}
}