-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
136 lines (121 loc) ยท 3.87 KB
/
http.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
"path"
"strconv"
"strings"
)
func NewEcho() *echo.Echo {
e := echo.New()
e.Pre(middleware.RemoveTrailingSlash())
g := e.Group("api")
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${time_rfc3339} ${method} ${status} uri=${uri} latency=${latency}\n",
Skipper: func(context echo.Context) bool {
// health check log๋ ๋๋ฌด verboseํจ.
if context.Request().URL.RequestURI() == "/healthz" {
return true
}
return false
},
}))
e.GET("/healthz", func(c echo.Context) error { return c.String(200, "OK") })
g.POST("/images", ImageUploadRequestHandler, ForceContentTypeMultipartFormDataMiddleware)
return e
}
func ImageUploadRequestHandler(c echo.Context) error {
// Source
input, err := c.FormFile("image")
if err != nil {
logrus.Error(err)
return err
}
inputFileName := input.Filename
if err != nil {
logrus.Error(err)
return c.JSON(400, map[string]interface{}{
"data": nil,
"message": ErrWrongImageFileName.Error(),
})
}
var hashedFileName string
if c.FormValue("hashing") == "false" {
splited := strings.Split(inputFileName, ".")
// .ํ์ฅ์์ ํํ๊ฐ ์๋ ๊ฒฝ์ฐ
if len(splited) == 1 {
hashedFileName = inputFileName
} else {
hashedFileName = strings.Join(splited[:len(splited)-1], ".")
}
logrus.Println("Omit hashing. not hashed name:", hashedFileName)
} else {
hashedFileName = getHashedFileName(inputFileName)
logrus.Println("Hashed", inputFileName, "into", hashedFileName)
}
src, err := input.Open()
if err != nil {
logrus.Error(err)
return err
}
defer src.Close()
//imageData, _, gifImageData, ext, err := DecodeImageFile(src)
imageData, orientation, gifImageData, ext, err := DecodeImageFile(src)
if orientation != 0 {
imageData = RotateImage(imageData, orientation)
}
if err != nil {
logrus.Error(err)
return c.JSON(400, map[string]interface{}{
"data": nil,
"message": ErrUnableToDecodeImage.Error(),
})
}
DispatchMessages(&BaseImageTask{
ImageData: imageData,
GIFImageData: gifImageData,
OriginalFileName: inputFileName,
HashedFileName: hashedFileName,
Extension: ext,
})
resp := GenerateSuccessfullyUploadedResponse(hashedFileName + "." + ext)
logrus.Println(resp)
return c.JSON(200, resp)
}
type BaseResponse struct {
Data interface{} `json:"data"`
Message string `json:"message"`
}
type SuccessfullyUploadedResponseData struct {
RootEndpoint string `json:"root_endpoint"`
FileName string `json:"file_name"`
ThumbnailURL string `json:"thumbnail_url"`
Resized256URL string `json:"resized_256_url"`
Resized1024URL string `json:"resized_1024_url"`
}
// fileFullName์ ํ์ผ ์ด๋ฆ ์์ฒด์ ., ํ์ฅ์๋ช
์ด ๋ชจ๋ ์ฐ๊ฒฐ๋ ๋ฌธ์.
// e.g. abcd123.png
func GenerateSuccessfullyUploadedResponse(fileFullName string) *BaseResponse {
rootEndpoint := Config.Storage.Aws.Endpoint
return &BaseResponse{
Data: SuccessfullyUploadedResponseData{
RootEndpoint: rootEndpoint,
FileName: fileFullName,
ThumbnailURL: path.Join(rootEndpoint, "thumbnail", fileFullName),
Resized256URL: path.Join(rootEndpoint, "resized", strconv.Itoa(256), fileFullName),
Resized1024URL: path.Join(rootEndpoint, "resized", strconv.Itoa(1024), fileFullName),
},
}
}
func ForceContentTypeMultipartFormDataMiddleware(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
return func(context echo.Context) error {
if !strings.HasPrefix(context.Request().Header.Get("Content-Type"), "multipart/form-data") {
logrus.Warn("Content-Type in Request", context.Request().Header)
resp := BaseResponse{Message: "Unsupported Content-Type:" + context.Request().Header.Get("Content-Type") + ". Please use multipart/form-data."}
logrus.Error(resp)
return context.JSON(400, resp)
}
return handlerFunc(context)
}
}