MM-12463 : Added capability to bulk export custom emojis (#9790)
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
2104c6878c
Коммит
246ff89391
@@ -5,14 +5,17 @@ package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
func (a *App) BulkExport(writer io.Writer) *model.AppError {
|
||||
func (a *App) BulkExport(writer io.Writer, file string, pathToEmojiDir string, dirNameToExportEmoji string) *model.AppError {
|
||||
if err := a.ExportVersion(writer); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -32,6 +35,9 @@ func (a *App) BulkExport(writer io.Writer) *model.AppError {
|
||||
if err := a.ExportAllPosts(writer); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.ExportCustomEmoji(writer, file, pathToEmojiDir, dirNameToExportEmoji); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -338,3 +344,88 @@ func (a *App) BuildPostReactions(postId string) (*[]ReactionImportData, *model.A
|
||||
return &reactionsOfPost, nil
|
||||
|
||||
}
|
||||
|
||||
func (a *App) ExportCustomEmoji(writer io.Writer, file string, pathToEmojiDir string, dirNameToExportEmoji string) *model.AppError {
|
||||
pageNumber := 0
|
||||
for {
|
||||
customEmojiList, err := a.GetEmojiList(pageNumber, 100, model.EMOJI_SORT_BY_NAME)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(customEmojiList) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
pageNumber++
|
||||
|
||||
pathToDir := a.createDirForEmoji(file, dirNameToExportEmoji)
|
||||
|
||||
for _, emoji := range customEmojiList {
|
||||
emojiImagePath := pathToEmojiDir + emoji.Id + "/image"
|
||||
err := a.copyEmojiImages(emoji.Id, emojiImagePath, pathToDir)
|
||||
if err != nil {
|
||||
return model.NewAppError("BulkExport", "app.export.export_custom_emoji.copy_emoji_images.error", nil, "err="+err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
filePath := dirNameToExportEmoji + "/" + emoji.Id + "/image"
|
||||
|
||||
emojiImportObject := ImportLineFromEmoji(emoji, filePath)
|
||||
|
||||
if err := a.ExportWriteLine(writer, emojiImportObject); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Creates directory named 'exported_emoji' to copy the emoji files
|
||||
// Directory and the file specified by admin share the same path
|
||||
func (a *App) createDirForEmoji(file string, dirName string) string {
|
||||
pathToFile, _ := filepath.Abs(file)
|
||||
pathSlice := strings.Split(pathToFile, "/")
|
||||
if len(pathSlice) > 0 {
|
||||
pathSlice = pathSlice[:len(pathSlice)-1]
|
||||
}
|
||||
pathToDir := strings.Join(pathSlice, "/") + "/" + dirName
|
||||
|
||||
if _, err := os.Stat(pathToDir); os.IsNotExist(err) {
|
||||
os.Mkdir(pathToDir, os.ModePerm)
|
||||
}
|
||||
return pathToDir
|
||||
}
|
||||
|
||||
// Copies emoji files from 'data/emoji' dir to 'exported_emoji' dir
|
||||
func (a *App) copyEmojiImages(emojiId string, emojiImagePath string, pathToDir string) error {
|
||||
var err error
|
||||
|
||||
fromPath, err := os.Open(emojiImagePath)
|
||||
if fromPath == nil || err != nil {
|
||||
return errors.New("Error reading " + emojiImagePath + "file")
|
||||
}
|
||||
defer fromPath.Close()
|
||||
|
||||
emojiDir := pathToDir + "/" + emojiId
|
||||
|
||||
if _, err := os.Stat(emojiDir); os.IsNotExist(err) {
|
||||
os.Mkdir(emojiDir, os.ModePerm)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.New("Error creating directory for the emoji " + err.Error())
|
||||
}
|
||||
toPath, err := os.OpenFile(emojiDir+"/image", os.O_RDWR|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
return errors.New("Error creating the image file " + err.Error())
|
||||
}
|
||||
defer toPath.Close()
|
||||
|
||||
_, err = io.Copy(toPath, fromPath)
|
||||
if err != nil {
|
||||
return errors.New("Error copying emojis " + err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user