From 7e01cb8227d63bd00d7864cc09c4183e15ca1f93 Mon Sep 17 00:00:00 2001 From: lealife Date: Sat, 10 Oct 2015 14:40:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B2=E6=AD=A2blog=20urlTitle=20=E6=97=A0?= =?UTF-8?q?=E9=99=90=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/init.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/service/init.go b/app/service/init.go index a07e1de..2d662a8 100644 --- a/app/service/init.go +++ b/app/service/init.go @@ -90,7 +90,7 @@ func decodeValue(val string) string { v, _ := url.ParseQuery("a=" + val) return v.Get("a") } - + func encodeValue(val string) string { if val == "" { return val @@ -99,16 +99,17 @@ func encodeValue(val string) string { v.Set("", val) return v.Encode()[1:] } + // 添加笔记时通过title得到urlTitle func fixUrlTitle(urlTitle string) string { if urlTitle != "" { // 把特殊字段给替换掉 -// str := `life "%&()+,/:;<>=?@\|` + // str := `life "%&()+,/:;<>=?@\|` reg, _ := regexp.Compile("/|#|\\$|!|\\^|\\*|'| |\"|%|&|\\(|\\)|\\+|\\,|/|:|;|<|>|=|\\?|@|\\||\\\\") urlTitle = reg.ReplaceAllString(urlTitle, "-") urlTitle = strings.Trim(urlTitle, "-") // 左右单独的-去掉 // 把空格替换成- -// urlTitle = strings.Replace(urlTitle, " ", "-", -1) + // urlTitle = strings.Replace(urlTitle, " ", "-", -1) for strings.Index(urlTitle, "--") >= 0 { // 防止出现连续的-- urlTitle = strings.Replace(urlTitle, "--", "-", -1) } @@ -119,11 +120,20 @@ func fixUrlTitle(urlTitle string) string { func getUniqueUrlTitle(userId string, urlTitle string, types string, padding int) string { 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 { urlTitle2 = urlTitle + "-" + strconv.Itoa(padding) } userIdO := bson.ObjectIdHex(userId) - + var collection *mgo.Collection if types == "note" { collection = db.Notes @@ -136,9 +146,10 @@ func getUniqueUrlTitle(userId string, urlTitle string, types string, padding int padding++ urlTitle2 = urlTitle + "-" + strconv.Itoa(padding) } - + return urlTitle2 } + // types == note,notebook,single func GetUrTitle(userId string, title string, types string) string { urlTitle := strings.Trim(title, " ")