From c2fdba17ef106083db14baa5283911b60c9a05e1 Mon Sep 17 00:00:00 2001 From: darmiel <71837281+darmiel@users.noreply.github.com> Date: Fri, 24 Nov 2023 10:36:33 +0100 Subject: [PATCH] fix[backend]: fixed total file size when no files were uploaded --- backend/api/handlers/project_handler.go | 8 ++++++-- backend/api/services/project_service.go | 10 ++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/backend/api/handlers/project_handler.go b/backend/api/handlers/project_handler.go index db47199..70c406d 100644 --- a/backend/api/handlers/project_handler.go +++ b/backend/api/handlers/project_handler.go @@ -295,7 +295,7 @@ func (h *ProjectHandler) UploadFile(ctx *fiber.Ctx) error { if err != nil { return ctx.Status(fiber.StatusInternalServerError).JSON(presenter.ErrorResponse(err)) } - totalSize = &ts + totalSize = ts } var uploaded uint @@ -395,8 +395,12 @@ func (h *ProjectHandler) FileQuotaInfo(ctx *fiber.Ctx) error { if err != nil { return ctx.Status(fiber.StatusInternalServerError).JSON(presenter.ErrorResponse(err)) } + var totalSizeUint64 uint64 + if totalSize != nil { + totalSizeUint64 = *totalSize + } return ctx.Status(fiber.StatusOK).JSON(presenter.SuccessResponse("", quotaInfoResponse{ - TotalSize: totalSize, + TotalSize: totalSizeUint64, Quota: p.ProjectFileSizeQuota, MaxFileSize: p.MaxProjectFileSize, })) diff --git a/backend/api/services/project_service.go b/backend/api/services/project_service.go index 62b9888..56ba445 100644 --- a/backend/api/services/project_service.go +++ b/backend/api/services/project_service.go @@ -2,7 +2,6 @@ package services import ( "errors" - "fmt" "github.com/darmiel/perplex/pkg/model" "gorm.io/gorm" "time" @@ -37,7 +36,7 @@ type ProjectService interface { FindFile(projectID uint, fileID uint) (*model.ProjectFile, error) FindFiles(projectID uint) ([]model.ProjectFile, error) DeleteFile(projectID uint, fileID uint) error - GetTotalProjectFileSize(projectID uint) (uint64, error) + GetTotalProjectFileSize(projectID uint) (*uint64, error) UpdateFileAccess(fileID uint) error } @@ -304,14 +303,13 @@ func (p *projectService) DeleteFile(projectID uint, fileID uint) error { }).Error } -func (p *projectService) GetTotalProjectFileSize(projectID uint) (uint64, error) { - var size uint64 +func (p *projectService) GetTotalProjectFileSize(projectID uint) (*uint64, error) { + var size *uint64 if err := p.DB.Model(&model.ProjectFile{}). Where("project_id = ?", projectID). Select("sum(size)"). Scan(&size).Error; err != nil { - fmt.Println("oh oh gorm error!") - return 0, err + return nil, err } return size, nil }