/
upload.go
61 lines (51 loc) · 1.56 KB
/
upload.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
54
55
56
57
58
59
60
61
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/EDDYCJY/go-gin-example/pkg/app"
"github.com/EDDYCJY/go-gin-example/pkg/e"
"github.com/EDDYCJY/go-gin-example/pkg/logging"
"github.com/EDDYCJY/go-gin-example/pkg/upload"
)
// @Summary Import Image
// @Produce json
// @Param image formData file true "Image File"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/tags/import [post]
func UploadImage(c *gin.Context) {
appG := app.Gin{C: c}
file, image, err := c.Request.FormFile("image")
if err != nil {
logging.Warn(err)
appG.Response(http.StatusInternalServerError, e.ERROR, nil)
return
}
if image == nil {
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}
imageName := upload.GetImageName(image.Filename)
fullPath := upload.GetImageFullPath()
savePath := upload.GetImagePath()
src := fullPath + imageName
if !upload.CheckImageExt(imageName) || !upload.CheckImageSize(file) {
appG.Response(http.StatusBadRequest, e.ERROR_UPLOAD_CHECK_IMAGE_FORMAT, nil)
return
}
err = upload.CheckImage(fullPath)
if err != nil {
logging.Warn(err)
appG.Response(http.StatusInternalServerError, e.ERROR_UPLOAD_CHECK_IMAGE_FAIL, nil)
return
}
if err := c.SaveUploadedFile(image, src); err != nil {
logging.Warn(err)
appG.Response(http.StatusInternalServerError, e.ERROR_UPLOAD_SAVE_IMAGE_FAIL, nil)
return
}
appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
"image_url": upload.GetImageFullUrl(imageName),
"image_save_url": savePath + imageName,
})
}