@@ -17,40 +17,45 @@ const s3Client = new S3({
1717const createCertificate = async ( certificate , hash ) => {
1818 const params = { Bucket : process . env . AWS_BUCKET_NAME , Key : 'bases/certificado-base.png' , } ;
1919
20- return new Promise ( ( resolve ) => {
20+ return new Promise ( ( resolve , reject ) => {
2121 s3Client . getObject ( params , async ( error , data ) => {
2222 if ( error ) {
2323 reject ( error ) ;
2424 return ;
2525 }
26-
27- const canvas = Canvas . createCanvas ( 1600 , 1194 ) ;
28- let canvasContext = canvas . getContext ( '2d' ) ;
29- const canvasImage = new Canvas . Image ( ) ;
30- const courseDates = getCourseDates ( certificate . period . dates ) ;
31-
32- canvasImage . src = Buffer . from ( await data . Body . transformToByteArray ( ) ) ;
33-
34- if ( certificate . type . name == "student" ) {
35- buildCertificateToStudent ( canvasContext , canvasImage , canvas , certificate , hash , courseDates ) ;
36- canvasContext = await addSignatureToCertificate ( canvasContext , certificate . course . responsibles ) ;
37- }
3826
39- if ( certificate . type . name == "author" || certificate . type . name == "speaker" ) {
40- buildCertificateToAuthorOrSpeaker (
41- canvasContext , canvasImage , canvas ,
42- certificate , hash , courseDates
43- ) ;
44- canvasContext = await addSignatureToCertificate ( canvasContext , certificate . course . responsibles ) ;
45- }
27+ try {
28+ const canvas = Canvas . createCanvas ( 1600 , 1194 ) ;
29+ let canvasContext = canvas . getContext ( '2d' ) ;
30+ const canvasImage = new Canvas . Image ( ) ;
31+ const courseDates = getCourseDates ( certificate . period . dates ) ;
32+
33+ canvasImage . src = Buffer . from ( await data . Body . transformToByteArray ( ) ) ;
34+
35+ if ( certificate . type . name == "student" ) {
36+ buildCertificateToStudent ( canvasContext , canvasImage , canvas , certificate , hash , courseDates ) ;
37+ canvasContext = await addSignatureToCertificate ( canvasContext , certificate . course . responsibles ) ;
38+ }
4639
47- if ( certificate . type . name == "teacher" ) {
48- // todo
40+ if ( certificate . type . name == "author" || certificate . type . name == "speaker" ) {
41+ buildCertificateToAuthorOrSpeaker (
42+ canvasContext , canvasImage , canvas ,
43+ certificate , hash , courseDates
44+ ) ;
45+ canvasContext = await addSignatureToCertificate ( canvasContext , certificate . course . responsibles ) ;
46+ }
47+
48+ if ( certificate . type . name == "teacher" ) {
49+ // todo
50+ }
51+
52+ const certificateOut = fs . createWriteStream ( path . join ( __dirname , `/../images/${ hash } .png` ) ) ;
53+ canvas . createPNGStream ( ) . pipe ( certificateOut ) ;
54+ certificateOut . on ( 'finish' , ( ) => resolve ( true ) ) ;
55+ } catch ( e ) {
56+ console . error ( e ) ;
57+ reject ( e . message ) ;
4958 }
50-
51- const certificateOut = fs . createWriteStream ( path . join ( __dirname , `/../images/${ hash } .png` ) ) ;
52- canvas . createPNGStream ( ) . pipe ( certificateOut ) ;
53- certificateOut . on ( 'finish' , ( ) => resolve ( true ) ) ;
5459 } )
5560 } ) ;
5661}
@@ -161,17 +166,22 @@ const addSignatureToCertificate = async (certificateCanvas, responsibles) => {
161166const uploadCertificate = ( hash ) => {
162167 const certificatePath = path . join ( __dirname , `/../images/${ hash } .png` ) ;
163168
164- return new Promise ( ( resolve ) => {
165- s3Client . putObject ( {
166- Bucket : process . env . AWS_BUCKET_NAME ,
167- Key : `certificados/${ hash } .png` ,
168- Body : fs . readFileSync ( certificatePath ) ,
169- } , async function ( error , data ) {
170- if ( error ) return console . log ( 'Upload error: ' , err . message ) ;
171- fs . unlink ( certificatePath , ( ) => console . log ( 'Certificado emitido.' ) ) ;
172-
173- resolve ( await getCertificateImageSignedUrl ( hash ) ) ;
174- } ) ;
169+ return new Promise ( ( resolve , reject ) => {
170+ try {
171+ s3Client . putObject ( {
172+ Bucket : process . env . AWS_BUCKET_NAME ,
173+ Key : `certificados/${ hash } .png` ,
174+ Body : fs . readFileSync ( certificatePath ) ,
175+ } , async function ( error , data ) {
176+ if ( error ) return console . log ( 'Upload error: ' , err . message ) ;
177+ fs . unlink ( certificatePath , ( ) => console . log ( 'Certificado emitido.' ) ) ;
178+
179+ resolve ( await getCertificateImageSignedUrl ( hash ) ) ;
180+ } ) ;
181+ } catch ( e ) {
182+ console . error ( e ) ;
183+ reject ( e . message )
184+ }
175185 } )
176186}
177187
0 commit comments