Skip to content

Commit

Permalink
APIv4: GET /files/{file_id}/preview
Browse files Browse the repository at this point in the history
patch 1
  • Loading branch information
saturninoabril committed Mar 3, 2017
1 parent 3a91d4e commit 8b36afd
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
33 changes: 33 additions & 0 deletions api4/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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")
BaseRoutes.File.Handle("/preview", ApiSessionRequired(getFilePreview)).Methods("GET")

}

Expand Down Expand Up @@ -125,6 +126,38 @@ func getFileThumbnail(c *Context, w http.ResponseWriter, r *http.Request) {
}
}

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)))
Expand Down
52 changes: 52 additions & 0 deletions api4/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,55 @@ func TestGetFileThumbnail(t *testing.T) {
_, resp = th.SystemAdminClient.GetFileThumbnail(fileId)
CheckNoError(t, resp)
}

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)
}
2 changes: 1 addition & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,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"
},
{
Expand Down
11 changes: 11 additions & 0 deletions model/client4.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,17 @@ func (c *Client4) GetFileThumbnail(fileId string) ([]byte, *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 {
Expand Down

0 comments on commit 8b36afd

Please sign in to comment.