summaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/context.go21
-rw-r--r--api/file.go76
-rw-r--r--api/file_test.go9
-rw-r--r--api/templates/error.html1
4 files changed, 96 insertions, 11 deletions
diff --git a/api/context.go b/api/context.go
index 8babf85f2..aaf304e2c 100644
--- a/api/context.go
+++ b/api/context.go
@@ -4,14 +4,15 @@
package api
import (
- l4g "code.google.com/p/log4go"
- "github.com/mattermost/platform/model"
- "github.com/mattermost/platform/store"
- "github.com/mattermost/platform/utils"
"net"
"net/http"
"net/url"
"strings"
+
+ l4g "code.google.com/p/log4go"
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/store"
+ "github.com/mattermost/platform/utils"
)
var sessionCache *utils.Cache = utils.NewLru(model.SESSION_CACHE_SIZE)
@@ -431,10 +432,22 @@ func IsPrivateIpAddress(ipAddress string) bool {
}
func RenderWebError(err *model.AppError, w http.ResponseWriter, r *http.Request) {
+
+ protocol := "http"
+ if utils.Cfg.ServiceSettings.UseSSL {
+ forwardProto := r.Header.Get(model.HEADER_FORWARDED_PROTO)
+ if forwardProto != "http" {
+ protocol = "https"
+ }
+ }
+
+ SiteURL := protocol + "://" + r.Host
+
m := make(map[string]string)
m["Message"] = err.Message
m["Details"] = err.DetailedError
m["SiteName"] = utils.Cfg.ServiceSettings.SiteName
+ m["SiteURL"] = SiteURL
w.WriteHeader(err.StatusCode)
ServerTemplates.ExecuteTemplate(w, "error.html", m)
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) {
diff --git a/api/file_test.go b/api/file_test.go
index 566fd69d0..eb1fcf2ce 100644
--- a/api/file_test.go
+++ b/api/file_test.go
@@ -201,6 +201,15 @@ func TestGetFile(t *testing.T) {
t.Fatal(downErr)
}
+ if resp, downErr := Client.GetFileInfo(filenames[0]); downErr != nil {
+ t.Fatal(downErr)
+ } else {
+ m := resp.Data.(map[string]string)
+ if len(m["size"]) == 0 {
+ t.Fail()
+ }
+ }
+
team2 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
team2 = Client.Must(Client.CreateTeam(team2)).Data.(*model.Team)
diff --git a/api/templates/error.html b/api/templates/error.html
index f38bb81a1..3474c9e1e 100644
--- a/api/templates/error.html
+++ b/api/templates/error.html
@@ -14,6 +14,7 @@
<div class="error__icon"><i class="fa fa-exclamation-triangle"></i></div>
<h2>{{ .SiteName }} needs your help:</h2>
<p>{{.Message}}</p>
+ <a href="{{.SiteURL}}">Go back to team site</a>
</div>
</div>
</body>