MM-31612 Optimize bulk import process by avoiding writing zip to disk (#17659)

Этот коммит содержится в:
Ian Whitlock
2021-06-09 11:11:46 -05:00
коммит произвёл GitHub
родитель 72c86448b9
Коммит fe94a20ce7
12 изменённых файлов: 281 добавлений и 90 удалений

Просмотреть файл

@@ -4,6 +4,7 @@
package app
import (
"archive/zip"
"context"
"io/ioutil"
"os"
@@ -4025,7 +4026,7 @@ func TestImportPostAndRepliesWithAttachments(t *testing.T) {
AssertFileIdsInPost(attachments, th, t)
})
t.Run("Reply with Attachments in Direct Pos", func(t *testing.T) {
t.Run("Reply with Attachments in Direct Post", func(t *testing.T) {
directImportData := LineImportWorkerData{
LineImportData{
DirectPost: &DirectPostImportData{
@@ -4177,3 +4178,161 @@ func TestImportDirectPostWithAttachments(t *testing.T) {
require.Len(t, attachments, 3)
})
}
func TestZippedImportPostAndRepliesWithAttachments(t *testing.T) {
th := Setup(t)
defer th.TearDown()
// Create a Team.
teamName := model.NewRandomTeamName()
th.App.importTeam(th.Context, &TeamImportData{
Name: &teamName,
DisplayName: ptrStr("Display Name"),
Type: ptrStr("O"),
}, false)
team, appErr := th.App.GetTeamByName(teamName)
require.Nil(t, appErr, "Failed to get team from database.")
// Create a Channel.
channelName := model.NewId()
th.App.importChannel(th.Context, &ChannelImportData{
Team: &teamName,
Name: &channelName,
DisplayName: ptrStr("Display Name"),
Type: ptrStr("O"),
}, false)
_, appErr = th.App.GetChannelByName(channelName, team.Id, false)
require.Nil(t, appErr, "Failed to get channel from database.")
// Create users
username2 := model.NewId()
th.App.importUser(&UserImportData{
Username: &username2,
Email: ptrStr(model.NewId() + "@example.com"),
}, false)
user2, appErr := th.App.GetUserByUsername(username2)
require.Nil(t, appErr, "Failed to get user3 from database.")
// Create direct post users.
username3 := model.NewId()
th.App.importUser(&UserImportData{
Username: &username3,
Email: ptrStr(model.NewId() + "@example.com"),
}, false)
user3, appErr := th.App.GetUserByUsername(username3)
require.Nil(t, appErr, "Failed to get user3 from database.")
username4 := model.NewId()
th.App.importUser(&UserImportData{
Username: &username4,
Email: ptrStr(model.NewId() + "@example.com"),
}, false)
user4, appErr := th.App.GetUserByUsername(username4)
require.Nil(t, appErr, "Failed to get user3 from database.")
// Post with attachments
time := model.GetMillis()
attachmentsPostTime := time
attachmentsReplyTime := time + 1
testsDir, _ := fileutils.FindDir("tests")
testImage := filepath.Join(testsDir, "test.png")
testZipFileName := filepath.Join(testsDir, "import_test.zip")
testZip, _ := os.Open(testZipFileName)
fi, err := testZip.Stat()
require.NoError(t, err, "failed to get file info")
testZipReader, err := zip.NewReader(testZip, fi.Size())
require.NoError(t, err, "failed to read test zip")
require.NotEmpty(t, testZipReader.File)
imageData := testZipReader.File[0]
require.NoError(t, err, "failed to copy test Image file into zip")
testMarkDown := filepath.Join(testsDir, "test-attachments.md")
data := LineImportWorkerData{
LineImportData{
Post: &PostImportData{
Team: &teamName,
Channel: &channelName,
User: &username3,
Message: ptrStr("Message with reply"),
CreateAt: &attachmentsPostTime,
Attachments: &[]AttachmentImportData{{Path: &testImage}, {Path: &testMarkDown}},
Replies: &[]ReplyImportData{{
User: &user4.Username,
Message: ptrStr("Message reply"),
CreateAt: &attachmentsReplyTime,
Attachments: &[]AttachmentImportData{{Path: &testImage, Data: imageData}},
}},
},
},
19,
}
t.Run("import with attachment", func(t *testing.T) {
errLine, err := th.App.importMultiplePostLines(th.Context, []LineImportWorkerData{data}, false)
require.Nil(t, err)
require.Equal(t, 0, errLine)
attachments := GetAttachments(user3.Id, th, t)
require.Len(t, attachments, 2)
assert.Contains(t, attachments[0].Path, team.Id)
assert.Contains(t, attachments[1].Path, team.Id)
AssertFileIdsInPost(attachments, th, t)
attachments = GetAttachments(user4.Id, th, t)
require.Len(t, attachments, 1)
assert.Contains(t, attachments[0].Path, team.Id)
AssertFileIdsInPost(attachments, th, t)
})
t.Run("import existing post with new attachment", func(t *testing.T) {
data.Post.Attachments = &[]AttachmentImportData{{Path: &testImage}}
errLine, err := th.App.importMultiplePostLines(th.Context, []LineImportWorkerData{data}, false)
require.Nil(t, err)
require.Equal(t, 0, errLine)
attachments := GetAttachments(user3.Id, th, t)
require.Len(t, attachments, 1)
assert.Contains(t, attachments[0].Path, team.Id)
AssertFileIdsInPost(attachments, th, t)
attachments = GetAttachments(user4.Id, th, t)
require.Len(t, attachments, 1)
assert.Contains(t, attachments[0].Path, team.Id)
AssertFileIdsInPost(attachments, th, t)
})
t.Run("Reply with Attachments in Direct Post", func(t *testing.T) {
directImportData := LineImportWorkerData{
LineImportData{
DirectPost: &DirectPostImportData{
ChannelMembers: &[]string{
user3.Username,
user2.Username,
},
User: &user3.Username,
Message: ptrStr("Message with Replies"),
CreateAt: ptrInt64(model.GetMillis()),
Replies: &[]ReplyImportData{{
User: &user2.Username,
Message: ptrStr("Message reply with attachment"),
CreateAt: ptrInt64(model.GetMillis()),
Attachments: &[]AttachmentImportData{{Path: &testImage}},
}},
},
},
7,
}
errLine, err := th.App.importMultipleDirectPostLines(th.Context, []LineImportWorkerData{directImportData}, false)
require.Nil(t, err, "Expected success.")
require.Equal(t, 0, errLine)
attachments := GetAttachments(user2.Id, th, t)
require.Len(t, attachments, 1)
assert.Contains(t, attachments[0].Path, "noteam")
AssertFileIdsInPost(attachments, th, t)
})
}