Implementation endpoint of APIv4: GET /files/{file_id}/thumbnail (#5553)
* APIv4: GET /files/{file_id}/thumbnail
* added delay time
Этот коммит содержится в:
коммит произвёл
enahum
родитель
2a621f7470
Коммит
28c218db3b
33
api4/file.go
33
api4/file.go
@@ -23,6 +23,7 @@ func InitFile() {
|
||||
|
||||
BaseRoutes.Files.Handle("", ApiSessionRequired(uploadFile)).Methods("POST")
|
||||
BaseRoutes.File.Handle("", ApiSessionRequired(getFile)).Methods("GET")
|
||||
BaseRoutes.File.Handle("/thumbnail", ApiSessionRequired(getFileThumbnail)).Methods("GET")
|
||||
|
||||
}
|
||||
|
||||
@@ -92,6 +93,38 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func getFileThumbnail(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireFileId()
|
||||
if c.Err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
info, err := app.GetFileInfo(c.Params.FileId)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
if info.CreatorId != c.Session.UserId && !app.SessionHasPermissionToChannelByPost(c.Session, info.PostId, model.PERMISSION_READ_CHANNEL) {
|
||||
c.SetPermissionError(model.PERMISSION_READ_CHANNEL)
|
||||
return
|
||||
}
|
||||
|
||||
if info.ThumbnailPath == "" {
|
||||
c.Err = model.NewLocAppError("getFileThumbnail", "api.file.get_file_thumbnail.no_thumbnail.app_error", nil, "file_id="+info.Id)
|
||||
c.Err.StatusCode = http.StatusBadRequest
|
||||
return
|
||||
}
|
||||
|
||||
if data, err := app.ReadFile(info.ThumbnailPath); err != nil {
|
||||
c.Err = err
|
||||
c.Err.StatusCode = http.StatusNotFound
|
||||
} else if err := writeFileResponse(info.Name, info.MimeType, data, w, r); err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func writeFileResponse(filename string, contentType string, bytes []byte, w http.ResponseWriter, r *http.Request) *model.AppError {
|
||||
w.Header().Set("Cache-Control", "max-age=2592000, public")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
|
||||
|
||||
@@ -149,3 +149,55 @@ func TestGetFile(t *testing.T) {
|
||||
_, resp = th.SystemAdminClient.GetFile(fileId)
|
||||
CheckNoError(t, resp)
|
||||
}
|
||||
|
||||
func TestGetFileThumbnail(t *testing.T) {
|
||||
th := Setup().InitBasic().InitSystemAdmin()
|
||||
defer TearDown()
|
||||
Client := th.Client
|
||||
channel := th.BasicChannel
|
||||
|
||||
if utils.Cfg.FileSettings.DriverName == "" {
|
||||
t.Skip("skipping because no file driver is enabled")
|
||||
}
|
||||
|
||||
fileId := ""
|
||||
var sent []byte
|
||||
var err error
|
||||
if sent, err = readTestFile("test.png"); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
fileResp, resp := Client.UploadFile(sent, channel.Id, "test.png")
|
||||
CheckNoError(t, resp)
|
||||
|
||||
fileId = fileResp.FileInfos[0].Id
|
||||
}
|
||||
|
||||
// Wait a bit for files to ready
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
data, resp := Client.GetFileThumbnail(fileId)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
if data == nil || len(data) == 0 {
|
||||
t.Fatal("should not be empty")
|
||||
}
|
||||
|
||||
_, resp = Client.GetFileThumbnail("junk")
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
_, resp = Client.GetFileThumbnail(model.NewId())
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
_, resp = Client.GetFileThumbnail(fileId)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
otherUser := th.CreateUser()
|
||||
Client.Login(otherUser.Email, otherUser.Password)
|
||||
_, resp = Client.GetFileThumbnail(fileId)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
_, resp = th.SystemAdminClient.GetFileThumbnail(fileId)
|
||||
CheckNoError(t, resp)
|
||||
}
|
||||
|
||||
@@ -882,6 +882,17 @@ func (c *Client4) GetFile(fileId string) ([]byte, *Response) {
|
||||
}
|
||||
}
|
||||
|
||||
// GetFileThumbnail gets the bytes for a file by id.
|
||||
func (c *Client4) GetFileThumbnail(fileId string) ([]byte, *Response) {
|
||||
if r, err := c.DoApiGet(c.GetFileRoute(fileId)+"/thumbnail", ""); err != nil {
|
||||
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
||||
} else if data, err := ioutil.ReadAll(r.Body); err != nil {
|
||||
return nil, &Response{StatusCode: r.StatusCode, Error: NewAppError("GetFileThumbnail", "model.client.read_file.app_error", nil, err.Error(), r.StatusCode)}
|
||||
} else {
|
||||
return data, BuildResponse(r)
|
||||
}
|
||||
}
|
||||
|
||||
// GetFileInfosForPost gets all the file info objects attached to a post.
|
||||
func (c *Client4) GetFileInfosForPost(postId string, etag string) ([]*FileInfo, *Response) {
|
||||
if r, err := c.DoApiGet(c.GetPostRoute(postId)+"/files/info", etag); err != nil {
|
||||
|
||||
Ссылка в новой задаче
Block a user