/
files.go
173 lines (151 loc) · 4.79 KB
/
files.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
package v3
import (
"fmt"
"net/http"
"strconv"
"strings"
vd "github.com/go-ozzo/ozzo-validation/v4"
"github.com/gofrs/uuid"
"github.com/labstack/echo/v4"
"github.com/traPtitech/traQ/model"
"github.com/traPtitech/traQ/repository"
"github.com/traPtitech/traQ/router/consts"
"github.com/traPtitech/traQ/router/extension/herror"
"github.com/traPtitech/traQ/router/utils"
"github.com/traPtitech/traQ/service/file"
"github.com/traPtitech/traQ/utils/optional"
)
// GetFilesRequest GET /files 用リクエストクエリ
type GetFilesRequest struct {
Limit int `query:"limit"`
Offset int `query:"offset"`
Since optional.Time `query:"since"`
Until optional.Time `query:"until"`
Inclusive bool `query:"inclusive"`
Order string `query:"order"`
ChannelID uuid.UUID `query:"channelId"`
Mine bool `query:"mine"`
}
func (q *GetFilesRequest) Validate() error {
if q.Limit == 0 {
q.Limit = 20
}
return vd.ValidateStruct(q,
vd.Field(&q.Limit, vd.Min(1), vd.Max(200)),
vd.Field(&q.Offset, vd.Min(0)),
vd.Field(&q.Mine, vd.When(q.ChannelID == uuid.Nil, vd.Required)),
)
}
// GetFiles GET /files
func (h *Handlers) GetFiles(c echo.Context) error {
var req GetFilesRequest
if err := bindAndValidate(c, &req); err != nil {
return err
}
q := repository.FilesQuery{
Since: req.Since,
Until: req.Until,
Inclusive: req.Inclusive,
Limit: req.Limit,
Offset: req.Offset,
Asc: strings.ToLower(req.Order) == "asc",
Type: model.FileTypeUserFile,
}
if req.Mine {
q.UploaderID = optional.UUIDFrom(getRequestUserID(c))
}
if req.ChannelID != uuid.Nil {
// チャンネルアクセス権確認
if ok, err := h.ChannelManager.IsChannelAccessibleToUser(getRequestUserID(c), req.ChannelID); err != nil {
return herror.InternalServerError(err)
} else if !ok {
return herror.BadRequest("invalid channelId")
}
q.ChannelID = optional.UUIDFrom(req.ChannelID)
}
files, more, err := h.FileManager.List(q)
if err != nil {
return herror.InternalServerError(err)
}
c.Response().Header().Set(consts.HeaderMore, strconv.FormatBool(more))
return c.JSON(http.StatusOK, formatFileInfos(files))
}
// PostFile POST /files
func (h *Handlers) PostFile(c echo.Context) error {
userID := getRequestUserID(c)
// ファイルチェック
src, uploadedFile, err := c.Request().FormFile("file")
if err != nil {
return herror.BadRequest(err)
}
defer src.Close()
if uploadedFile.Size == 0 {
return herror.BadRequest("non-empty file is required")
}
args := file.SaveArgs{
FileName: uploadedFile.Filename,
FileSize: uploadedFile.Size,
MimeType: uploadedFile.Header.Get(echo.HeaderContentType),
FileType: model.FileTypeUserFile,
CreatorID: optional.UUIDFrom(userID),
Src: src,
}
// チャンネルアクセス権確認
channelID := uuid.FromStringOrNil(c.FormValue("channelId"))
if ok, err := h.ChannelManager.IsChannelAccessibleToUser(userID, channelID); err != nil {
return herror.InternalServerError(err)
} else if !ok {
return herror.BadRequest("invalid channelId")
}
ch, err := h.ChannelManager.GetChannel(channelID)
if err != nil {
return herror.InternalServerError(err)
}
if ch.IsArchived() {
return herror.BadRequest(fmt.Sprintf("channel #%s has been archived", h.ChannelManager.PublicChannelTree().GetChannelPath(ch.ID)))
}
if !ch.IsPublic {
// アクセスコントロール設定
members, err := h.ChannelManager.GetDMChannelMembers(ch.ID)
if err != nil {
return herror.InternalServerError(err)
}
for _, v := range members {
args.ACLAllow(v)
}
}
args.ChannelID = optional.UUIDFrom(channelID)
// 保存
file, err := h.FileManager.Save(args)
if err != nil {
return herror.InternalServerError(err)
}
return c.JSON(http.StatusCreated, formatFileInfo(file))
}
// GetFileMeta GET /files/:fileID/meta
func (h *Handlers) GetFileMeta(c echo.Context) error {
c.Response().Header().Set(consts.HeaderCacheControl, "private, max-age=86400") // 1日キャッシュ
return c.JSON(http.StatusOK, formatFileInfo(getParamFile(c)))
}
// GetThumbnailImage GET /files/:fileID/thumbnail
func (h *Handlers) GetThumbnailImage(c echo.Context) error {
return utils.ServeFileThumbnail(c, getParamFile(c))
}
// GetFile GET /files/:fileID
func (h *Handlers) GetFile(c echo.Context) error {
return utils.ServeFile(c, getParamFile(c))
}
// DeleteFile DELETE /files/:fileID
func (h *Handlers) DeleteFile(c echo.Context) error {
f := getParamFile(c)
if !f.GetCreatorID().Valid || f.GetFileType() != model.FileTypeUserFile {
return herror.Forbidden()
}
if f.GetCreatorID().UUID != getRequestUserID(c) { // TODO 管理者権限
return herror.Forbidden()
}
if err := h.FileManager.Delete(f.GetID()); err != nil {
return herror.InternalServerError(err)
}
return c.NoContent(http.StatusNoContent)
}