Skip to content

Commit dc17708

Browse files
author
Rohit Kumar
committed
#
1 parent 7f1e355 commit dc17708

File tree

8 files changed

+8818
-5591
lines changed

8 files changed

+8818
-5591
lines changed

app/public/assets/CMV_ID.pdf

177 KB
Binary file not shown.

app/public/assets/fonts/code_bold.otf

90 KB
Binary file not shown.
64.9 KB
Binary file not shown.

server/API/card/GenerateCard.js

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import AWS from "aws-sdk";
77
import { sendWhatsAppDoc } from "../sms/WhatsApp.js";
88
import { sendVoterID } from "../mail/Mail.js";
99
import sendSMS from "../sms/SMS.js";
10+
import Jimp from "jimp";
1011

1112
const generatePDF = async (data) => {
1213
// Fetch
1314
const pdfRaw = await fetch(
14-
"https://s3.us-east-1.amazonaws.com/rohit-kumar/castmyvote/assets/CMV_ID.pdf"
15+
"https://firebasestorage.googleapis.com/v0/b/aboutrohitin.appspot.com/o/castmyvote%2Fassets%2FCMV_ID.pdf?alt=media&token=5fc23488-72cb-48e9-86f2-8374b9feca4b"
1516
).then((res) => res.arrayBuffer());
1617

1718
const pdfDoc = await PDFDocument.load(pdfRaw);
@@ -25,22 +26,24 @@ const generatePDF = async (data) => {
2526
// Load Font
2627

2728
const codeBold = await fetch(
28-
"https://s3.us-east-1.amazonaws.com/rohit-kumar/castmyvote/assets/fonts/code_bold.otf"
29+
"https://firebasestorage.googleapis.com/v0/b/aboutrohitin.appspot.com/o/castmyvote%2Fassets%2Ffonts%2Fcode_bold.otf?alt=media&token=03fc3cc3-a075-4d2c-b063-6ea09685aa9f"
2930
).then((res) => res.arrayBuffer());
3031
const codeBoldFont = await pdfDoc.embedFont(codeBold);
3132
const codeLight = await fetch(
32-
"https://s3.us-east-1.amazonaws.com/rohit-kumar/castmyvote/assets/fonts/code_light.otf"
33+
"https://firebasestorage.googleapis.com/v0/b/aboutrohitin.appspot.com/o/castmyvote%2Fassets%2Ffonts%2Fcode_light.otf?alt=media&token=94daad96-04bf-4ce6-85cf-4edf61fecd24"
3334
).then((res) => res.arrayBuffer());
3435
const codeLightFont = await pdfDoc.embedFont(codeLight);
3536

3637
const pages = pdfDoc.getPages();
3738
const firstPage = pages[0];
3839

3940
//Fetch Image from URL
40-
const photoImageBytes = await fetch(data.photo).then((res) =>
41-
res.arrayBuffer()
42-
);
43-
const photoImage = await pdfDoc.embedPng(photoImageBytes);
41+
42+
43+
// const photoImageBytes = await fetch(data.photo).then((res) =>
44+
// res.arrayBuffer()
45+
// );
46+
// const photoImage = await pdfDoc.embedPng(photoImageBytes);
4447

4548
firstPage.drawText(data.id_no, {
4649
x: 180,
@@ -49,13 +52,13 @@ const generatePDF = async (data) => {
4952
font: codeBoldFont,
5053
color: rgb(0, 0, 0),
5154
});
52-
firstPage.drawImage(photoImage, {
53-
// QR Code
54-
x: 48,
55-
y: 595,
56-
width: 50,
57-
height: 70,
58-
});
55+
// firstPage.drawImage(photoImage, {
56+
// // QR Code
57+
// x: 48,
58+
// y: 595,
59+
// width: 50,
60+
// height: 70,
61+
// });
5962

6063
const pngImageBytes = await fetch(
6164
`https://www.cognex.com/api/Sitecore/Barcode/Get?data=${data.id_no}&code=BCL_CODE128&width=1000&imageType=PNG&foreColor=%23000000&backColor=%23FFFFFF&rotation=RotateNoneFlipNone`
@@ -128,43 +131,49 @@ const generatePDF = async (data) => {
128131
return ((Math.random() * 16) | 0).toString(16);
129132
})
130133
.toLowerCase() +
131-
".pdf";
132-
// Upload file to S3
133-
const s3 = new AWS.S3({
134-
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
135-
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
136-
region: process.env.AWS_REGION,
137-
});
138-
139-
s3.upload({
140-
Bucket: process.env.AWS_BUCKET_NAME,
141-
Key: "castmyvote/cmv_id/" + data.id_no + "-" + filename,
142-
Body: pdfBytes,
143-
ContentType: "application/pdf",
144-
})
145-
.on("httpUploadProgress", function (evt) {
146-
// console.log(parseInt((evt.loaded / evt.total) * 100));
147-
})
148-
.send(function (err, aws) {
149-
if (err) {
150-
console.log(err);
151-
} else {
152-
console.log(aws.Location);
134+
"." +
135+
file.originalname.split(".")[1];
136+
137+
const storageRef = ref(storage, `"castmyvote/cmv_id/${filename}`);
138+
// Set the upload type.
139+
const metadata = {
140+
contentType: file.mimetype,
141+
cacheControl: "public",
142+
};
143+
// Upload the file and metadata
144+
const uploadTask = uploadBytesResumable(storageRef, file.buffer, metadata);
145+
146+
uploadTask.on(
147+
"state_changed",
148+
(snapshot) => {
149+
const progress = Math.round(
150+
(snapshot.bytesTransferred / snapshot.totalBytes) * 100
151+
);
152+
console.log("Upload is " + progress + "% done");
153+
},
154+
(error) => {
155+
console.log(error);
156+
},
157+
() => {
158+
getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
159+
console.log("File available at", downloadURL);
153160
Card.findOne({ id_no: data.id_no })
154161
.then((card) => {
155-
card.id_doc = aws.Location;
162+
card.id_doc = downloadURL;
156163
card.save();
157164
sendVoterID(card);
158165
const msg = `\nGreetings from CMV!\n\n Your Application No. ${card.application_no} has been approved\n\nYou Voter ID No. : ${card.id_no}\n\nYou will receive your Voter ID through mail or you can download it from User Dashboard \n\nThank You\nTeam CastMyVote!`;
159166
sendSMS(`+91${card.phone}`, msg);
160-
sendWhatsAppDoc(aws.Location);
167+
sendWhatsAppDoc(downloadURL);
161168
console.log("Card Generated ID: " + card.id_no);
162169
})
163170
.catch((err) => {
164171
console.log(err);
165172
});
166-
}
167-
});
173+
res.send({ Location: downloadURL });
174+
});
175+
}
176+
);
168177
}
169178
};
170179

server/firebase.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { initializeApp } from "firebase/app";
2+
import { getStorage } from "firebase/storage";
3+
4+
const firebaseConfig = {
5+
apiKey: "AIzaSyAw4lebDZkNSMKK1nB0cUrk3-M6d1s9foY",
6+
authDomain: "aboutrohitin.firebaseapp.com",
7+
databaseURL: "https://aboutrohitin-default-rtdb.firebaseio.com",
8+
projectId: "aboutrohitin",
9+
storageBucket: "aboutrohitin.appspot.com",
10+
messagingSenderId: "546826450895",
11+
appId: "1:546826450895:web:82cc8286ca7848f6e304ba"
12+
};
13+
14+
export const app = initializeApp(firebaseConfig);
15+
export const storage = getStorage(app);

0 commit comments

Comments
 (0)