From 64b179ab0e6a66c0f8edb72ab24ef28bbc2f9969 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Mon, 24 Aug 2015 15:03:52 -0700 Subject: Fixes mm-1912 move get file info into its own web service call --- api/file.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 7 deletions(-) (limited to 'api/file.go') diff --git a/api/file.go b/api/file.go index 558f9357e..50482a057 100644 --- a/api/file.go +++ b/api/file.go @@ -30,12 +30,15 @@ import ( "time" ) +var fileInfoCache *utils.Cache = utils.NewLru(1000) + func InitFile(r *mux.Router) { l4g.Debug("Initializing file api routes") sr := r.PathPrefix("/files").Subrouter() sr.Handle("/upload", ApiUserRequired(uploadFile)).Methods("POST") - sr.Handle("/get/{channel_id:[A-Za-z0-9]+}/{user_id:[A-Za-z0-9]+}/{filename:([A-Za-z0-9]+/)?.+(\\.[A-Za-z0-9]{3,})?}", ApiAppHandler(getFile)).Methods("GET", "HEAD") + sr.Handle("/get/{channel_id:[A-Za-z0-9]+}/{user_id:[A-Za-z0-9]+}/{filename:([A-Za-z0-9]+/)?.+(\\.[A-Za-z0-9]{3,})?}", ApiAppHandler(getFile)).Methods("GET") + sr.Handle("/get_info/{channel_id:[A-Za-z0-9]+}/{user_id:[A-Za-z0-9]+}/{filename:([A-Za-z0-9]+/)?.+(\\.[A-Za-z0-9]{3,})?}", ApiAppHandler(getFileInfo)).Methods("GET") sr.Handle("/get_public_link", ApiUserRequired(getPublicLink)).Methods("POST") } @@ -213,9 +216,72 @@ type ImageGetResult struct { ImageData []byte } +func getFileInfo(c *Context, w http.ResponseWriter, r *http.Request) { + if !utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage { + c.Err = model.NewAppError("getFileInfo", "Unable to get file info. Amazon S3 not configured and local server storage turned off. ", "") + c.Err.StatusCode = http.StatusNotImplemented + return + } + + params := mux.Vars(r) + + channelId := params["channel_id"] + if len(channelId) != 26 { + c.SetInvalidParam("getFileInfo", "channel_id") + return + } + + userId := params["user_id"] + if len(userId) != 26 { + c.SetInvalidParam("getFileInfo", "user_id") + return + } + + filename := params["filename"] + if len(filename) == 0 { + c.SetInvalidParam("getFileInfo", "filename") + return + } + + cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, channelId, c.Session.UserId) + + path := "teams/" + c.Session.TeamId + "/channels/" + channelId + "/users/" + userId + "/" + filename + size := "" + + if s, ok := fileInfoCache.Get(path); ok { + size = s.(string) + } else { + + fileData := make(chan []byte) + asyncGetFile(path, fileData) + + f := <-fileData + + if f == nil { + c.Err = model.NewAppError("getFileInfo", "Could not find file.", "path="+path) + c.Err.StatusCode = http.StatusNotFound + return + } + + size = strconv.Itoa(len(f)) + fileInfoCache.Add(path, size) + } + + if !c.HasPermissionsToChannel(cchan, "getFileInfo") { + return + } + + w.Header().Set("Cache-Control", "max-age=2592000, public") + + result := make(map[string]string) + result["filename"] = filename + result["size"] = size + w.Write([]byte(model.MapToJson(result))) +} + func getFile(c *Context, w http.ResponseWriter, r *http.Request) { if !utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage { - c.Err = model.NewAppError("getFile", "Unable to upload file. Amazon S3 not configured and local server storage turned off. ", "") + c.Err = model.NewAppError("getFile", "Unable to get file. Amazon S3 not configured and local server storage turned off. ", "") c.Err.StatusCode = http.StatusNotImplemented return } @@ -281,11 +347,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) { } w.Header().Set("Cache-Control", "max-age=2592000, public") - w.Header().Set("Content-Length", strconv.Itoa(len(f))) - - if r.Method != "HEAD" { - w.Write(f) - } + w.Write(f) } func asyncGetFile(path string, fileData chan []byte) { -- cgit v1.2.3-1-g7c22