/
file.go
166 lines (152 loc) · 4.37 KB
/
file.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
package controllers
import (
"context"
"net/http"
"strings"
"github.com/labstack/echo"
flog "github.com/xgxw/foundation-go/log"
fstorage "github.com/xgxw/foundation-go/storage"
"github.com/xgxw/xgxw-go"
"github.com/xgxw/xgxw-go/internal/utils"
)
type (
// FileController is ...
FileController struct {
logger *flog.Logger
fileSvc xgxw.FileService
}
)
// NewFileController is ...
func NewFileController(logger *flog.Logger, fileSvc xgxw.FileService) *FileController {
return &FileController{
logger: logger,
fileSvc: fileSvc,
}
}
func getFidFromPath(ctx echo.Context, prefix string) string {
path := ctx.Request().URL.Path
path = utils.CleanPath(path)
if !strings.HasPrefix(path, prefix) {
return ""
}
return path[len(prefix):]
}
// Get is 获取File.md
func (this *FileController) GetURL(ctx echo.Context) error {
fid := getFidFromPath(ctx, "/v1/url/")
if fid == "" {
return ctx.NoContent(http.StatusNotFound)
}
if strings.HasSuffix(fid, "/") {
return ctx.String(http.StatusRequestedRangeNotSatisfiable, "path is dir")
}
url, err := this.fileSvc.SignURL(context.Background(), fid, fstorage.HTTPGet, 0)
url = url[:4] + "s" + url[4:]
if err != nil {
this.logger.Error(err)
return ctx.NoContent(http.StatusNotFound)
}
return ctx.String(http.StatusOK, url)
}
// Get is 获取File.md
func (this *FileController) Get(ctx echo.Context) error {
fid := getFidFromPath(ctx, "/v1/file/")
if fid == "" {
return ctx.NoContent(http.StatusNotFound)
}
if strings.HasSuffix(fid, "/") {
return ctx.String(http.StatusRequestedRangeNotSatisfiable, "path is dir")
}
file, err := this.fileSvc.Get(context.Background(), fid)
if err != nil {
this.logger.Error(err)
return ctx.NoContent(http.StatusNotFound)
}
return ctx.JSON(http.StatusOK, file)
}
type putRequestCarrier struct {
Content string `json:"content" form:"content" query:"content"`
}
// Put is ...
func (this *FileController) Put(ctx echo.Context) error {
fid := getFidFromPath(ctx, "/v1/file/")
if fid == "" {
return ctx.NoContent(http.StatusNotFound)
}
if strings.HasSuffix(fid, "/") {
return ctx.String(http.StatusRequestedRangeNotSatisfiable, "path is dir")
}
r := new(putRequestCarrier)
if err := ctx.Bind(r); err != nil {
this.logger.Error(err)
return ctx.String(http.StatusBadRequest, "please input content")
}
err := this.fileSvc.Put(context.Background(), fid, r.Content)
if err != nil {
this.logger.Error(err)
return ctx.NoContent(http.StatusNotFound)
}
return ctx.NoContent(http.StatusOK)
}
// Del is ...
func (this *FileController) Del(ctx echo.Context) error {
fid := getFidFromPath(ctx, "/v1/file/")
if fid == "" {
return ctx.NoContent(http.StatusNotFound)
}
if strings.HasSuffix(fid, "/") {
return ctx.String(http.StatusRequestedRangeNotSatisfiable, "path is dir")
}
err := this.fileSvc.Del(context.Background(), fid)
if err != nil {
this.logger.Error(err)
return ctx.NoContent(http.StatusNotFound)
}
return ctx.NoContent(http.StatusOK)
}
type DelFilesRequestCarrier struct {
Fids []string `json:"fids" form:"fids" query:"fids"`
}
// DelFiles is ...
func (this *FileController) DelFiles(ctx echo.Context) error {
r := new(DelFilesRequestCarrier)
if err := ctx.Bind(r); err != nil {
this.logger.Error(err)
return ctx.String(http.StatusBadRequest, "please input fids")
}
err := this.fileSvc.DelFiles(context.Background(), r.Fids)
if err != nil {
this.logger.Error(err)
return ctx.NoContent(http.StatusNotFound)
}
return ctx.NoContent(http.StatusOK)
}
type GetCatalogRequestCarrier struct {
Options fstorage.ListOption `json:"options" form:"options" query:"options"`
}
type GetCatalogResopnseCarrier struct {
Catalog string `json:"catalog" form:"catalog" query:"catalog"`
Paths []string `json:"paths" form:"paths" query:"paths"`
}
// GetCatalog is ...
func (this *FileController) GetCatalog(ctx echo.Context) error {
path := getFidFromPath(ctx, "/v1/catalog/")
if path == "" {
return ctx.NoContent(http.StatusNotFound)
}
r := new(GetCatalogRequestCarrier)
if err := ctx.Bind(r); err != nil {
this.logger.Error(err)
return ctx.String(http.StatusBadRequest, "please input params")
}
catalog, paths, err := this.fileSvc.GetCatalog(context.Background(), path, r.Options)
resp := &GetCatalogResopnseCarrier{
Catalog: catalog,
Paths: paths,
}
if err != nil {
this.logger.Error(err)
return ctx.NoContent(http.StatusNotFound)
}
return ctx.JSON(http.StatusOK, resp)
}