diff --git a/app/slackimport.go b/app/slackimport.go index 49764853d8d..640da02ae1b 100644 --- a/app/slackimport.go +++ b/app/slackimport.go @@ -62,6 +62,7 @@ type SlackPost struct { Comment *SlackComment `json:"comment"` Upload bool `json:"upload"` File *SlackFile `json:"file"` + Files []*SlackFile `json:"files"` Attachments []*model.SlackAttachment `json:"attachments"` } @@ -250,8 +251,16 @@ func (a *App) SlackAddPosts(teamId string, channel *model.Channel, posts []Slack CreateAt: SlackConvertTimeStamp(sPost.TimeStamp), } if sPost.Upload { - if fileInfo, ok := a.SlackUploadFile(sPost, uploads, teamId, newPost.ChannelId, newPost.UserId); ok { - newPost.FileIds = append(newPost.FileIds, fileInfo.Id) + if sPost.File != nil { + if fileInfo, ok := a.SlackUploadFile(sPost.File, uploads, teamId, newPost.ChannelId, newPost.UserId, sPost.TimeStamp); ok { + newPost.FileIds = append(newPost.FileIds, fileInfo.Id) + } + } else if sPost.Files != nil { + for _, file := range sPost.Files { + if fileInfo, ok := a.SlackUploadFile(file, uploads, teamId, newPost.ChannelId, newPost.UserId, sPost.TimeStamp); ok { + newPost.FileIds = append(newPost.FileIds, fileInfo.Id) + } + } } } // If post in thread @@ -418,27 +427,27 @@ func (a *App) SlackAddPosts(teamId string, channel *model.Channel, posts []Slack } } -func (a *App) SlackUploadFile(sPost SlackPost, uploads map[string]*zip.File, teamId string, channelId string, userId string) (*model.FileInfo, bool) { - if sPost.File == nil { +func (a *App) SlackUploadFile(slackPostFile *SlackFile, uploads map[string]*zip.File, teamId string, channelId string, userId string, slackTimestamp string) (*model.FileInfo, bool) { + if slackPostFile == nil { mlog.Warn("Slack Import: Unable to attach the file to the post as the latter has no file section present in Slack export.") return nil, false } - file, ok := uploads[sPost.File.Id] + file, ok := uploads[slackPostFile.Id] if !ok { - mlog.Warn(fmt.Sprintf("Slack Import: Unable to import file %v as the file is missing from the Slack export zip file.", sPost.File.Id)) + mlog.Warn(fmt.Sprintf("Slack Import: Unable to import file %v as the file is missing from the Slack export zip file.", slackPostFile.Id)) return nil, false } openFile, err := file.Open() if err != nil { - mlog.Warn(fmt.Sprintf("Slack Import: Unable to open the file %v from the Slack export: %v.", sPost.File.Id, err.Error())) + mlog.Warn(fmt.Sprintf("Slack Import: Unable to open the file %v from the Slack export: %v.", slackPostFile.Id, err.Error())) return nil, false } defer openFile.Close() - timestamp := utils.TimeFromMillis(SlackConvertTimeStamp(sPost.TimeStamp)) + timestamp := utils.TimeFromMillis(SlackConvertTimeStamp(slackTimestamp)) uploadedFile, err := a.OldImportFile(timestamp, openFile, teamId, channelId, userId, filepath.Base(file.Name)) if err != nil { - mlog.Warn(fmt.Sprintf("Slack Import: An error occurred when uploading file %v: %v.", sPost.File.Id, err.Error())) + mlog.Warn(fmt.Sprintf("Slack Import: An error occurred when uploading file %v: %v.", slackPostFile.Id, err.Error())) return nil, false } diff --git a/app/slackimport_test.go b/app/slackimport_test.go index 66662bfa276..02096ab6b2e 100644 --- a/app/slackimport_test.go +++ b/app/slackimport_test.go @@ -168,7 +168,17 @@ func TestSlackParsePosts(t *testing.T) { posts, err := SlackParsePosts(file) require.NoError(t, err) - assert.Equal(t, 8, len(posts)) + assert.Equal(t, 9, len(posts)) +} + +func TestSlackParseMultipleAttachments(t *testing.T) { + file, err := os.Open("tests/slack-import-test-posts.json") + require.NoError(t, err) + defer file.Close() + + posts, err := SlackParsePosts(file) + require.NoError(t, err) + assert.Equal(t, 2, len(posts[8].Files)) } func TestSlackSanitiseChannelProperties(t *testing.T) { diff --git a/tests/slack-import-test-posts.json b/tests/slack-import-test-posts.json index ca0d0a78c52..07b8c65d5ba 100644 --- a/tests/slack-import-test-posts.json +++ b/tests/slack-import-test-posts.json @@ -176,5 +176,113 @@ "username": "<@U07Q4MHCP|lindy>", "bot_id": null, "ts": "1469782077.000010" + }, + { + "type": "message", + "text": "multiple_attachments", + "files": [ + { + "id": "FL9GSN60L", + "created": 1562758810, + "timestamp": 1562758810, + "name": "Screenshot from 2019-07-09 17-08-24.png", + "title": "Screenshot from 2019-07-09 17-08-24.png", + "mimetype": "image\/png", + "filetype": "png", + "pretty_type": "PNG", + "user": "UKW2XKH7E", + "editable": false, + "size": 79513, + "mode": "hosted", + "is_external": false, + "external_type": "", + "is_public": true, + "public_url_shared": false, + "display_as_bot": false, + "username": "", + "url_private": "https:\/\/files.slack.com\/files-pri\/TKK4L637U-FL9GSN60L\/screenshot_from_2019-07-09_17-08-24.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "url_private_download": "https:\/\/files.slack.com\/files-pri\/TKK4L637U-FL9GSN60L\/download\/screenshot_from_2019-07-09_17-08-24.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_64": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_64.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_80": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_80.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_360": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_360.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_360_w": 197, + "thumb_360_h": 360, + "thumb_480": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_480.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_480_w": 263, + "thumb_480_h": 480, + "thumb_160": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_160.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_720": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_720.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_720_w": 395, + "thumb_720_h": 720, + "thumb_800": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_800.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_800_w": 439, + "thumb_800_h": 800, + "thumb_960": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_960.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_960_w": 526, + "thumb_960_h": 960, + "thumb_1024": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_1024.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_1024_w": 561, + "thumb_1024_h": 1024, + "image_exif_rotation": 1, + "original_w": 632, + "original_h": 1153, + "permalink": "https:\/\/svenstestteam.slack.com\/files\/UKW2XKH7E\/FL9GSN60L\/screenshot_from_2019-07-09_17-08-24.png", + "permalink_public": "https:\/\/slack-files.com\/TKK4L637U-FL9GSN60L-19fdfd655a", + "has_rich_preview": false + }, + { + "id": "FL3LBGRT3", + "created": 1562758810, + "timestamp": 1562758810, + "name": "vscode.jpg", + "title": "vscode.jpg", + "mimetype": "image\/jpeg", + "filetype": "jpg", + "pretty_type": "JPEG", + "user": "UKW2XKH7E", + "editable": false, + "size": 549439, + "mode": "hosted", + "is_external": false, + "external_type": "", + "is_public": true, + "public_url_shared": false, + "display_as_bot": false, + "username": "", + "url_private": "https:\/\/files.slack.com\/files-pri\/TKK4L637U-FL3LBGRT3\/vscode.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "url_private_download": "https:\/\/files.slack.com\/files-pri\/TKK4L637U-FL3LBGRT3\/download\/vscode.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_64": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_64.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_80": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_80.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_360": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_360.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_360_w": 360, + "thumb_360_h": 203, + "thumb_480": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_480.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_480_w": 480, + "thumb_480_h": 270, + "thumb_160": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_160.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_720": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_720.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_720_w": 720, + "thumb_720_h": 405, + "thumb_800": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_800.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_800_w": 800, + "thumb_800_h": 450, + "thumb_960": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_960.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_960_w": 960, + "thumb_960_h": 540, + "thumb_1024": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_1024.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673", + "thumb_1024_w": 1024, + "thumb_1024_h": 576, + "image_exif_rotation": 1, + "original_w": 1920, + "original_h": 1080, + "permalink": "https:\/\/svenstestteam.slack.com\/files\/UKW2XKH7E\/FL3LBGRT3\/vscode.jpg", + "permalink_public": "https:\/\/slack-files.com\/TKK4L637U-FL3LBGRT3-ffd5da7911", + "has_rich_preview": false + } + ], + "upload": true, + "user": "UKW2XKH7E", + "ts": "1562758814.000100", + "client_msg_id": "bf2a1192-6b57-4dd3-90f9-8e833fcad758" } -] \ No newline at end of file +]