Files
leanote/app/service/init.go

179 lines
4.8 KiB
Go
Raw Permalink Normal View History

2014-05-07 13:06:24 +08:00
package service
import (
2014-11-12 17:32:03 +08:00
"github.com/leanote/leanote/app/db"
. "github.com/leanote/leanote/app/lea"
2015-11-13 17:58:41 +08:00
"gopkg.in/mgo.v2"
2014-11-12 17:32:03 +08:00
"gopkg.in/mgo.v2/bson"
2015-11-13 17:58:41 +08:00
"net/url"
"regexp"
"strconv"
"strings"
2014-05-07 13:06:24 +08:00
)
// init service, for share service bettween services
// 初始化, 实例service
// 为了共享service
2014-09-22 19:15:28 +08:00
var notebookService, NotebookS *NotebookService
var noteService, NoteS *NoteService
var noteContentHistoryService, NoteContentHistoryS *NoteContentHistoryService
var trashService, TrashS *TrashService
var shareService, ShareS *ShareService
var userService, UserS *UserService
2014-11-12 17:32:03 +08:00
var groupService, GroupS *GroupService
2014-09-22 19:15:28 +08:00
var tagService, TagS *TagService
var blogService, BlogS *BlogService
var tokenService, TokenS *TokenService
var noteImageService, NoteImageS *NoteImageService
var fileService, FileS *FileService
var albumService, AlbumS *AlbumService
var attachService, AttachS *AttachService
var configService, ConfigS *ConfigService
2014-09-22 19:15:28 +08:00
var PwdS *PwdService
var SuggestionS *SuggestionService
2014-10-22 16:20:45 +08:00
var emailService, EmailS *EmailService
2014-09-22 19:15:28 +08:00
var AuthS *AuthService
2014-10-22 16:20:45 +08:00
var UpgradeS *UpgradeService
var SessionS, sessionService *SessionService
2014-11-09 16:24:19 +08:00
var ThemeS, themeService *ThemeService
2014-10-22 16:20:45 +08:00
2014-09-22 19:15:28 +08:00
// onAppStart调用
func InitService() {
NotebookS = &NotebookService{}
NoteS = &NoteService{}
NoteContentHistoryS = &NoteContentHistoryService{}
TrashS = &TrashService{}
ShareS = &ShareService{}
UserS = &UserService{}
2014-11-12 17:32:03 +08:00
GroupS = &GroupService{}
2014-09-22 19:15:28 +08:00
TagS = &TagService{}
BlogS = &BlogService{}
TokenS = &TokenService{}
NoteImageS = &NoteImageService{}
FileS = &FileService{}
AlbumS = &AlbumService{}
AttachS = &AttachService{}
ConfigS = &ConfigService{}
2014-09-22 19:15:28 +08:00
PwdS = &PwdService{}
SuggestionS = &SuggestionService{}
AuthS = &AuthService{}
2014-10-22 16:20:45 +08:00
EmailS = NewEmailService()
UpgradeS = &UpgradeService{}
SessionS = &SessionService{}
2014-11-09 16:24:19 +08:00
ThemeS = &ThemeService{}
2015-11-13 17:58:41 +08:00
2014-09-22 19:15:28 +08:00
notebookService = NotebookS
noteService = NoteS
noteContentHistoryService = NoteContentHistoryS
trashService = TrashS
shareService = ShareS
userService = UserS
2014-11-12 17:32:03 +08:00
groupService = GroupS
2014-09-22 19:15:28 +08:00
tagService = TagS
blogService = BlogS
tokenService = TokenS
noteImageService = NoteImageS
fileService = FileS
albumService = AlbumS
attachService = AttachS
configService = ConfigS
2014-10-22 16:20:45 +08:00
emailService = EmailS
sessionService = SessionS
2014-11-09 16:24:19 +08:00
themeService = ThemeS
}
2014-11-12 17:32:03 +08:00
//----------------
// service 公用方法
// 将name=val的val进行encoding
func decodeValue(val string) string {
v, _ := url.ParseQuery("a=" + val)
return v.Get("a")
}
2015-10-10 14:40:52 +08:00
2014-11-12 17:32:03 +08:00
func encodeValue(val string) string {
if val == "" {
return val
}
v := url.Values{}
v.Set("", val)
return v.Encode()[1:]
}
2015-10-10 14:40:52 +08:00
2014-11-12 17:32:03 +08:00
// 添加笔记时通过title得到urlTitle
func fixUrlTitle(urlTitle string) string {
if urlTitle != "" {
// 把特殊字段给替换掉
2015-10-10 14:40:52 +08:00
// str := `life "%&()+,/:;<>=?@\|`
2014-11-12 17:32:03 +08:00
reg, _ := regexp.Compile("/|#|\\$|!|\\^|\\*|'| |\"|%|&|\\(|\\)|\\+|\\,|/|:|;|<|>|=|\\?|@|\\||\\\\")
urlTitle = reg.ReplaceAllString(urlTitle, "-")
urlTitle = strings.Trim(urlTitle, "-") // 左右单独的-去掉
// 把空格替换成-
2015-10-10 14:40:52 +08:00
// urlTitle = strings.Replace(urlTitle, " ", "-", -1)
2014-11-12 17:32:03 +08:00
for strings.Index(urlTitle, "--") >= 0 { // 防止出现连续的--
urlTitle = strings.Replace(urlTitle, "--", "-", -1)
}
return encodeValue(urlTitle)
}
return urlTitle
}
func getUniqueUrlTitle(userId string, urlTitle string, types string, padding int) string {
urlTitle2 := urlTitle
2015-10-10 14:40:52 +08:00
// 判断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截了
}
2014-11-12 17:32:03 +08:00
if padding > 1 {
urlTitle2 = urlTitle + "-" + strconv.Itoa(padding)
}
userIdO := bson.ObjectIdHex(userId)
2015-10-10 14:40:52 +08:00
2014-11-12 17:32:03 +08:00
var collection *mgo.Collection
if types == "note" {
collection = db.Notes
} else if types == "notebook" {
collection = db.Notebooks
} else if types == "single" {
collection = db.BlogSingles
}
for db.Has(collection, bson.M{"UserId": userIdO, "UrlTitle": urlTitle2}) { // 用户下唯一
padding++
urlTitle2 = urlTitle + "-" + strconv.Itoa(padding)
}
2015-10-10 14:40:52 +08:00
2014-11-12 17:32:03 +08:00
return urlTitle2
}
2015-10-10 14:40:52 +08:00
// 截取id 24位变成12位
// 先md5, 再取12位
func subIdHalf(id string) string {
idMd5 := Md5(id)
return idMd5[:12]
}
2014-11-12 17:32:03 +08:00
// types == note,notebook,single
// id noteId, notebookId, singleId 当title没的时候才有用, 用它来替换
func GetUrTitle(userId string, title string, types string, id string) string {
2014-11-12 17:32:03 +08:00
urlTitle := strings.Trim(title, " ")
if urlTitle == "" {
if id == "" {
urlTitle = "Untitled-" + userId
} else {
urlTitle = subIdHalf(id)
}
// 不允许title是ObjectId
} else if bson.IsObjectIdHex(title) {
urlTitle = subIdHalf(id)
2014-11-12 17:32:03 +08:00
}
2014-11-12 17:32:03 +08:00
urlTitle = fixUrlTitle(urlTitle)
return getUniqueUrlTitle(userId, urlTitle, types, 1)
}