[MM-13033] Continue bulk import on large image error (#10780)

* [MM-13033] Continue bulk import on large image error

* add tests, review comments

* Update app/import_functions.go

Co-Authored-By: liusy182 <liusy182@hotmail.com>
Этот коммит содержится в:
Siyuan Liu
2019-05-08 13:29:42 -07:00
коммит произвёл Jesús Espino
родитель 66cb36f5dc
Коммит 27566f6a06
3 изменённых файлов: 43 добавлений и 9 удалений

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

@@ -6,14 +6,25 @@ package app
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
)
func stopOnError(err LineImportWorkerError) bool {
if err.Error.Id == "api.file.upload_file.large_image.app_error" {
mlog.Warn(fmt.Sprintf("Large image import error: %s", err.Error.Error()))
return false
}
return true
}
func (a *App) bulkImportWorker(dryRun bool, wg *sync.WaitGroup, lines <-chan LineImportWorkerData, errors chan<- LineImportWorkerError) {
for line := range lines {
if err := a.ImportLine(line.LineImportData, dryRun); err != nil {
@@ -65,7 +76,9 @@ func (a *App) BulkImport(fileReader io.Reader, dryRun bool, workers int) (*model
// Check no errors occurred while waiting for the queue to empty.
if len(errorsChan) != 0 {
err := <-errorsChan
return err.Error, err.LineNumber
if stopOnError(err) {
return err.Error, err.LineNumber
}
}
}
@@ -81,9 +94,11 @@ func (a *App) BulkImport(fileReader io.Reader, dryRun bool, workers int) (*model
select {
case linesChan <- LineImportWorkerData{line, lineNumber}:
case err := <-errorsChan:
close(linesChan)
wg.Wait()
return err.Error, err.LineNumber
if stopOnError(err) {
close(linesChan)
wg.Wait()
return err.Error, err.LineNumber
}
}
}
@@ -94,7 +109,9 @@ func (a *App) BulkImport(fileReader io.Reader, dryRun bool, workers int) (*model
// Check no errors occurred while waiting for the queue to empty.
if len(errorsChan) != 0 {
err := <-errorsChan
return err.Error, err.LineNumber
if stopOnError(err) {
return err.Error, err.LineNumber
}
}
if err := scanner.Err(); err != nil {

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

@@ -909,7 +909,6 @@ func (a *App) ImportReply(data *ReplyImportData, post *model.Post, teamId string
}
func (a *App) ImportAttachment(data *AttachmentImportData, post *model.Post, teamId string, dryRun bool) (*model.FileInfo, *model.AppError) {
fileUploadError := model.NewAppError("BulkImport", "app.import.attachment.file_upload.error", map[string]interface{}{"FilePath": *data.Path}, "", http.StatusBadRequest)
file, err := os.Open(*data.Path)
if err != nil {
return nil, model.NewAppError("BulkImport", "app.import.attachment.bad_file.error", map[string]interface{}{"FilePath": *data.Path}, "", http.StatusBadRequest)
@@ -922,8 +921,8 @@ func (a *App) ImportAttachment(data *AttachmentImportData, post *model.Post, tea
fileInfo, err := a.DoUploadFile(timestamp, teamId, post.ChannelId, post.UserId, file.Name(), buf.Bytes())
if err != nil {
fmt.Print(err)
return nil, fileUploadError
mlog.Error(fmt.Sprintf("Failed to upload file: %s", err.Error()))
return nil, err
}
a.HandleImages([]string{fileInfo.PreviewPath}, []string{fileInfo.ThumbnailPath}, [][]byte{buf.Bytes()})
@@ -931,7 +930,7 @@ func (a *App) ImportAttachment(data *AttachmentImportData, post *model.Post, tea
mlog.Info(fmt.Sprintf("uploading file with name %s", file.Name()))
return fileInfo, nil
}
return nil, fileUploadError
return nil, model.NewAppError("BulkImport", "app.import.attachment.file_upload.error", map[string]interface{}{"FilePath": *data.Path}, "", http.StatusBadRequest)
}
func (a *App) ImportPost(data *PostImportData, dryRun bool) *model.AppError {

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

@@ -4,6 +4,7 @@
package app
import (
"net/http"
"path/filepath"
"runtime/debug"
"strings"
@@ -159,6 +160,23 @@ func TestImportImportLine(t *testing.T) {
}
}
func TestStopOnError(t *testing.T) {
assert.True(t, stopOnError(LineImportWorkerError{
model.NewAppError("test", "app.import.attachment.bad_file.error", nil, "", http.StatusBadRequest),
1,
}))
assert.True(t, stopOnError(LineImportWorkerError{
model.NewAppError("test", "app.import.attachment.file_upload.error", nil, "", http.StatusBadRequest),
1,
}))
assert.False(t, stopOnError(LineImportWorkerError{
model.NewAppError("test", "api.file.upload_file.large_image.app_error", nil, "", http.StatusBadRequest),
1,
}))
}
func TestImportBulkImport(t *testing.T) {
th := Setup(t)
defer th.TearDown()