From 2bf64b54c236945727a2d93afb57b1665d1daa7a Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Wed, 12 Jul 2017 14:13:25 +0100 Subject: PLT-7025: Fix Slack Import API. (#6905) --- api4/team.go | 8 ++++---- api4/team_test.go | 8 +++++++- model/client4.go | 8 +++----- webapp/components/team_import_tab.jsx | 8 ++++---- webapp/utils/utils.jsx | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/api4/team.go b/api4/team.go index 1cbd89481..98a672d93 100644 --- a/api4/team.go +++ b/api4/team.go @@ -5,7 +5,7 @@ package api4 import ( "bytes" - "io" + "encoding/base64" "net/http" "strconv" @@ -657,12 +657,12 @@ func importTeam(c *Context, w http.ResponseWriter, r *http.Request) { } } - w.Header().Set("Content-Disposition", "attachment; filename=MattermostImportLog.txt") - w.Header().Set("Content-Type", "application/octet-stream") + data := map[string]string{} + data["results"] = base64.StdEncoding.EncodeToString([]byte(log.Bytes())) if c.Err != nil { w.WriteHeader(c.Err.StatusCode) } - io.Copy(w, bytes.NewReader(log.Bytes())) + w.Write([]byte(model.MapToJson(data))) } func inviteUsersToTeam(c *Context, w http.ResponseWriter, r *http.Request) { diff --git a/api4/team_test.go b/api4/team_test.go index b517f67fe..440b2feb2 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -15,6 +15,7 @@ import ( "github.com/mattermost/platform/app" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" + "encoding/base64" ) func TestCreateTeam(t *testing.T) { @@ -1345,7 +1346,12 @@ func TestImportTeam(t *testing.T) { fileResp, resp := th.SystemAdminClient.ImportTeam(data, binary.Size(data), "slack", "Fake_Team_Import.zip", th.BasicTeam.Id) CheckNoError(t, resp) - fileReturned := fmt.Sprintf("%s", fileResp) + fileData, err := base64.StdEncoding.DecodeString(fileResp["results"]) + if err != nil { + t.Fatal("failed to decode base64 results data") + } + + fileReturned := fmt.Sprintf("%s", fileData) if !strings.Contains(fileReturned, "darth.vader@stardeath.com") { t.Log(fileReturned) t.Fatal("failed to report the user was imported") diff --git a/model/client4.go b/model/client4.go index b10ced27f..9ecdf9a62 100644 --- a/model/client4.go +++ b/model/client4.go @@ -352,7 +352,7 @@ func (c *Client4) DoEmojiUploadFile(url string, data []byte, contentType string) } } -func (c *Client4) DoUploadImportTeam(url string, data []byte, contentType string) ([]byte, *Response) { +func (c *Client4) DoUploadImportTeam(url string, data []byte, contentType string) (map[string]string, *Response) { rq, _ := http.NewRequest("POST", c.ApiUrl+url, bytes.NewReader(data)) rq.Header.Set("Content-Type", contentType) rq.Close = true @@ -365,11 +365,9 @@ func (c *Client4) DoUploadImportTeam(url string, data []byte, contentType string return nil, &Response{Error: NewAppError(url, "model.client.connecting.app_error", nil, err.Error(), 0)} } else if rp.StatusCode >= 300 { return nil, &Response{StatusCode: rp.StatusCode, Error: AppErrorFromJson(rp.Body)} - } else if data, err := ioutil.ReadAll(rp.Body); err != nil { - return nil, &Response{StatusCode: rp.StatusCode, Error: NewAppError("UploadImportTeam", "model.client.read_file.app_error", nil, err.Error(), rp.StatusCode)} } else { defer closeBody(rp) - return data, BuildResponse(rp) + return MapFromJson(rp.Body), BuildResponse(rp) } } @@ -1210,7 +1208,7 @@ func (c *Client4) GetTeamUnread(teamId, userId string) (*TeamUnread, *Response) } // ImportTeam will import an exported team from other app into a existing team. -func (c *Client4) ImportTeam(data []byte, filesize int, importFrom, filename, teamId string) ([]byte, *Response) { +func (c *Client4) ImportTeam(data []byte, filesize int, importFrom, filename, teamId string) (map[string]string, *Response) { body := &bytes.Buffer{} writer := multipart.NewWriter(body) diff --git a/webapp/components/team_import_tab.jsx b/webapp/components/team_import_tab.jsx index a17442dc9..b310cdc12 100644 --- a/webapp/components/team_import_tab.jsx +++ b/webapp/components/team_import_tab.jsx @@ -29,12 +29,12 @@ class TeamImportTab extends React.Component { }; } - onImportFailure(e, err, res) { - this.setState({status: 'fail', link: 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(res.text)}); + onImportFailure() { + this.setState({status: 'fail'}); } - onImportSuccess(data, res) { - this.setState({status: 'done', link: 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(res.text)}); + onImportSuccess(data) { + this.setState({status: 'done', link: 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(atob(data.results))}); } doImportSlack(file) { diff --git a/webapp/utils/utils.jsx b/webapp/utils/utils.jsx index 529ea5666..83e538605 100644 --- a/webapp/utils/utils.jsx +++ b/webapp/utils/utils.jsx @@ -1107,7 +1107,7 @@ export function isDirectChannelForUser(otherUserId, channel) { } export function importSlack(file, success, error) { - Client4.importTeam(file, 'slack').then(success).catch(error); + Client4.importTeam(TeamStore.getCurrent().id, file, 'slack').then(success).catch(error); } export function windowWidth() { -- cgit v1.2.3-1-g7c22