Skip to content

Commit

Permalink
📝 update swagger with correct file formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Alban Rahier committed Dec 11, 2022
1 parent fe78132 commit 98b4fe3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 34 deletions.
19 changes: 10 additions & 9 deletions controller/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/h2non/filetype/matchers"
"github.com/haran/biophonie-api/controller/geopoint"
"github.com/haran/biophonie-api/controller/user"
"github.com/haran/biophonie-api/database"
Expand Down Expand Up @@ -351,8 +352,8 @@ func (c *Controller) MakeAdmin(ctx *gin.Context) {
// @Produce json
// @Tags Geopoint
// @Param geopoint formData string true "geopoint infos in a utf-8 json file"
// @Param sound formData file true "geopoint sound"
// @Param picture formData file false "geopoint picture"
// @Param sound formData file true "geopoint sound in aac"
// @Param picture formData file false "geopoint picture in webp"
// @Param Authorization header string true "Authentication header"
// @Success 200 {object} geopoint.GeoPoint
// @Failure 404 {object} controller.ErrMsg
Expand Down Expand Up @@ -382,20 +383,20 @@ func (c *Controller) BindGeoPoint(ctx *gin.Context) {
return
}

soundName := uuid.NewString() + ".wav"
pictureName := uuid.NewString() + ".jpg"
if !httputil.CheckFileContentType(bindGeo.Sound, "audio/wave") {
ctx.AbortWithError(http.StatusBadRequest, errors.New("sound was not wave file")).SetType(gin.ErrorTypePublic)
soundName := uuid.NewString() + ".aac"
pictureName := uuid.NewString() + ".webp"
if !httputil.CheckFileContentType(bindGeo.Sound, matchers.Aac) {
ctx.AbortWithError(http.StatusBadRequest, errors.New("sound was not aac file")).SetType(gin.ErrorTypePublic)
return
}

if addGeo.PictureTemplate == "" {
if !httputil.CheckFileContentType(bindGeo.Picture, "image/jpeg") {
ctx.AbortWithError(http.StatusBadRequest, errors.New("image was not jpeg file")).SetType(gin.ErrorTypePublic)
if !httputil.CheckFileContentType(bindGeo.Picture, matchers.Webp) {
ctx.AbortWithError(http.StatusBadRequest, errors.New("image was not webp file")).SetType(gin.ErrorTypePublic)
return
}
} else {
pictureName = addGeo.PictureTemplate + ".jpg"
pictureName = addGeo.PictureTemplate + ".webp"
}

addGeo.UserId, _ = ctx.MustGet("userId").(int)
Expand Down
4 changes: 2 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,14 @@ var doc = `{
},
{
"type": "file",
"description": "geopoint sound",
"description": "geopoint sound in aac",
"name": "sound",
"in": "formData",
"required": true
},
{
"type": "file",
"description": "geopoint picture",
"description": "geopoint picture in webp",
"name": "picture",
"in": "formData"
},
Expand Down
4 changes: 2 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@
},
{
"type": "file",
"description": "geopoint sound",
"description": "geopoint sound in aac",
"name": "sound",
"in": "formData",
"required": true
},
{
"type": "file",
"description": "geopoint picture",
"description": "geopoint picture in webp",
"name": "picture",
"in": "formData"
},
Expand Down
4 changes: 2 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ paths:
name: geopoint
required: true
type: string
- description: geopoint sound
- description: geopoint sound in aac
in: formData
name: sound
required: true
type: file
- description: geopoint picture
- description: geopoint picture in webp
in: formData
name: picture
type: file
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ require (
)

require (
github.com/gabriel-vasile/mimetype v1.4.1 // indirect
github.com/gin-contrib/static v0.0.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/cridenour/go-postgis v1.0.0/go.mod h1:AtTeWrKgwtl5WjwG1oJwcc3AHIf/EC1
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.1 h1:TRWk7se+TOjCYgRth7+1/OYLNiRNIotknkFtf/dnN7Q=
github.com/gabriel-vasile/mimetype v1.4.1/go.mod h1:05Vi0w3Y9c/lNvJOdmIwvrrAhX3rYhfQQCaf9VJcv7M=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g=
github.com/gin-contrib/cors v1.4.0/go.mod h1:bs9pNM0x/UsmHPBWT2xZz9ROh8xYjYkiURUfmBoMlcs=
Expand Down Expand Up @@ -68,6 +70,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down Expand Up @@ -178,6 +182,7 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220524220425-1d687d428aca h1:xTaFYiPROfpPhqrfTIDXj0ri1SpfueYT951s4bAuDO8=
golang.org/x/net v0.0.0-20220524220425-1d687d428aca/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -194,6 +199,7 @@ golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
Expand Down
25 changes: 6 additions & 19 deletions httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package httputil
import (
"log"
"mime/multipart"
"net/http"

"github.com/gin-gonic/gin"
"github.com/h2non/filetype/matchers"
)

// NewError example
Expand All @@ -23,7 +23,7 @@ type HTTPError struct {
Message string `json:"message" example:"status bad request"`
}

func CheckFileContentType(fileHeader *multipart.FileHeader, checkType string) bool {
func CheckFileContentType(fileHeader *multipart.FileHeader, matcher matchers.Matcher) bool {
if fileHeader == nil {
return false
}
Expand All @@ -34,25 +34,12 @@ func CheckFileContentType(fileHeader *multipart.FileHeader, checkType string) bo
}
defer file.Close()

detectedType, err := getFileContentType(file)
if err != nil {
log.Panicln(err)
}

return detectedType == checkType
}
buffer := make([]byte, 261)

func getFileContentType(out multipart.File) (string, error) {

// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)

_, err := out.Read(buffer)
_, err = file.Read(buffer)
if err != nil {
return "", err
return false
}

contentType := http.DetectContentType(buffer)

return contentType, nil
return matcher(buffer)
}

0 comments on commit 98b4fe3

Please sign in to comment.