Skip to content

Commit

Permalink
Fix function name to use AddImageIDToRoom instead of AddImageIdToRoom
Browse files Browse the repository at this point in the history
  • Loading branch information
waveyboym committed Jul 20, 2024
1 parent f5eebdc commit bed657d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion occupi-backend/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ func GetUserImage(ctx *gin.Context, appsession *models.AppSession, email string)
return user.Details.ImageID, nil

Check warning on line 1397 in occupi-backend/pkg/database/database.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/database/database.go#L1397

Added line #L1397 was not covered by tests
}

func AddImageIdToRoom(ctx *gin.Context, appsession *models.AppSession, roomID, imageID string) error {
func AddImageIDToRoom(ctx *gin.Context, appsession *models.AppSession, roomID, imageID string) error {

Check warning on line 1400 in occupi-backend/pkg/database/database.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/database/database.go#L1400

Added line #L1400 was not covered by tests
// check if database is nil
if appsession.DB == nil {
logrus.Error("Database is nil")
Expand Down
56 changes: 31 additions & 25 deletions occupi-backend/pkg/handlers/api_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ func UploadProfileImage(ctx *gin.Context, appsession *models.AppSession) {
}

// Save the image to the database
new_id, err := database.UploadImageData(ctx, appsession, profileImage)
newID, err := database.UploadImageData(ctx, appsession, profileImage)

if err != nil {
logrus.WithError(err).Error("Failed to upload image data")
Expand All @@ -754,7 +754,7 @@ func UploadProfileImage(ctx *gin.Context, appsession *models.AppSession) {
}

// Update the user details with the image id
err = database.SetUserImage(ctx, appsession, requestEmail.Email, new_id)
err = database.SetUserImage(ctx, appsession, requestEmail.Email, newID)

if err != nil {
logrus.WithError(err).Error("Failed to set user image")
Expand All @@ -766,8 +766,8 @@ func UploadProfileImage(ctx *gin.Context, appsession *models.AppSession) {
}

func DownloadProfileImage(ctx *gin.Context, appsession *models.AppSession) {
var requestEmail models.ProfileImageRequest
if err := ctx.ShouldBindJSON(&requestEmail); err != nil {
var request models.ProfileImageRequest
if err := ctx.ShouldBindJSON(&request); err != nil {
email := ctx.Query("email")
quality := ctx.Query("quality")
if email == "" {
Expand All @@ -781,30 +781,30 @@ func DownloadProfileImage(ctx *gin.Context, appsession *models.AppSession) {
nil))
return
}
requestEmail.Email = email
request.Email = email
} else {
requestEmail.Email = email
request.Email = email
}
requestEmail.Quality = quality
request.Quality = quality

}

if requestEmail.Quality != "" && requestEmail.Quality != constants.ThumbnailRes && requestEmail.Quality != constants.LowRes && requestEmail.Quality != constants.MidRes && requestEmail.Quality != "constants.HighRes" {
requestEmail.Quality = constants.MidRes
} else if requestEmail.Quality == "" {
requestEmail.Quality = constants.MidRes
if request.Quality != "" && request.Quality != constants.ThumbnailRes && request.Quality != constants.LowRes && request.Quality != constants.MidRes && request.Quality != constants.HighRes {
request.Quality = constants.MidRes
} else if request.Quality == "" {
request.Quality = constants.MidRes
}

// get the image id
id, err := database.GetUserImage(ctx, appsession, requestEmail.Email)
id, err := database.GetUserImage(ctx, appsession, request.Email)
if err != nil {
logrus.WithError(err).Error("Failed to get user image")
ctx.JSON(http.StatusInternalServerError, utils.InternalServerError())
return
}

// get the image data
imageData, err := database.GetImageData(ctx, appsession, id, requestEmail.Quality)
imageData, err := database.GetImageData(ctx, appsession, id, request.Quality)
if err != nil {
logrus.WithError(err).Error("Failed to get image data")
ctx.JSON(http.StatusInternalServerError, utils.InternalServerError())
Expand All @@ -814,14 +814,17 @@ func DownloadProfileImage(ctx *gin.Context, appsession *models.AppSession) {
// set the response headers
ctx.Header("Content-Disposition", "attachment; filename="+imageData.FileName)
ctx.Header("/Content-Type", "application/octet-stream")
if requestEmail.Quality == constants.ThumbnailRes {
switch request.Quality {
case constants.ThumbnailRes:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.Thumbnail)
} else if requestEmail.Quality == constants.LowRes {
case constants.LowRes:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.ImageLowRes)
} else if requestEmail.Quality == constants.MidRes {
case constants.MidRes:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.ImageMidRes)
} else if requestEmail.Quality == "constants.HighRes" {
case constants.HighRes:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.ImageHighRes)
default:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.ImageMidRes)
}
}

Expand All @@ -841,7 +844,7 @@ func DownloadImage(ctx *gin.Context, appsession *models.AppSession) {
}
}

if request.Quality != "" && request.Quality != constants.ThumbnailRes && request.Quality != constants.LowRes && request.Quality != constants.MidRes && request.Quality != "constants.HighRes" {
if request.Quality != "" && request.Quality != constants.ThumbnailRes && request.Quality != constants.LowRes && request.Quality != constants.MidRes && request.Quality != constants.HighRes {
request.Quality = constants.MidRes
} else if request.Quality == "" {
request.Quality = constants.MidRes
Expand All @@ -860,14 +863,17 @@ func DownloadImage(ctx *gin.Context, appsession *models.AppSession) {
// set the response headers
ctx.Header("Content-Disposition", "attachment; filename="+imageData.FileName)
ctx.Header("/Content-Type", "application/octet-stream")
if request.Quality == constants.ThumbnailRes {
switch request.Quality {
case constants.ThumbnailRes:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.Thumbnail)
} else if request.Quality == constants.LowRes {
case constants.LowRes:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.ImageLowRes)
} else if request.Quality == constants.MidRes {
case constants.MidRes:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.ImageMidRes)
} else if request.Quality == "constants.HighRes" {
case constants.HighRes:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.ImageHighRes)
default:
ctx.Data(http.StatusOK, "application/octet-stream", imageData.ImageMidRes)
}
}

Expand Down Expand Up @@ -904,7 +910,7 @@ func UploadImage(ctx *gin.Context, appsession *models.AppSession, roomUpload boo
}

// Save the image to the database
new_id, err := database.UploadImageData(ctx, appsession, profileImage)
newID, err := database.UploadImageData(ctx, appsession, profileImage)

if err != nil {
ctx.JSON(http.StatusInternalServerError, utils.ErrorResponse(http.StatusInternalServerError, "Failed to upload image", constants.InternalServerErrorCode, "Failed to upload image", nil))
Expand All @@ -924,13 +930,13 @@ func UploadImage(ctx *gin.Context, appsession *models.AppSession, roomUpload boo
}

// Update the room details with the image id
err = database.AddImageIdToRoom(ctx, appsession, roomid, new_id)
err = database.AddImageIDToRoom(ctx, appsession, roomid, newID)

if err != nil {
ctx.JSON(http.StatusInternalServerError, utils.ErrorResponse(http.StatusInternalServerError, "Failed to update room image", constants.InternalServerErrorCode, "Failed to update room image", nil))
return
}
}

ctx.JSON(http.StatusOK, utils.SuccessResponse(http.StatusOK, "Successfully uploaded image!", gin.H{"id": new_id}))
ctx.JSON(http.StatusOK, utils.SuccessResponse(http.StatusOK, "Successfully uploaded image!", gin.H{"id": newID}))
}
15 changes: 10 additions & 5 deletions occupi-backend/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,14 @@ func GetClientTime(ctx *gin.Context) time.Time {
}

