dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 0b374e952b9dc410caac1a23b299163c678397a0
parent 724de4cfd4b19a7b03f499cd5c99029d86514190
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 12 Jan 2023 13:46:02 -0800

config instead of hardcoded values

Diffstat:
Mpkg/config/config.go | 6++++--
Mpkg/web/handlers/api/v1/topBarHandler.go | 5+++--
Mpkg/web/handlers/api/v1/uploadInterceptor.go | 11+++++++----
Mpkg/web/handlers/handlers.go | 8++++----
4 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/pkg/config/config.go b/pkg/config/config.go @@ -13,8 +13,10 @@ import ( ) const ( - DbFileName = "dkf.db" - AppDirName = ".dkf" + DbFileName = "dkf.db" + AppDirName = ".dkf" + MaxUserFileUploadSize = 30 << 20 + MaxUserTotalUploadSize = 100 << 20 ) // DefaultMasterKey Should be overwritten using ldflags diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go @@ -12,6 +12,7 @@ import ( "fmt" "github.com/Depado/bfchroma" chtml "github.com/alecthomas/chroma/formatters/html" + "github.com/dustin/go-humanize" "github.com/labstack/echo" "github.com/microcosm-cc/bluemonday" bf "github.com/russross/blackfriday/v2" @@ -266,8 +267,8 @@ func ChatTopBarHandler(c echo.Context) error { return c.Render(http.StatusOK, "chat-top-bar", data) } - if c.Request().ContentLength > 30<<20 { - data.Error = "The maximum file size is 30 MB" + if c.Request().ContentLength > config.MaxUserFileUploadSize { + data.Error = fmt.Sprintf("The maximum file size is %s", humanize.Bytes(config.MaxUserFileUploadSize)) return c.Render(http.StatusOK, "chat-top-bar", data) } diff --git a/pkg/web/handlers/api/v1/uploadInterceptor.go b/pkg/web/handlers/api/v1/uploadInterceptor.go @@ -1,11 +1,14 @@ package v1 import ( + "dkforest/pkg/config" "dkforest/pkg/database" "dkforest/pkg/utils" hutils "dkforest/pkg/web/handlers/utils" "errors" + "fmt" "github.com/asaskevich/govalidator" + "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "io/ioutil" "mime/multipart" @@ -31,12 +34,12 @@ func handleUploadedFile(file multipart.File, handler *multipart.FileHeader, auth return nil, hutils.AccountTooYoungErr } userSizeUploaded := database.GetUserTotalUploadSize(authUser.ID) - if handler.Size+userSizeUploaded > 100<<20 { - return nil, errors.New("user upload limit reached (100 MB)") + if handler.Size+userSizeUploaded > config.MaxUserTotalUploadSize { + return nil, fmt.Errorf("user upload limit reached (%s)", humanize.Bytes(config.MaxUserTotalUploadSize)) } origFileName := handler.Filename - if handler.Size > 30<<20 { - return nil, errors.New("the maximum file size is 30 MB") + if handler.Size > config.MaxUserFileUploadSize { + return nil, fmt.Errorf("the maximum file size is %s", humanize.Bytes(config.MaxUserFileUploadSize)) } if !govalidator.StringLength(origFileName, "3", "50") { return nil, errors.New("invalid file name, 3-50 characters") diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -4057,8 +4057,8 @@ func BHCHandler(c echo.Context) error { func FileDropHandler(c echo.Context) error { uuidParam := c.Param("uuid") - //if c.Request().ContentLength > 30<<20 { - // data.Error = "The maximum file size is 30 MB" + //if c.Request().ContentLength > config.MaxUserFileUploadSize { + // data.Error = fmt.Sprintf("The maximum file size is %s", humanize.Bytes(config.MaxUserFileUploadSize)) // return c.Render(http.StatusOK, "chat-top-bar", data) //} @@ -4086,8 +4086,8 @@ func FileDropHandler(c echo.Context) error { defer file.Close() origFileName := handler.Filename - //if handler.Size > 30<<20 { - // return nil, html, errors.New("the maximum file size is 30 MB") + //if handler.Size > config.MaxUserFileUploadSize { + // return nil, html, fmt.Errorf("the maximum file size is %s", humanize.Bytes(config.MaxUserFileUploadSize)) //} if !govalidator.StringLength(origFileName, "3", "50") { data.Error = "invalid file name, 3-50 characters"