коммит произвёл
Joram Wilander
родитель
c84b761e75
Коммит
8b59a2a291
33
api4/file.go
33
api4/file.go
@@ -25,6 +25,7 @@ func InitFile() {
|
||||
BaseRoutes.File.Handle("", ApiSessionRequired(getFile)).Methods("GET")
|
||||
BaseRoutes.File.Handle("/thumbnail", ApiSessionRequired(getFileThumbnail)).Methods("GET")
|
||||
BaseRoutes.File.Handle("/link", ApiSessionRequired(getFileLink)).Methods("GET")
|
||||
BaseRoutes.File.Handle("/preview", ApiSessionRequired(getFilePreview)).Methods("GET")
|
||||
|
||||
}
|
||||
|
||||
@@ -161,6 +162,38 @@ func getFileLink(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(model.MapToJson(resp)))
|
||||
}
|
||||
|
||||
func getFilePreview(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.PreviewPath == "" {
|
||||
c.Err = model.NewLocAppError("getFilePreview", "api.file.get_file_preview.no_preview.app_error", nil, "file_id="+info.Id)
|
||||
c.Err.StatusCode = http.StatusBadRequest
|
||||
return
|
||||
}
|
||||
|
||||
if data, err := app.ReadFile(info.PreviewPath); 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)))
|
||||
|
||||
@@ -277,3 +277,55 @@ func TestGetFileLink(t *testing.T) {
|
||||
cleanupTestFile(result.Data.(*model.FileInfo))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFilePreview(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.GetFilePreview(fileId)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
if data == nil || len(data) == 0 {
|
||||
t.Fatal("should not be empty")
|
||||
}
|
||||
|
||||
_, resp = Client.GetFilePreview("junk")
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
_, resp = Client.GetFilePreview(model.NewId())
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
_, resp = Client.GetFilePreview(fileId)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
otherUser := th.CreateUser()
|
||||
Client.Login(otherUser.Email, otherUser.Password)
|
||||
_, resp = Client.GetFilePreview(fileId)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
_, resp = th.SystemAdminClient.GetFilePreview(fileId)
|
||||
CheckNoError(t, resp)
|
||||
}
|
||||
|
||||
@@ -1026,7 +1026,7 @@
|
||||
"translation": "The public link does not appear to be valid"
|
||||
},
|
||||
{
|
||||
"id": "api.file.get_file_preview.no_thumbnail.app_error",
|
||||
"id": "api.file.get_file_preview.no_preview.app_error",
|
||||
"translation": "File doesn't have a preview image"
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1055,6 +1055,17 @@ func (c *Client4) GetFileLink(fileId string) (string, *Response) {
|
||||
}
|
||||
}
|
||||
|
||||
// GetFilePreview gets the bytes for a file by id.
|
||||
func (c *Client4) GetFilePreview(fileId string) ([]byte, *Response) {
|
||||
if r, err := c.DoApiGet(c.GetFileRoute(fileId)+"/preview", ""); 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("GetFilePreview", "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