| @@ -3,6 +3,7 @@ package models | |||||
| import ( | import ( | ||||
| "fmt" | "fmt" | ||||
| "strings" | "strings" | ||||
| "unicode/utf8" | |||||
| "xorm.io/builder" | "xorm.io/builder" | ||||
| @@ -127,6 +128,32 @@ func GetImageByID(id int64) (*Image, error) { | |||||
| return rel, nil | return rel, nil | ||||
| } | } | ||||
| func SanitizeAndValidateImageTopics(topics []string) (validTopics []string, invalidTopics []string) { | |||||
| validTopics = make([]string, 0) | |||||
| mValidTopics := make(map[string]struct{}) | |||||
| invalidTopics = make([]string, 0) | |||||
| for _, topic := range topics { | |||||
| topic = strings.TrimSpace(strings.ToLower(topic)) | |||||
| // ignore empty string | |||||
| if len(topic) == 0 { | |||||
| continue | |||||
| } | |||||
| // ignore same topic twice | |||||
| if _, ok := mValidTopics[topic]; ok { | |||||
| continue | |||||
| } | |||||
| if utf8.RuneCountInString(topic) <= 35 { | |||||
| validTopics = append(validTopics, topic) | |||||
| mValidTopics[topic] = struct{}{} | |||||
| } else { | |||||
| invalidTopics = append(invalidTopics, topic) | |||||
| } | |||||
| } | |||||
| return validTopics, invalidTopics | |||||
| } | |||||
| func FindImageTopics(opts *FindImageTopicOptions) (topics []*ImageTopic, err error) { | func FindImageTopics(opts *FindImageTopicOptions) (topics []*ImageTopic, err error) { | ||||
| sess := x.Select("image_topic.*").Where(opts.toConds()) | sess := x.Select("image_topic.*").Where(opts.toConds()) | ||||
| if opts.ImageID > 0 { | if opts.ImageID > 0 { | ||||
| @@ -2147,6 +2147,7 @@ topic.manage_topics = Manage Topics | |||||
| topic.done = Done | topic.done = Done | ||||
| topic.count_prompt = You can not select more than 25 topics | topic.count_prompt = You can not select more than 25 topics | ||||
| topic.format_prompt = Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long. | topic.format_prompt = Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long. | ||||
| imagetopic.format_prompt = Topics can be up to 35 characters long. | |||||
| [org] | [org] | ||||
| org_name_holder = Organization Name | org_name_holder = Organization Name | ||||
| @@ -2155,6 +2155,7 @@ topic.manage_topics=管理主题 | |||||
| topic.done=保存 | topic.done=保存 | ||||
| topic.count_prompt=您最多选择25个标签 | topic.count_prompt=您最多选择25个标签 | ||||
| topic.format_prompt=标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符 | topic.format_prompt=标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符 | ||||
| imagetopic.format_prompt=标签长度不得超过35个字符 | |||||
| [org] | [org] | ||||
| org_name_holder=组织名称 | org_name_holder=组织名称 | ||||
| @@ -586,11 +586,18 @@ func checkTopics(Topics string) ([]string, string) { | |||||
| topics = strings.Split(topicsStr, ",") | topics = strings.Split(topicsStr, ",") | ||||
| } | } | ||||
| if len(topics) > 25 { | |||||
| validTopics, invalidTopics := models.SanitizeAndValidateImageTopics(topics) | |||||
| if len(validTopics) > 25 { | |||||
| return nil, "repo.topic.count_prompt" | return nil, "repo.topic.count_prompt" | ||||
| } | } | ||||
| return topics, "" | |||||
| if len(invalidTopics) > 0 { | |||||
| return nil, "repo.imagetopic.format_prompt" | |||||
| } | |||||
| return validTopics, "" | |||||
| } | } | ||||
| func CloudBrainStop(ctx *context.Context) { | func CloudBrainStop(ctx *context.Context) { | ||||