Fixes mm-1912 move get file info into its own web service call
Этот коммит содержится в:
76
api/file.go
76
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) {
|
||||
|
||||
Ссылка в новой задаче
Block a user