Skip to content

Commit

Permalink
fix[backend]: fixed total file size when no files were uploaded
Browse files Browse the repository at this point in the history
  • Loading branch information
darmiel committed Nov 24, 2023
1 parent be5aab3 commit c2fdba1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 6 additions & 2 deletions backend/api/handlers/project_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}))
Expand Down
10 changes: 4 additions & 6 deletions backend/api/services/project_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package services

import (
"errors"
"fmt"
"github.com/darmiel/perplex/pkg/model"
"gorm.io/gorm"
"time"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit c2fdba1

Please sign in to comment.