summaryrefslogtreecommitdiff
path: root/internal/api/client/media/mediacreate.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2021-12-07 13:31:39 +0100
committerLibravatar GitHub <noreply@github.com>2021-12-07 13:31:39 +0100
commit0884f89431cd26bcc9674b3b7ab628b090f5881e (patch)
treecdd3b3f77f780a8b59d075dbcc3d4d013811e405 /internal/api/client/media/mediacreate.go
parentUpdate dependencies (#333) (diff)
downloadgotosocial-0884f89431cd26bcc9674b3b7ab628b090f5881e.tar.xz
Implement Cobra CLI tooling, Viper config tooling (#336)
* start pulling out + replacing urfave and config * replace many many instances of config * move more stuff => viper * properly remove urfave * move some flags to root command * add testrig commands to root * alias config file keys * start adding cli parsing tests * reorder viper init * remove config path alias * fmt * change config file keys to non-nested * we're more or less in business now * tidy up the common func * go fmt * get tests passing again * add note about the cliparsing tests * reorganize * update docs with changes * structure cmd dir better * rename + move some files around * fix dangling comma
Diffstat (limited to 'internal/api/client/media/mediacreate.go')
-rw-r--r--internal/api/client/media/mediacreate.go24
1 files changed, 16 insertions, 8 deletions
diff --git a/internal/api/client/media/mediacreate.go b/internal/api/client/media/mediacreate.go
index 5b7a2dea7..f808b554c 100644
--- a/internal/api/client/media/mediacreate.go
+++ b/internal/api/client/media/mediacreate.go
@@ -21,9 +21,11 @@ package media
import (
"errors"
"fmt"
- "github.com/sirupsen/logrus"
"net/http"
+ "github.com/sirupsen/logrus"
+ "github.com/spf13/viper"
+
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
@@ -102,7 +104,7 @@ func (m *Module) MediaCreatePOSTHandler(c *gin.Context) {
// Give the fields on the request form a first pass to make sure the request is superficially valid.
l.Tracef("validating form %+v", form)
- if err := validateCreateMedia(form, m.config.MediaConfig); err != nil {
+ if err := validateCreateMedia(form); err != nil {
l.Debugf("error validating form: %s", err)
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
@@ -119,24 +121,30 @@ func (m *Module) MediaCreatePOSTHandler(c *gin.Context) {
c.JSON(http.StatusOK, apiAttachment)
}
-func validateCreateMedia(form *model.AttachmentRequest, config *config.MediaConfig) error {
+func validateCreateMedia(form *model.AttachmentRequest) error {
// check there actually is a file attached and it's not size 0
if form.File == nil {
return errors.New("no attachment given")
}
+ keys := config.Keys
+ maxVideoSize := viper.GetInt(keys.MediaVideoMaxSize)
+ maxImageSize := viper.GetInt(keys.MediaImageMaxSize)
+ minDescriptionChars := viper.GetInt(keys.MediaDescriptionMinChars)
+ maxDescriptionChars := viper.GetInt(keys.MediaDescriptionMaxChars)
+
// a very superficial check to see if no size limits are exceeded
// we still don't actually know which media types we're dealing with but the other handlers will go into more detail there
- maxSize := config.MaxVideoSize
- if config.MaxImageSize > maxSize {
- maxSize = config.MaxImageSize
+ maxSize := maxVideoSize
+ if maxImageSize > maxSize {
+ maxSize = maxImageSize
}
if form.File.Size > int64(maxSize) {
return fmt.Errorf("file size limit exceeded: limit is %d bytes but attachment was %d bytes", maxSize, form.File.Size)
}
- if len(form.Description) < config.MinDescriptionChars || len(form.Description) > config.MaxDescriptionChars {
- return fmt.Errorf("image description length must be between %d and %d characters (inclusive), but provided image description was %d chars", config.MinDescriptionChars, config.MaxDescriptionChars, len(form.Description))
+ if len(form.Description) < minDescriptionChars || len(form.Description) > maxDescriptionChars {
+ return fmt.Errorf("image description length must be between %d and %d characters (inclusive), but provided image description was %d chars", minDescriptionChars, maxDescriptionChars, len(form.Description))
}
// TODO: validate focus here