-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (73 loc) · 2.79 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
72
73
74
75
76
77
const serverless = require('serverless-http');
const express = require('express')
const app = express();
const AWS = require('aws-sdk');
const {parse, stringify} = require('svgson');
require('dotenv').config()
app.post('/certificate', async (req, res) => {
let s3 = new AWS.S3();
const data = JSON.parse(req.body);
const userParams = {
Certificate: {
template: data.template,
certificateName: data.certificateName,
recognition: data.recognition,
signature: data.signature,
studentName: data.studentName,
logo: data.logo,
mascotLogo: data.mascotLogo,
dateCreated: new Date().getTime()
}
};
let params = {Bucket: process.env.AWS_S3_BUCKET, Key: 'svg.svg'};
try {
let data = await s3.getObject(params).promise();
parse(Buffer.from(data.Body).toString()).then(data => {
data["children"][0]["children"][2]["children"][0]["value"] = userParams.Certificate.studentName
if (!userParams.Certificate.logo) {
data["children"][0]["children"][3]["attributes"]["visibility"] = "hidden";
}
const uploadParams = {
Bucket: process.env.AWS_S3_BUCKET,
Key: 'svg12.svg', // File name you want to save as in S3
Body: stringify(data)
};
s3.putObject(uploadParams).promise()
return res.status(200).json({
message: stringify(data)
})
}).catch(err => {
return res.status(400).json({
message: "Read fails"
});
})
} catch (err) {
return res.status(403).json({
message: err
});
}
// let message;
// const uploadFile = () => {
// return new Promise(function (resolve, reject) {
// readSvgFile().then(data => {
// data = data.replace("Name", userParams.Certificate.studentName)
// data = data.replace("Signature", userParams.Certificate.signature)
// const params = {
// Bucket: process.env.AWS_S3_BUCKET,
// Key: 'svg12.svg', // File name you want to save as in S3
// Body: data
// };
// s3.upload(params, function(err, data) {
// if (err) {
// reject(err.message)
// } else {
// message = `File uploaded successfully. ${data.Location}`
// resolve(message);
// }
// });
// })
// })
// };
// uploadFile().then(data => console.log(data)).catch(err => console.log(err))
});
module.exports.handler = serverless(app);