-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.go
53 lines (45 loc) · 1.73 KB
/
media.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package v1
import (
"fmt"
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/golanguzb70/simple-post-app/models"
"github.com/google/uuid"
)
// Upload photo
// @Summary Upload media
// @Description Through this api frontent can upload photo and get the link to the media.
// @Tags Media
// @Security BearerAuth
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "File"
// @Success 200 {object} models.MediaResponse
// @Failure default {object} models.StandardResponse
// @Router /media/photo [post]
func (h *handlerV1) UploadMedia(ctx *gin.Context) {
file := &models.File{}
err := ctx.ShouldBind(&file)
if h.HandleResponse(ctx, err, http.StatusBadRequest, BadRequest, "invalid body", nil) {
return
}
fileSize := file.File.Size
if fileSize > (int64(h.cfg.MaxImageSize) << 20) {
h.HandleResponse(ctx, fmt.Errorf(SizeExceeded), http.StatusRequestEntityTooLarge, SizeExceeded, fmt.Sprintf("Image size should be less than %d mb", h.cfg.MaxImageSize), nil)
return
}
ext := filepath.Ext(file.File.Filename)
if ext != ".jpg" && ext != ".png" {
h.HandleResponse(ctx, fmt.Errorf(SizeExceeded), http.StatusBadRequest, BadRequest, "Only .jpg and .png images are allowed", nil)
return
}
file.File.Filename = uuid.New().String() + filepath.Ext(file.File.Filename)
err = ctx.SaveUploadedFile(file.File, "./media/"+file.File.Filename)
if h.HandleResponse(ctx, err, http.StatusInternalServerError, InternalServerError, "UploadMedia:SaveUploadedFile", nil) {
return
}
h.HandleResponse(ctx, nil, http.StatusOK, Success, "", models.UploadPhotoRes{
URL: h.cfg.BaseUrl + "media/" + file.File.Filename,
})
}