diff options
author | Saturnino Abril <saturnino.abril@gmail.com> | 2017-03-01 10:18:36 +0900 |
---|---|---|
committer | enahum <nahumhbl@gmail.com> | 2017-02-28 22:18:36 -0300 |
commit | 28c218db3bbdcc0776be1be91ff4acbd0586f590 (patch) | |
tree | 10259512af822b378a93e105cb2608e039324001 /api4/file.go | |
parent | 2a621f7470476b0f10d6241a13fdcfd838748edd (diff) | |
download | chat-28c218db3bbdcc0776be1be91ff4acbd0586f590.tar.gz chat-28c218db3bbdcc0776be1be91ff4acbd0586f590.tar.bz2 chat-28c218db3bbdcc0776be1be91ff4acbd0586f590.zip |
Implementation endpoint of APIv4: GET /files/{file_id}/thumbnail (#5553)
* APIv4: GET /files/{file_id}/thumbnail
* added delay time
Diffstat (limited to 'api4/file.go')
-rw-r--r-- | api4/file.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/api4/file.go b/api4/file.go index b486fc220..924f7e416 100644 --- a/api4/file.go +++ b/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))) |