Update export.go (fix errcheck issue) (#28971)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
902888efd9
Коммит
812034f5eb
@@ -83,7 +83,6 @@ issues:
|
|||||||
channels/app/bot_test.go|\
|
channels/app/bot_test.go|\
|
||||||
channels/app/brand.go|\
|
channels/app/brand.go|\
|
||||||
channels/app/config_test.go|\
|
channels/app/config_test.go|\
|
||||||
channels/app/export.go|\
|
|
||||||
channels/app/file.go|\
|
channels/app/file.go|\
|
||||||
channels/app/file_bench_test.go|\
|
channels/app/file_bench_test.go|\
|
||||||
channels/app/file_test.go|\
|
channels/app/file_test.go|\
|
||||||
|
|||||||
@@ -113,7 +113,11 @@ func (a *App) BulkExport(ctx request.CTX, writer io.Writer, outPath string, job
|
|||||||
if opts.CreateArchive {
|
if opts.CreateArchive {
|
||||||
var err error
|
var err error
|
||||||
zipWr = zip.NewWriter(writer)
|
zipWr = zip.NewWriter(writer)
|
||||||
defer zipWr.Close()
|
defer func() {
|
||||||
|
if err = zipWr.Close(); err != nil {
|
||||||
|
ctx.Logger().Error("Error closing zip writer", mlog.Err(err))
|
||||||
|
}
|
||||||
|
}()
|
||||||
writer, err = zipWr.Create("import.jsonl")
|
writer, err = zipWr.Create("import.jsonl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.NewAppError("BulkExport", "app.export.zip_create.error",
|
return model.NewAppError("BulkExport", "app.export.zip_create.error",
|
||||||
@@ -202,7 +206,7 @@ func (a *App) BulkExport(ctx request.CTX, writer io.Writer, outPath string, job
|
|||||||
emojisLen := len(emojiPaths)
|
emojisLen := len(emojiPaths)
|
||||||
ctx.Logger().Info("Bulk export: exporting custom emojis")
|
ctx.Logger().Info("Bulk export: exporting custom emojis")
|
||||||
for _, emojiPath := range emojiPaths {
|
for _, emojiPath := range emojiPaths {
|
||||||
if err := a.exportFile(outPath, emojiPath, zipWr); err != nil {
|
if err := a.exportFile(ctx, outPath, emojiPath, zipWr); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
totalExportedEmojis++
|
totalExportedEmojis++
|
||||||
@@ -217,7 +221,7 @@ func (a *App) BulkExport(ctx request.CTX, writer io.Writer, outPath string, job
|
|||||||
if opts.IncludeProfilePictures {
|
if opts.IncludeProfilePictures {
|
||||||
ctx.Logger().Info("Bulk export: exporting profile pictures")
|
ctx.Logger().Info("Bulk export: exporting profile pictures")
|
||||||
for _, profilePicture := range profilePictures {
|
for _, profilePicture := range profilePictures {
|
||||||
if err := a.exportFile(outPath, profilePicture, zipWr); err != nil {
|
if err := a.exportFile(ctx, outPath, profilePicture, zipWr); err != nil {
|
||||||
ctx.Logger().Warn("Unable to export profile picture", mlog.String("profile_picture", profilePicture), mlog.Err(err))
|
ctx.Logger().Warn("Unable to export profile picture", mlog.String("profile_picture", profilePicture), mlog.Err(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,7 +235,7 @@ func (a *App) exportAttachments(ctx request.CTX, attachments []imports.Attachmen
|
|||||||
totalExportedFiles := 0
|
totalExportedFiles := 0
|
||||||
attachmentsLen := len(attachments)
|
attachmentsLen := len(attachments)
|
||||||
for _, attachment := range attachments {
|
for _, attachment := range attachments {
|
||||||
if err := a.exportFile(outPath, *attachment.Path, zipWr); err != nil {
|
if err := a.exportFile(ctx, outPath, *attachment.Path, zipWr); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
totalExportedFiles++
|
totalExportedFiles++
|
||||||
@@ -847,12 +851,13 @@ func (a *App) buildPostAttachments(postID string) ([]imports.AttachmentImportDat
|
|||||||
return attachments, nil
|
return attachments, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) exportCustomEmoji(c request.CTX, job *model.Job, writer io.Writer, outPath, exportDir string, exportFiles bool) ([]string, *model.AppError) {
|
func (a *App) exportCustomEmoji(rctx request.CTX, job *model.Job, writer io.Writer, outPath, exportDir string, exportFiles bool) ([]string, *model.AppError) {
|
||||||
var emojiPaths []string
|
var emojiPaths []string
|
||||||
pageNumber := 0
|
pageNumber := 0
|
||||||
cnt := 0
|
cnt := 0
|
||||||
for {
|
for {
|
||||||
customEmojiList, err := a.GetEmojiList(c, pageNumber, 100, model.EmojiSortByName)
|
customEmojiList, err := a.GetEmojiList(rctx, pageNumber, 100, model.EmojiSortByName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -861,7 +866,7 @@ func (a *App) exportCustomEmoji(c request.CTX, job *model.Job, writer io.Writer,
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
cnt += len(customEmojiList)
|
cnt += len(customEmojiList)
|
||||||
updateJobProgress(c.Logger(), a.Srv().Store(), job, "emojis_exported", cnt)
|
updateJobProgress(rctx.Logger(), a.Srv().Store(), job, "emojis_exported", cnt)
|
||||||
|
|
||||||
pageNumber++
|
pageNumber++
|
||||||
|
|
||||||
@@ -869,7 +874,8 @@ func (a *App) exportCustomEmoji(c request.CTX, job *model.Job, writer io.Writer,
|
|||||||
pathToDir := filepath.Join(outPath, exportDir)
|
pathToDir := filepath.Join(outPath, exportDir)
|
||||||
if exportFiles {
|
if exportFiles {
|
||||||
if _, err := os.Stat(pathToDir); os.IsNotExist(err) {
|
if _, err := os.Stat(pathToDir); os.IsNotExist(err) {
|
||||||
os.Mkdir(pathToDir, os.ModePerm)
|
if err := os.Mkdir(pathToDir, os.ModePerm); err != nil {
|
||||||
|
return nil, model.NewAppError("BulkExport", "app.export.export_custom_emoji.mkdir.error", nil, "err="+err.Error(), http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -877,7 +883,7 @@ func (a *App) exportCustomEmoji(c request.CTX, job *model.Job, writer io.Writer,
|
|||||||
emojiImagePath := filepath.Join(emojiPath, emoji.Id, "image")
|
emojiImagePath := filepath.Join(emojiPath, emoji.Id, "image")
|
||||||
filePath := filepath.Join(exportDir, emoji.Id, "image")
|
filePath := filepath.Join(exportDir, emoji.Id, "image")
|
||||||
if exportFiles {
|
if exportFiles {
|
||||||
err := a.copyEmojiImages(emoji.Id, emojiImagePath, pathToDir)
|
err := a.copyEmojiImages(rctx, emoji.Id, emojiImagePath, pathToDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, model.NewAppError("BulkExport", "app.export.export_custom_emoji.copy_emoji_images.error", nil, "err="+err.Error(), http.StatusBadRequest)
|
return nil, model.NewAppError("BulkExport", "app.export.export_custom_emoji.copy_emoji_images.error", nil, "err="+err.Error(), http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
@@ -892,17 +898,21 @@ func (a *App) exportCustomEmoji(c request.CTX, job *model.Job, writer io.Writer,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return emojiPaths, nil
|
return emojiPaths, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copies emoji files from 'data/emoji' dir to 'exported_emoji' dir
|
// Copies emoji files from 'data/emoji' dir to 'exported_emoji' dir
|
||||||
func (a *App) copyEmojiImages(emojiId string, emojiImagePath string, pathToDir string) error {
|
func (a *App) copyEmojiImages(rctx request.CTX, emojiId string, emojiImagePath string, pathToDir string) error {
|
||||||
fromPath, err := os.Open(emojiImagePath)
|
fromPath, err := os.Open(emojiImagePath)
|
||||||
if fromPath == nil || err != nil {
|
if fromPath == nil || err != nil {
|
||||||
return errors.New("Error reading " + emojiImagePath + "file")
|
return errors.New("Error reading " + emojiImagePath + " file")
|
||||||
}
|
}
|
||||||
defer fromPath.Close()
|
defer func() {
|
||||||
|
if err = fromPath.Close(); err != nil {
|
||||||
|
rctx.Logger().Error("Error closing source file", mlog.String("path", emojiImagePath), mlog.Err(err))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
emojiDir := pathToDir + "/" + emojiId
|
emojiDir := pathToDir + "/" + emojiId
|
||||||
|
|
||||||
@@ -920,8 +930,11 @@ func (a *App) copyEmojiImages(emojiId string, emojiImagePath string, pathToDir s
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Error creating the image file " + err.Error())
|
return errors.New("Error creating the image file " + err.Error())
|
||||||
}
|
}
|
||||||
defer toPath.Close()
|
defer func() {
|
||||||
|
if err = toPath.Close(); err != nil {
|
||||||
|
rctx.Logger().Error("Error closing destination file", mlog.String("path", emojiDir+"/image"), mlog.Err(err))
|
||||||
|
}
|
||||||
|
}()
|
||||||
_, err = io.Copy(toPath, fromPath)
|
_, err = io.Copy(toPath, fromPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Error copying emojis " + err.Error())
|
return errors.New("Error copying emojis " + err.Error())
|
||||||
@@ -1159,17 +1172,19 @@ func (a *App) exportAllDirectPosts(ctx request.CTX, job *model.Job, writer io.Wr
|
|||||||
return attachments, nil
|
return attachments, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) exportFile(outPath, filePath string, zipWr *zip.Writer) *model.AppError {
|
func (a *App) exportFile(rctx request.CTX, outPath, filePath string, zipWr *zip.Writer) *model.AppError {
|
||||||
var wr io.Writer
|
|
||||||
var err error
|
|
||||||
rd, appErr := a.FileReader(filePath)
|
rd, appErr := a.FileReader(filePath)
|
||||||
if appErr != nil {
|
if appErr != nil {
|
||||||
return appErr
|
return appErr
|
||||||
}
|
}
|
||||||
defer rd.Close()
|
defer func() {
|
||||||
|
if err := rd.Close(); err != nil {
|
||||||
|
rctx.Logger().Error("Error closing file", mlog.Err(err))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if zipWr != nil {
|
if zipWr != nil {
|
||||||
wr, err = zipWr.CreateHeader(&zip.FileHeader{
|
wr, err := zipWr.CreateHeader(&zip.FileHeader{
|
||||||
Name: filepath.Join(model.ExportDataDir, filePath),
|
Name: filepath.Join(model.ExportDataDir, filePath),
|
||||||
Method: zip.Store,
|
Method: zip.Store,
|
||||||
})
|
})
|
||||||
@@ -1177,25 +1192,34 @@ func (a *App) exportFile(outPath, filePath string, zipWr *zip.Writer) *model.App
|
|||||||
return model.NewAppError("exportFileAttachment", "app.export.export_attachment.zip_create_header.error",
|
return model.NewAppError("exportFileAttachment", "app.export.export_attachment.zip_create_header.error",
|
||||||
nil, "err="+err.Error(), http.StatusInternalServerError)
|
nil, "err="+err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, err = io.Copy(wr, rd); err != nil {
|
||||||
|
return model.NewAppError("exportFileAttachment", "app.export.export_attachment.copy_file.error",
|
||||||
|
nil, "err="+err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
filePath = filepath.Join(outPath, model.ExportDataDir, filePath)
|
filePath = filepath.Join(outPath, model.ExportDataDir, filePath)
|
||||||
if err = os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
|
if err := os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
|
||||||
return model.NewAppError("exportFileAttachment", "app.export.export_attachment.mkdirall.error",
|
return model.NewAppError("exportFileAttachment", "app.export.export_attachment.mkdirall.error",
|
||||||
nil, "err="+err.Error(), http.StatusInternalServerError)
|
nil, "err="+err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
wr, err = os.Create(filePath)
|
file, err := os.Create(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.NewAppError("exportFileAttachment", "app.export.export_attachment.create_file.error",
|
return model.NewAppError("exportFileAttachment", "app.export.export_attachment.create_file.error",
|
||||||
nil, "err="+err.Error(), http.StatusInternalServerError)
|
nil, "err="+err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
defer wr.(*os.File).Close()
|
defer func() {
|
||||||
|
if err = file.Close(); err != nil {
|
||||||
|
rctx.Logger().Error("Error closing file", mlog.Err(err))
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if _, err := io.Copy(wr, rd); err != nil {
|
if _, err = io.Copy(file, rd); err != nil {
|
||||||
return model.NewAppError("exportFileAttachment", "app.export.export_attachment.copy_file.error",
|
return model.NewAppError("exportFileAttachment", "app.export.export_attachment.copy_file.error",
|
||||||
nil, "err="+err.Error(), http.StatusInternalServerError)
|
nil, "err="+err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ func TestCopyEmojiImages(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer os.RemoveAll(filePath)
|
defer os.RemoveAll(filePath)
|
||||||
|
|
||||||
copyError := th.App.copyEmojiImages(emoji.Id, emojiImagePath, pathToDir)
|
copyError := th.App.copyEmojiImages(th.Context, emoji.Id, emojiImagePath, pathToDir)
|
||||||
require.NoError(t, copyError)
|
require.NoError(t, copyError)
|
||||||
|
|
||||||
_, err = os.Stat(pathToDir + "/" + emoji.Id + "/image")
|
_, err = os.Stat(pathToDir + "/" + emoji.Id + "/image")
|
||||||
|
|||||||
@@ -5114,6 +5114,10 @@
|
|||||||
"id": "app.export.export_custom_emoji.copy_emoji_images.error",
|
"id": "app.export.export_custom_emoji.copy_emoji_images.error",
|
||||||
"translation": "Unable to copy custom emoji images"
|
"translation": "Unable to copy custom emoji images"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "app.export.export_custom_emoji.mkdir.error",
|
||||||
|
"translation": "Unable to create a directory for custom emoji images"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "app.export.export_write_line.io_writer.error",
|
"id": "app.export.export_write_line.io_writer.error",
|
||||||
"translation": "An error occurred writing the export data."
|
"translation": "An error occurred writing the export data."
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user