@@ -7,11 +7,12 @@ import AWS from "aws-sdk";
7
7
import { sendWhatsAppDoc } from "../sms/WhatsApp.js" ;
8
8
import { sendVoterID } from "../mail/Mail.js" ;
9
9
import sendSMS from "../sms/SMS.js" ;
10
+ import Jimp from "jimp" ;
10
11
11
12
const generatePDF = async ( data ) => {
12
13
// Fetch
13
14
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 "
15
16
) . then ( ( res ) => res . arrayBuffer ( ) ) ;
16
17
17
18
const pdfDoc = await PDFDocument . load ( pdfRaw ) ;
@@ -25,22 +26,24 @@ const generatePDF = async (data) => {
25
26
// Load Font
26
27
27
28
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 "
29
30
) . then ( ( res ) => res . arrayBuffer ( ) ) ;
30
31
const codeBoldFont = await pdfDoc . embedFont ( codeBold ) ;
31
32
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 "
33
34
) . then ( ( res ) => res . arrayBuffer ( ) ) ;
34
35
const codeLightFont = await pdfDoc . embedFont ( codeLight ) ;
35
36
36
37
const pages = pdfDoc . getPages ( ) ;
37
38
const firstPage = pages [ 0 ] ;
38
39
39
40
//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);
44
47
45
48
firstPage . drawText ( data . id_no , {
46
49
x : 180 ,
@@ -49,13 +52,13 @@ const generatePDF = async (data) => {
49
52
font : codeBoldFont ,
50
53
color : rgb ( 0 , 0 , 0 ) ,
51
54
} ) ;
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
+ // });
59
62
60
63
const pngImageBytes = await fetch (
61
64
`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) => {
128
131
return ( ( Math . random ( ) * 16 ) | 0 ) . toString ( 16 ) ;
129
132
} )
130
133
. 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 ) ;
153
160
Card . findOne ( { id_no : data . id_no } )
154
161
. then ( ( card ) => {
155
- card . id_doc = aws . Location ;
162
+ card . id_doc = downloadURL ;
156
163
card . save ( ) ;
157
164
sendVoterID ( card ) ;
158
165
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!` ;
159
166
sendSMS ( `+91${ card . phone } ` , msg ) ;
160
- sendWhatsAppDoc ( aws . Location ) ;
167
+ sendWhatsAppDoc ( downloadURL ) ;
161
168
console . log ( "Card Generated ID: " + card . id_no ) ;
162
169
} )
163
170
. catch ( ( err ) => {
164
171
console . log ( err ) ;
165
172
} ) ;
166
- }
167
- } ) ;
173
+ res . send ( { Location : downloadURL } ) ;
174
+ } ) ;
175
+ }
176
+ ) ;
168
177
}
169
178
} ;
170
179
0 commit comments