func ConvertImageToBytes(file *multipart.FileHeader, width uint, thumbnail bool) ([]byte, error) {
const (
pngExt = ".png"
jpgExt = ".jpg"
jpegExt = ".jpeg"
)

Check warning on line 587 in occupi-backend/pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/utils/utils.go#L582-L587

Added lines #L582 - L587 were not covered by tests
// Check the file extension
ext := filepath.Ext(file.Filename)
if ext != ".jpeg" && ext != ".jpg" && ext != ".png" {
if ext != jpegExt && ext != jpgExt && ext != pngExt {
return nil, errors.New("unsupported file type")

Check warning on line 591 in occupi-backend/pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/utils/utils.go#L589-L591

Added lines #L589 - L591 were not covered by tests
}

Expand All @@ -595,12 +600,12 @@ func ConvertImageToBytes(file *multipart.FileHeader, width uint, thumbnail bool)
// Decode the image
var img image.Image
switch ext {
case ".jpeg", ".jpg":
case jpegExt, jpgExt:
img, err = jpeg.Decode(src)
if err != nil {
return nil, err

Check warning on line 606 in occupi-backend/pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/utils/utils.go#L601-L606

Added lines #L601 - L606 were not covered by tests
}
case ".png":
case pngExt:
img, err = png.Decode(src)
if err != nil {
return nil, err

Check warning on line 611 in occupi-backend/pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/utils/utils.go#L608-L611

Added lines #L608 - L611 were not covered by tests
Expand All @@ -618,9 +623,9 @@ func ConvertImageToBytes(file *multipart.FileHeader, width uint, thumbnail bool)
// Convert the image to bytes
buf := new(bytes.Buffer)
switch ext {
case ".jpeg", ".jpg":
case jpegExt, jpgExt:
err = jpeg.Encode(buf, m, nil)
case ".png":
case pngExt:
err = png.Encode(buf, m)

Check warning on line 629 in occupi-backend/pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/utils/utils.go#L624-L629

Added lines #L624 - L629 were not covered by tests
}
if err != nil {
Expand Down

0 comments on commit bed657d

Please sign in to comment.