@@ -356,6 +356,75 @@ func (h *Handlers) UploadProfilePicture(c *gin.Context) {
356
356
})
357
357
}
358
358
359
+ func (h * Handlers ) UploadStudentID (c * gin.Context ) {
360
+ userID , err := utils .GetUserIDFromContext (c )
361
+ if err != nil {
362
+ c .JSON (http .StatusInternalServerError , gin.H {"success" : false , "message" : []string {err .Error ()}})
363
+ return
364
+ }
365
+
366
+ user , err := h .UserService .GetUserByID (userID )
367
+ if err != nil {
368
+ c .JSON (http .StatusNotFound , gin.H {"success" : false , "message" : []string {err .Error ()}})
369
+ return
370
+ }
371
+
372
+ file , _ , err := c .Request .FormFile ("student_id" )
373
+ if err != nil {
374
+ c .JSON (http .StatusBadRequest , gin.H {"success" : false , "message" : []string {err .Error ()}})
375
+ return
376
+ }
377
+
378
+ // Check if file size is greater than 2MB
379
+ optimizedImage , err := utils .OptimizeImage (file , 2800 , 1080 )
380
+ if err != nil {
381
+ c .JSON (http .StatusInternalServerError , gin.H {"success" : false , "message" : []string {err .Error ()}})
382
+ return
383
+ }
384
+
385
+ optimizedImageBytes , err := io .ReadAll (optimizedImage )
386
+ if err != nil {
387
+ c .JSON (http .StatusInternalServerError , gin.H {"success" : false , "message" : []string {err .Error ()}})
388
+ return
389
+ }
390
+
391
+ // Choose storage service to upload image to (AWS or R2)
392
+ upload := utils .ChooseStorageService ()
393
+
394
+ // Upload image to storage service
395
+ if upload == utils .R2Service {
396
+ err = h .R2Service .UploadFileToR2 (context .Background (), "users" , "student_id_" + userID .String (), optimizedImageBytes )
397
+ if err != nil {
398
+ c .JSON (http .StatusInternalServerError , gin.H {"success" : false , "message" : []string {err .Error ()}})
399
+ return
400
+ }
401
+
402
+ imageUrl , _ := h .R2Service .GetFileR2 ("users" , "student_id_" + userID .String ())
403
+ user .StudentIDVerification = & imageUrl
404
+ } else {
405
+ err = h .AWSService .UploadFileToAWS (context .Background (), "users" , "student_id_" + userID .String (), optimizedImageBytes )
406
+ if err != nil {
407
+ c .JSON (http .StatusInternalServerError , gin.H {"success" : false , "message" : []string {err .Error ()}})
408
+ return
409
+ }
410
+
411
+ imageUrl , _ := h .AWSService .GetFileAWS ("users" , "student_id_" + userID .String ())
412
+ user .StudentIDVerification = & imageUrl
413
+ }
414
+
415
+ // Update user profile picture
416
+ if err := h .UserService .UploadProfilePicture (userID , user .ProfilePicture ); err != nil {
417
+ c .JSON (http .StatusInternalServerError , gin.H {"success" : false , "message" : []string {err .Error ()}})
418
+ return
419
+ }
420
+
421
+ c .JSON (http .StatusOK , gin.H {
422
+ "success" : true ,
423
+ "message" : "Profile Picture Uploaded Successfully" ,
424
+ "data" : user .ProfilePicture ,
425
+ })
426
+ }
427
+
359
428
func (h * Handlers ) EnableTwoFA (c * gin.Context ) {
360
429
userID , err := (& auth.Handlers {}).ExtractUserIDAndCheckPermission (c , "users:2fa" )
361
430
if err != nil {
0 commit comments