summaryrefslogtreecommitdiffstats
path: root/api4/file.go
diff options
context:
space:
mode:
authorHarrison Healey <harrisonmhealey@gmail.com>2018-02-20 10:41:00 -0500
committerGitHub <noreply@github.com>2018-02-20 10:41:00 -0500
commitbabd795d792e95f6e708af6ee8207ef6877e2b32 (patch)
tree32116afb6e1ea7b598c41eaddb318cea8fabd132 /api4/file.go
parentf85d9105925a82c5006654b99060506a0ff7d7af (diff)
downloadchat-babd795d792e95f6e708af6ee8207ef6877e2b32.tar.gz
chat-babd795d792e95f6e708af6ee8207ef6877e2b32.tar.bz2
chat-babd795d792e95f6e708af6ee8207ef6877e2b32.zip
MM-9556 Added ability to upload files without a multipart request (#8306)
* MM-9556 Added ability to upload files without a multipart request * MM-9556 Handled some unusual test behaviour
Diffstat (limited to 'api4/file.go')
-rw-r--r--api4/file.go69
1 files changed, 50 insertions, 19 deletions
diff --git a/api4/file.go b/api4/file.go
index acc4c78e5..0b0973b30 100644
--- a/api4/file.go
+++ b/api4/file.go
@@ -4,6 +4,7 @@
package api4
import (
+ "io"
"net/http"
"net/url"
"strconv"
@@ -65,32 +66,62 @@ func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
- if err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize); err != nil {
+ var resStruct *model.FileUploadResponse
+ var appErr *model.AppError
+
+ if err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize); err != nil && err != http.ErrNotMultipart {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
- }
+ } else if err == http.ErrNotMultipart {
+ defer r.Body.Close()
- m := r.MultipartForm
+ c.RequireChannelId()
+ c.RequireFilename()
- props := m.Value
- if len(props["channel_id"]) == 0 {
- c.SetInvalidParam("channel_id")
- return
- }
- channelId := props["channel_id"][0]
- if len(channelId) == 0 {
- c.SetInvalidParam("channel_id")
- return
- }
+ if c.Err != nil {
+ return
+ }
- if !c.App.SessionHasPermissionToChannel(c.Session, channelId, model.PERMISSION_UPLOAD_FILE) {
- c.SetPermissionError(model.PERMISSION_UPLOAD_FILE)
- return
+ channelId := c.Params.ChannelId
+ filename := c.Params.Filename
+
+ if !c.App.SessionHasPermissionToChannel(c.Session, channelId, model.PERMISSION_UPLOAD_FILE) {
+ c.SetPermissionError(model.PERMISSION_UPLOAD_FILE)
+ return
+ }
+
+ resStruct, appErr = c.App.UploadFiles(
+ FILE_TEAM_ID,
+ channelId,
+ c.Session.UserId,
+ []io.ReadCloser{r.Body},
+ []string{filename},
+ []string{},
+ )
+ } else {
+ m := r.MultipartForm
+
+ props := m.Value
+ if len(props["channel_id"]) == 0 {
+ c.SetInvalidParam("channel_id")
+ return
+ }
+ channelId := props["channel_id"][0]
+ if len(channelId) == 0 {
+ c.SetInvalidParam("channel_id")
+ return
+ }
+
+ if !c.App.SessionHasPermissionToChannel(c.Session, channelId, model.PERMISSION_UPLOAD_FILE) {
+ c.SetPermissionError(model.PERMISSION_UPLOAD_FILE)
+ return
+ }
+
+ resStruct, appErr = c.App.UploadMultipartFiles(FILE_TEAM_ID, channelId, c.Session.UserId, m.File["files"], m.Value["client_ids"])
}
- resStruct, err := c.App.UploadFiles(FILE_TEAM_ID, channelId, c.Session.UserId, m.File["files"], m.Value["client_ids"])
- if err != nil {
- c.Err = err
+ if appErr != nil {
+ c.Err = appErr
return
}