-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
189 lines (171 loc) · 4.8 KB
/
storage.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gin_storage
import (
"encoding/json"
"github.com/cockroachdb/errors"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
errorhandlerTool "github.com/justdomepaul/gin-storage/pkg/errorhandler"
"github.com/justdomepaul/gin-storage/storage"
"github.com/justdomepaul/toolbox/errorhandler"
"net/http"
)
const (
// DefaultPrefix url prefix of pprof
DefaultPrefix = "/storage"
)
func getPrefix(prefixOptions ...string) string {
prefix := DefaultPrefix
if len(prefixOptions) > 0 {
prefix = prefixOptions[0]
}
return prefix
}
func Register(r *gin.Engine, prefixOptions ...string) (closeFn func()) {
return RouteRegister(&(r.RouterGroup), prefixOptions...)
}
func RouteRegister(rg *gin.RouterGroup, prefixOptions ...string) (closeFn func()) {
fileStorage, fn := storage.Load()
handler := NewFileHandler(fileStorage)
prefixRouter := rg.Group(getPrefix(prefixOptions...))
{
prefixRouter.GET("", handler.List)
prefixRouter.POST("", handler.Upload)
prefixRouter.POST("/multiple", handler.Batch)
prefixRouter.PUT("", handler.Publicize)
prefixRouter.PUT("/multiple", handler.MultiplePublicize)
prefixRouter.DELETE("", handler.Remove)
}
return fn
}
func NewFileHandler(storage storage.IFile) *FileHandler {
return &FileHandler{
storage: storage,
}
}
type FileHandler struct {
storage storage.IFile
}
func (fh FileHandler) Upload(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
panic(errorhandler.NewErrVariable(err))
}
f, err := file.Open()
if err != nil {
panic(errorhandler.NewErrExecute(err))
}
path, err := fh.storage.Upload(c, c.Request.FormValue("prefix"), f)
if err != nil {
panic(errorhandler.NewErrDBExecute(err))
}
c.JSON(http.StatusOK, gin.H{
"path": path,
})
}
type BatchFile struct {
Filename string `json:"filename,omitempty" validate:"required"`
Path string `json:"path,omitempty" validate:"required"`
}
type PublicizeURL struct {
Filename string `json:"filename,omitempty" validate:"required"`
URL string `json:"url,omitempty" validate:"required"`
}
func (fh FileHandler) Batch(c *gin.Context) {
form, err := c.MultipartForm()
if err != nil {
panic(errorhandler.NewErrVariable(err))
}
var responsePaths []BatchFile
for _, file := range form.File["file[]"] {
f, err := file.Open()
if err != nil {
panic(errorhandler.NewErrExecute(err))
}
path, err := fh.storage.Upload(c, c.Request.FormValue("prefix"), f)
if err != nil {
panic(errorhandler.NewErrDBExecute(err))
}
responsePaths = append(responsePaths, BatchFile{
Filename: file.Filename,
Path: path,
})
}
c.JSON(http.StatusOK, responsePaths)
}
func (fh FileHandler) Publicize(c *gin.Context) {
req := struct {
Path string `json:"path,omitempty" validate:"required"`
}{}
defer c.Request.Body.Close()
if err := json.NewDecoder(c.Request.Body).Decode(&req); err != nil {
panic(errorhandler.NewErrJSONUnmarshal(err))
}
if err := validator.New().Struct(&req); err != nil {
panic(errorhandler.NewErrVariable(err))
}
url, err := fh.storage.GetURL(c, req.Path)
if err != nil {
panic(errorhandler.NewErrDBExecute(err))
}
c.JSON(http.StatusOK, gin.H{
"url": url,
})
}
func (fh FileHandler) MultiplePublicize(c *gin.Context) {
req := struct {
Paths []BatchFile `json:"paths,omitempty" validate:"required,min=1,dive"`
}{}
defer c.Request.Body.Close()
if err := json.NewDecoder(c.Request.Body).Decode(&req); err != nil {
panic(errorhandler.NewErrJSONUnmarshal(err))
}
if err := validator.New().Struct(&req); err != nil {
panic(errorhandler.NewErrVariable(err))
}
var responseURLs []PublicizeURL
for _, path := range req.Paths {
url, err := fh.storage.GetURL(c, path.Path)
if err != nil {
panic(errorhandler.NewErrDBExecute(err))
}
responseURLs = append(responseURLs, PublicizeURL{
Filename: path.Filename,
URL: url,
})
}
c.JSON(http.StatusOK, responseURLs)
}
func (fh FileHandler) Remove(c *gin.Context) {
req := struct {
Path string `validate:"required"`
}{
Path: c.Query("path"),
}
defer c.Request.Body.Close()
if err := validator.New().Struct(&req); err != nil {
panic(errorhandler.NewErrVariable(err))
}
if err := fh.storage.Remove(c, req.Path); err != nil {
panic(errorhandler.NewErrDBExecute(err))
}
c.String(http.StatusOK, "ok")
}
func (fh FileHandler) List(c *gin.Context) {
q := storage.Query{}
if c.Query("delimiter") != "" {
q = storage.WithFileCloudDelimiter(q, c.Query("delimiter"))
}
if c.Query("prefix") != "" {
q = storage.WithFileCloudPrefix(q, c.Query("prefix"))
}
var files []storage.File
if err := fh.storage.List(c, q, func(file storage.File) error {
files = append(files, file)
return nil
}); errors.Is(err, errorhandlerTool.ErrGetFile) {
panic(errorhandler.NewErrDBRowNotFound(err))
} else if err != nil {
panic(errorhandler.NewErrDBExecute(err))
}
c.JSON(http.StatusOK, files)
}