防止blog urlTitle 无限循环

This commit is contained in:
lealife
2015-10-10 14:40:52 +08:00
parent ae0eb3b918
commit 7e01cb8227

View File

@ -99,16 +99,17 @@ func encodeValue(val string) string {
v.Set("", val) v.Set("", val)
return v.Encode()[1:] return v.Encode()[1:]
} }
// 添加笔记时通过title得到urlTitle // 添加笔记时通过title得到urlTitle
func fixUrlTitle(urlTitle string) string { func fixUrlTitle(urlTitle string) string {
if urlTitle != "" { if urlTitle != "" {
// 把特殊字段给替换掉 // 把特殊字段给替换掉
// str := `life "%&()+,/:;<>=?@\|` // str := `life "%&()+,/:;<>=?@\|`
reg, _ := regexp.Compile("/|#|\\$|!|\\^|\\*|'| |\"|%|&|\\(|\\)|\\+|\\,|/|:|;|<|>|=|\\?|@|\\||\\\\") reg, _ := regexp.Compile("/|#|\\$|!|\\^|\\*|'| |\"|%|&|\\(|\\)|\\+|\\,|/|:|;|<|>|=|\\?|@|\\||\\\\")
urlTitle = reg.ReplaceAllString(urlTitle, "-") urlTitle = reg.ReplaceAllString(urlTitle, "-")
urlTitle = strings.Trim(urlTitle, "-") // 左右单独的-去掉 urlTitle = strings.Trim(urlTitle, "-") // 左右单独的-去掉
// 把空格替换成- // 把空格替换成-
// urlTitle = strings.Replace(urlTitle, " ", "-", -1) // urlTitle = strings.Replace(urlTitle, " ", "-", -1)
for strings.Index(urlTitle, "--") >= 0 { // 防止出现连续的-- for strings.Index(urlTitle, "--") >= 0 { // 防止出现连续的--
urlTitle = strings.Replace(urlTitle, "--", "-", -1) urlTitle = strings.Replace(urlTitle, "--", "-", -1)
} }
@ -119,6 +120,15 @@ func fixUrlTitle(urlTitle string) string {
func getUniqueUrlTitle(userId string, urlTitle string, types string, padding int) string { func getUniqueUrlTitle(userId string, urlTitle string, types string, padding int) string {
urlTitle2 := urlTitle urlTitle2 := urlTitle
// 判断urlTitle是不是过长, 过长则截断, 300
// 不然生成index有问题
// it will not index a single field with more than 1024 bytes.
// If you're indexing a field that is 2.5MB, it's not really indexing it, it's being skipped.
if len(urlTitle2) > 320 {
urlTitle2 = urlTitle2[:300] // 为什么要少些, 因为怕无限循环, 因为把padding截了
}
if padding > 1 { if padding > 1 {
urlTitle2 = urlTitle + "-" + strconv.Itoa(padding) urlTitle2 = urlTitle + "-" + strconv.Itoa(padding)
} }
@ -139,6 +149,7 @@ func getUniqueUrlTitle(userId string, urlTitle string, types string, padding int
return urlTitle2 return urlTitle2
} }
// types == note,notebook,single // types == note,notebook,single
func GetUrTitle(userId string, title string, types string) string { func GetUrTitle(userId string, title string, types string) string {
urlTitle := strings.Trim(title, " ") urlTitle := strings.Trim(title, " ")