diff options
Diffstat (limited to 'app/import.go')
-rw-r--r-- | app/import.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/app/import.go b/app/import.go index baf936567..37bef01e7 100644 --- a/app/import.go +++ b/app/import.go @@ -33,6 +33,7 @@ type LineImportData struct { Post *PostImportData `json:"post"` DirectChannel *DirectChannelImportData `json:"direct_channel"` DirectPost *DirectPostImportData `json:"direct_post"` + Emoji *EmojiImportData `json:"emoji"` Version *int `json:"version"` } @@ -114,6 +115,11 @@ type UserChannelNotifyPropsImportData struct { MarkUnread *string `json:"mark_unread"` } +type EmojiImportData struct { + Name *string `json:"name"` + Image *string `json:"image"` +} + type ReactionImportData struct { User *string `json:"user"` CreateAt *int64 `json:"create_at"` @@ -337,6 +343,12 @@ func (a *App) ImportLine(line LineImportData, dryRun bool) *model.AppError { } else { return a.ImportDirectPost(line.DirectPost, dryRun) } + case line.Type == "emoji": + if line.Emoji == nil { + return model.NewAppError("BulkImport", "app.import.import_line.null_emoji.error", nil, "", http.StatusBadRequest) + } else { + return a.ImportEmoji(line.Emoji, dryRun) + } default: return model.NewAppError("BulkImport", "app.import.import_line.unknown_line_type.error", map[string]interface{}{"Type": line.Type}, "", http.StatusBadRequest) } @@ -1925,6 +1937,71 @@ func validateDirectPostImportData(data *DirectPostImportData, maxPostSize int) * return nil } +func (a *App) ImportEmoji(data *EmojiImportData, dryRun bool) *model.AppError { + if err := validateEmojiImportData(data); err != nil { + return err + } + + // If this is a Dry Run, do not continue any further. + if dryRun { + return nil + } + + var emoji *model.Emoji + + if result := <-a.Srv.Store.Emoji().GetByName(*data.Name); result.Err != nil && result.Err.StatusCode != http.StatusNotFound { + return result.Err + } else if result.Data != nil { + emoji = result.Data.(*model.Emoji) + } + + alreadyExists := emoji != nil + + if !alreadyExists { + emoji = &model.Emoji{ + Name: *data.Name, + } + emoji.PreSave() + } + + file, err := os.Open(*data.Image) + if err != nil { + return model.NewAppError("BulkImport", "app.import.emoji.bad_file.error", map[string]interface{}{"EmojiName": *data.Name}, "", http.StatusBadRequest) + } + + if _, err := a.WriteFile(file, getEmojiImagePath(emoji.Id)); err != nil { + return err + } + + if !alreadyExists { + if result := <-a.Srv.Store.Emoji().Save(emoji); result.Err != nil { + return result.Err + } + } + + return nil +} + +func validateEmojiImportData(data *EmojiImportData) *model.AppError { + if data == nil { + return model.NewAppError("BulkImport", "app.import.validate_emoji_import_data.empty.error", nil, "", http.StatusBadRequest) + } + + if data.Name == nil || len(*data.Name) == 0 { + return model.NewAppError("BulkImport", "app.import.validate_emoji_import_data.name_missing.error", nil, "", http.StatusBadRequest) + } + + if err := model.IsValidEmojiName(*data.Name); err != nil { + return err + } + + if data.Image == nil || len(*data.Image) == 0 { + return model.NewAppError("BulkImport", "app.import.validate_emoji_import_data.image_missing.error", nil, "", http.StatusBadRequest) + } + + return nil +} + // // -- Old SlackImport Functions -- // Import functions are sutible for entering posts and users into the database without |