-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
63 lines (47 loc) · 1.93 KB
/
Copy pathhandler.js
File metadata and controls
63 lines (47 loc) · 1.93 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
'use strict';
const crypto = require('crypto');
const { S3, PutObjectCommand } = require("@aws-sdk/client-s3");
module.exports.app = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
let clientImageFilePath;
try {
const region = 'us-east-2';
const S3Client = new S3(region);
const Controller = require('./controller')({S3Client, PutObjectCommand});
const clientImageController = Controller.clientImageController;
const clientMultipartParseController = Controller.clientMultipartParseController;
const clientData = await clientMultipartParseController(event);
clientImageFilePath = clientData.files.filePath;
const clientImageFileStream = fs.createReadStream(clientImageFilePath);
const imageMIMEType = clientData.files.contentType;
const clientEmail = clientData.body.client_email;
const clientEmailSalted = clientEmail + "" + MagicWord;
const clientId = crypto.createHash('sha256').update(clientEmailSalted).digest('base64');
const imageFileName = `${clientId}.png`;
await clientImageController.uploadClientImage(clientImageFileStream, imageFileName, imageMIMEType);
const response = {
MESSAGE: 'DONE',
RESPONSE: 'Client Image Updated Successfully!',
CODE: 'CLIENT_UPDATED_SUCCESSFULLY'
};
return {
statusCode: 200,
body: JSON.stringify(response)
};
} catch(err) {
console.error(`ERR: ${JSON.stringify(err.message)}`);
const response = {
ERR: err.message,
RESPONSE: 'Client Image Upload Failed!',
CODE: 'CLIENT_UPDATION_FAILED'
};
return {
statusCode: 400,
body: JSON.stringify(response)
};
}
finally {
if(clientImageFilePath)
fs.unlinkSync(clientImageFilePath);
}
}