summaryrefslogtreecommitdiffstats
path: root/api/file.go
diff options
context:
space:
mode:
author=Corey Hulen <corey@hulen.com>2015-08-24 15:03:52 -0700
committer=Corey Hulen <corey@hulen.com>2015-08-24 15:03:52 -0700
commit64b179ab0e6a66c0f8edb72ab24ef28bbc2f9969 (patch)
tree3094d69abb3e9bd32a14587c8d787c9ad8f0b688 /api/file.go
parent930488f002c819efed7e3afc982b73d1c06a9bbe (diff)
downloadchat-64b179ab0e6a66c0f8edb72ab24ef28bbc2f9969.tar.gz
chat-64b179ab0e6a66c0f8edb72ab24ef28bbc2f9969.tar.bz2
chat-64b179ab0e6a66c0f8edb72ab24ef28bbc2f9969.zip
Fixes mm-1912 move get file info into its own web service call
Diffstat (limited to 'api/file.go')
-rw-r--r--api/file.go76
1 files changed, 69 insertions, 7 deletions
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) {