PageRenderTime 126ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/app/controllers/member/MemberBlogController.go

https://gitlab.com/Mr.Tomato/leanote
Go | 526 lines | 408 code | 76 blank | 42 comment | 67 complexity | f1ca18266afc000e238efe4d6015f7c3 MD5 | raw file
  1. package member
  2. import (
  3. "fmt"
  4. "github.com/leanote/leanote/app/info"
  5. . "github.com/leanote/leanote/app/lea"
  6. "github.com/revel/revel"
  7. "io/ioutil"
  8. "os"
  9. "strings"
  10. "time"
  11. // "github.com/leanote/leanote/app/lea/blog"
  12. )
  13. // 博客管理
  14. type MemberBlog struct {
  15. MemberBaseController
  16. }
  17. func (c MemberBlog) common() info.UserBlog {
  18. userId := c.GetUserId()
  19. userInfo := userService.GetUserInfo(userId)
  20. c.RenderArgs["userInfo"] = userInfo
  21. // 得到博客设置信息
  22. c.RenderArgs["allowCustomDomain"] = configService.GetGlobalStringConfig("allowCustomDomain")
  23. userBlog := blogService.GetUserBlog(userId)
  24. c.RenderArgs["userBlog"] = userBlog
  25. c.SetUserInfo()
  26. c.SetLocale()
  27. return userBlog
  28. }
  29. // 得到sorterField 和 isAsc
  30. // okSorter = ['email', 'username']
  31. func (c MemberBlog) getSorter(sorterField string, isAsc bool, okSorter []string) (string, bool) {
  32. sorter := ""
  33. c.Params.Bind(&sorter, "sorter")
  34. if sorter == "" {
  35. return sorterField, isAsc
  36. }
  37. // sorter形式 email-up, email-down
  38. s2 := strings.Split(sorter, "-")
  39. if len(s2) != 2 {
  40. return sorterField, isAsc
  41. }
  42. // 必须是可用的sorter
  43. if okSorter != nil && len(okSorter) > 0 {
  44. if !InArray(okSorter, s2[0]) {
  45. return sorterField, isAsc
  46. }
  47. }
  48. sorterField = strings.Title(s2[0])
  49. if s2[1] == "up" {
  50. isAsc = true
  51. } else {
  52. isAsc = false
  53. }
  54. c.RenderArgs["sorter"] = sorter
  55. return sorterField, isAsc
  56. }
  57. // 博客列表
  58. var userPageSize = 15
  59. func (c MemberBlog) Index(sorter, keywords string) revel.Result {
  60. userId := c.GetUserId()
  61. userInfo := userService.GetUserInfo(userId)
  62. c.RenderArgs["userInfo"] = userInfo
  63. c.RenderArgs["title"] = "Posts"
  64. pageNumber := c.GetPage()
  65. sorterField, isAsc := c.getSorter("CreatedTime", false, []string{"title", "urlTitle", "updatedTime", "publicTime", "createdTime"})
  66. pageInfo, blogs := blogService.ListAllBlogs(c.GetUserId(), "", keywords, false, pageNumber, userPageSize, sorterField, isAsc)
  67. c.RenderArgs["pageInfo"] = pageInfo
  68. c.RenderArgs["blogs"] = blogs
  69. c.RenderArgs["keywords"] = keywords
  70. userAndBlog := userService.GetUserAndBlog(c.GetUserId())
  71. c.RenderArgs["userAndBlog"] = userAndBlog
  72. return c.RenderTemplate("member/blog/list.html")
  73. }
  74. // 修改笔记的urlTitle
  75. func (c MemberBlog) UpdateBlogUrlTitle(noteId, urlTitle string) revel.Result {
  76. re := info.NewRe()
  77. re.Ok, re.Item = blogService.UpateBlogUrlTitle(c.GetUserId(), noteId, urlTitle)
  78. return c.RenderJson(re)
  79. }
  80. // 修改笔记的urlTitle
  81. func (c MemberBlog) UpdateBlogAbstract(noteId string) revel.Result {
  82. c.RenderArgs["title"] = "Update Post Abstract"
  83. note := noteService.GetNoteAndContent(noteId, c.GetUserId())
  84. if !note.Note.IsBlog {
  85. return c.E404()
  86. }
  87. c.RenderArgs["note"] = note
  88. c.RenderArgs["noteId"] = noteId
  89. return c.RenderTemplate("member/blog/update_abstract.html")
  90. }
  91. func (c MemberBlog) DoUpdateBlogAbstract(noteId, imgSrc, desc, abstract string) revel.Result {
  92. re := info.NewRe()
  93. re.Ok = blogService.UpateBlogAbstract(c.GetUserId(), noteId, imgSrc, desc, abstract)
  94. return c.RenderJson(re)
  95. }
  96. // 基本信息设置
  97. func (c MemberBlog) Base() revel.Result {
  98. c.common()
  99. c.RenderArgs["title"] = "Blog Base Info"
  100. return c.RenderTemplate("member/blog/base.html")
  101. }
  102. func (c MemberBlog) Comment() revel.Result {
  103. c.common()
  104. c.RenderArgs["title"] = "Comment"
  105. return c.RenderTemplate("member/blog/comment.html")
  106. }
  107. func (c MemberBlog) Paging() revel.Result {
  108. c.common()
  109. c.RenderArgs["title"] = "Paging"
  110. return c.RenderTemplate("member/blog/paging.html")
  111. }
  112. func (c MemberBlog) Cate() revel.Result {
  113. userBlog := c.common()
  114. c.RenderArgs["title"] = "Cate"
  115. notebooks := blogService.ListBlogNotebooks(c.GetUserId())
  116. notebooksMap := map[string]info.Notebook{}
  117. for _, each := range notebooks {
  118. notebooksMap[each.NotebookId.Hex()] = each
  119. }
  120. var i = 0
  121. notebooks2 := make([]info.Notebook, len(notebooks))
  122. // 先要保证已有的是正确的排序
  123. cateIds := userBlog.CateIds
  124. has := map[string]bool{} // cateIds中有的
  125. if cateIds != nil && len(cateIds) > 0 {
  126. for _, cateId := range cateIds {
  127. if n, ok := notebooksMap[cateId]; ok {
  128. notebooks2[i] = n
  129. i++
  130. has[cateId] = true
  131. }
  132. }
  133. }
  134. // 之后
  135. for _, each := range notebooks {
  136. id := each.NotebookId.Hex()
  137. if !has[id] {
  138. notebooks2[i] = each
  139. i++
  140. }
  141. }
  142. c.RenderArgs["notebooks"] = notebooks2
  143. return c.RenderTemplate("member/blog/cate.html")
  144. }
  145. // 修改分类排序
  146. func (c MemberBlog) UpateCateIds(cateIds []string) revel.Result {
  147. re := info.NewRe()
  148. re.Ok = blogService.UpateCateIds(c.GetUserId(), cateIds)
  149. return c.RenderJson(re)
  150. }
  151. func (c MemberBlog) UpdateCateUrlTitle(cateId, urlTitle string) revel.Result {
  152. re := info.NewRe()
  153. re.Ok, re.Item = blogService.UpateCateUrlTitle(c.GetUserId(), cateId, urlTitle)
  154. return c.RenderJson(re)
  155. }
  156. // 保存之, 包含增加与保存
  157. func (c MemberBlog) DoAddOrUpdateSingle(singleId, title, content string) revel.Result {
  158. re := info.NewRe()
  159. re.Ok = blogService.AddOrUpdateSingle(c.GetUserId(), singleId, title, content)
  160. return c.RenderJson(re)
  161. }
  162. func (c MemberBlog) AddOrUpdateSingle(singleId string) revel.Result {
  163. c.common()
  164. c.RenderArgs["title"] = "Add Single"
  165. c.RenderArgs["singleId"] = singleId
  166. if singleId != "" {
  167. c.RenderArgs["title"] = "Update Single"
  168. c.RenderArgs["single"] = blogService.GetSingle(singleId)
  169. }
  170. return c.RenderTemplate("member/blog/add_single.html")
  171. }
  172. func (c MemberBlog) SortSingles(singleIds []string) revel.Result {
  173. re := info.NewRe()
  174. re.Ok = blogService.SortSingles(c.GetUserId(), singleIds)
  175. return c.RenderJson(re)
  176. }
  177. func (c MemberBlog) DeleteSingle(singleId string) revel.Result {
  178. re := info.NewRe()
  179. re.Ok = blogService.DeleteSingle(c.GetUserId(), singleId)
  180. return c.RenderJson(re)
  181. }
  182. // 修改页面标题
  183. func (c MemberBlog) UpdateSingleUrlTitle(singleId, urlTitle string) revel.Result {
  184. re := info.NewRe()
  185. re.Ok, re.Item = blogService.UpdateSingleUrlTitle(c.GetUserId(), singleId, urlTitle)
  186. return c.RenderJson(re)
  187. }
  188. func (c MemberBlog) Single() revel.Result {
  189. c.common()
  190. c.RenderArgs["title"] = "Cate"
  191. c.RenderArgs["singles"] = blogService.GetSingles(c.GetUserId())
  192. return c.RenderTemplate("member/blog/single.html")
  193. }
  194. // 主题
  195. func (c MemberBlog) Theme() revel.Result {
  196. c.common()
  197. activeTheme, otherThemes := themeService.GetUserThemes(c.GetUserId())
  198. c.RenderArgs["activeTheme"] = activeTheme
  199. c.RenderArgs["otherThemes"] = otherThemes
  200. c.RenderArgs["optionThemes"] = themeService.GetDefaultThemes()
  201. c.RenderArgs["title"] = "Theme"
  202. return c.RenderTemplate("member/blog/theme.html")
  203. }
  204. // 编辑主题
  205. var baseTpls = []string{"header.html", "footer.html", "index.html", "cate.html", "search.html", "post.html", "single.html", "tags.html", "tag_posts.html", "archive.html", "share_comment.html", "404.html", "theme.json", "style.css", "blog.js"}
  206. func (c MemberBlog) UpdateTheme(themeId string, isNew int) revel.Result {
  207. // 查看用户是否有该theme, 若没有则复制default之
  208. // 得到主题的文件列表
  209. userBlog := blogService.GetUserBlog(c.GetUserId())
  210. if themeId == "" {
  211. _, themeId = themeService.NewThemeForFirst(userBlog)
  212. return c.Redirect("/member/blog/updateTheme?themeId=" + themeId)
  213. }
  214. c.common()
  215. c.RenderArgs["title"] = "Upate Theme"
  216. c.RenderArgs["isNew"] = isNew
  217. // 先复制之
  218. c.RenderArgs["themeId"] = themeId
  219. // 得到脚本目录
  220. userId := c.GetUserId()
  221. theme := themeService.GetTheme(userId, themeId)
  222. if theme.ThemeId == "" {
  223. return c.E404()
  224. }
  225. c.RenderArgs["theme"] = theme
  226. path := revel.BasePath + "/" + theme.Path
  227. tpls := ListDir(path)
  228. myTpls := make([]string, len(baseTpls))
  229. tplMap := map[string]bool{}
  230. for i, t := range baseTpls {
  231. myTpls[i] = t
  232. tplMap[t] = true
  233. }
  234. // 得到没有的tpls
  235. for _, t := range tpls {
  236. if t == "images" {
  237. continue
  238. }
  239. if !tplMap[t] {
  240. myTpls = append(myTpls, t)
  241. }
  242. }
  243. c.RenderArgs["myTpls"] = myTpls
  244. return c.RenderTemplate("member/blog/update_theme.html")
  245. }
  246. // 得到文件内容
  247. func (c MemberBlog) GetTplContent(themeId string, filename string) revel.Result {
  248. re := info.NewRe()
  249. re.Ok = true
  250. re.Item = themeService.GetTplContent(c.GetUserId(), themeId, filename)
  251. return c.RenderJson(re)
  252. }
  253. func (c MemberBlog) UpdateTplContent(themeId, filename, content string) revel.Result {
  254. re := info.NewRe()
  255. re.Ok, re.Msg = themeService.UpdateTplContent(c.GetUserId(), themeId, filename, content)
  256. return c.RenderRe(re)
  257. }
  258. func (c MemberBlog) DeleteTpl(themeId, filename string) revel.Result {
  259. re := info.NewRe()
  260. re.Ok = themeService.DeleteTpl(c.GetUserId(), themeId, filename)
  261. return c.RenderJson(re)
  262. }
  263. func (c MemberBlog) ListThemeImages(themeId string) revel.Result {
  264. re := info.NewRe()
  265. userId := c.GetUserId()
  266. path := themeService.GetThemeAbsolutePath(userId, themeId) + "/images"
  267. os.MkdirAll(path, 0755)
  268. images := ListDir(path)
  269. re.List = images
  270. re.Ok = true
  271. return c.RenderJson(re)
  272. }
  273. func (c MemberBlog) DeleteThemeImage(themeId, filename string) revel.Result {
  274. re := info.NewRe()
  275. path := themeService.GetThemeAbsolutePath(c.GetUserId(), themeId) + "/images/" + filename
  276. re.Ok = DeleteFile(path)
  277. return c.RenderJson(re)
  278. }
  279. // 上传主题图片
  280. func (c MemberBlog) UploadThemeImage(themeId string) revel.Result {
  281. re := c.uploadImage(themeId)
  282. c.RenderArgs["fileUrlPath"] = re.Id
  283. c.RenderArgs["resultCode"] = re.Code
  284. c.RenderArgs["resultMsg"] = re.Msg
  285. return c.RenderTemplate("file/blog_logo.html")
  286. }
  287. func (c MemberBlog) uploadImage(themeId string) (re info.Re) {
  288. var fileId = ""
  289. var resultCode = 0 // 1表示正常
  290. var resultMsg = "内部错误" // 错误信息
  291. var Ok = false
  292. defer func() {
  293. re.Id = fileId // 只是id, 没有其它信息
  294. re.Code = resultCode
  295. re.Msg = resultMsg
  296. re.Ok = Ok
  297. }()
  298. file, handel, err := c.Request.FormFile("file")
  299. if err != nil {
  300. return re
  301. }
  302. defer file.Close()
  303. // 生成上传路径
  304. dir := themeService.GetThemeAbsolutePath(c.GetUserId(), themeId) + "/images"
  305. err = os.MkdirAll(dir, 0755)
  306. if err != nil {
  307. return re
  308. }
  309. // 生成新的文件名
  310. filename := handel.Filename
  311. var ext string
  312. _, ext = SplitFilename(filename)
  313. if ext != ".gif" && ext != ".jpg" && ext != ".png" && ext != ".bmp" && ext != ".jpeg" {
  314. resultMsg = "不是图片"
  315. return re
  316. }
  317. filename = filename
  318. data, err := ioutil.ReadAll(file)
  319. if err != nil {
  320. LogJ(err)
  321. return re
  322. }
  323. // > 2M?
  324. if len(data) > 5*1024*1024 {
  325. resultCode = 0
  326. resultMsg = "图片大于2M"
  327. return re
  328. }
  329. toPath := dir + "/" + filename
  330. err = ioutil.WriteFile(toPath, data, 0777)
  331. if err != nil {
  332. LogJ(err)
  333. return re
  334. }
  335. TransToGif(toPath, 0, true)
  336. resultCode = 1
  337. resultMsg = "上传成功!"
  338. return re
  339. }
  340. //
  341. // 使用主题
  342. func (c MemberBlog) ActiveTheme(themeId string) revel.Result {
  343. re := info.NewRe()
  344. re.Ok = themeService.ActiveTheme(c.GetUserId(), themeId)
  345. return c.RenderJson(re)
  346. }
  347. // 删除主题
  348. func (c MemberBlog) DeleteTheme(themeId string) revel.Result {
  349. re := info.NewRe()
  350. re.Ok = themeService.DeleteTheme(c.GetUserId(), themeId)
  351. return c.RenderJson(re)
  352. }
  353. // 管理员公开主题
  354. func (c MemberBlog) PublicTheme(themeId string) revel.Result {
  355. re := info.NewRe()
  356. re.Ok = themeService.PublicTheme(c.GetUserId(), themeId)
  357. return c.RenderJson(re)
  358. }
  359. // 导出
  360. func (c MemberBlog) ExportTheme(themeId string) revel.Result {
  361. re := info.NewRe()
  362. var path string
  363. re.Ok, path = themeService.ExportTheme(c.GetUserId(), themeId)
  364. if !re.Ok {
  365. return c.RenderText("error...")
  366. }
  367. fw, err := os.Open(path)
  368. if err != nil {
  369. return c.RenderText("error")
  370. }
  371. return c.RenderBinary(fw, GetFilename(path), revel.Attachment, time.Now()) // revel.Attachment
  372. }
  373. // 导入主题
  374. func (c MemberBlog) ImportTheme() revel.Result {
  375. re := info.NewRe()
  376. file, handel, err := c.Request.FormFile("file")
  377. if err != nil {
  378. re.Msg = fmt.Sprintf("%v", err)
  379. return c.RenderJson(re)
  380. }
  381. defer file.Close()
  382. // 生成上传路径
  383. userId := c.GetUserId()
  384. dir := revel.BasePath + "/public/upload/" + userId + "/tmp"
  385. err = os.MkdirAll(dir, 0755)
  386. if err != nil {
  387. re.Msg = fmt.Sprintf("%v", err)
  388. return c.RenderJson(re)
  389. }
  390. // 生成新的文件名
  391. filename := handel.Filename
  392. var ext string
  393. _, ext = SplitFilename(filename)
  394. if ext != ".zip" {
  395. re.Msg = "Please upload zip file"
  396. return c.RenderJson(re)
  397. }
  398. filename = filename
  399. data, err := ioutil.ReadAll(file)
  400. if err != nil {
  401. return c.RenderJson(re)
  402. }
  403. // > 10M?
  404. if len(data) > 10*1024*1024 {
  405. re.Msg = "File is big than 10M"
  406. return c.RenderJson(re)
  407. }
  408. toPath := dir + "/" + filename
  409. err = ioutil.WriteFile(toPath, data, 0777)
  410. if err != nil {
  411. re.Msg = fmt.Sprintf("%v", err)
  412. return c.RenderJson(re)
  413. }
  414. // 上传好后, 增加之
  415. re.Ok, re.Msg = themeService.ImportTheme(c.GetUserId(), toPath)
  416. return c.RenderRe(re)
  417. }
  418. // 安装
  419. func (c MemberBlog) InstallTheme(themeId string) revel.Result {
  420. re := info.NewRe()
  421. re.Ok = themeService.InstallTheme(c.GetUserId(), themeId)
  422. return c.RenderJson(re)
  423. }
  424. // 新建主题
  425. func (c MemberBlog) NewTheme() revel.Result {
  426. _, themeId := themeService.NewTheme(c.GetUserId())
  427. return c.Redirect("/member/blog/updateTheme?isNew=1&themeId=" + themeId)
  428. }
  429. //-----------
  430. //
  431. func (c MemberBlog) SetUserBlogBase(userBlog info.UserBlogBase) revel.Result {
  432. re := info.NewRe()
  433. re.Ok = blogService.UpdateUserBlogBase(c.GetUserId(), userBlog)
  434. return c.RenderJson(re)
  435. }
  436. func (c MemberBlog) SetUserBlogComment(userBlog info.UserBlogComment) revel.Result {
  437. re := info.NewRe()
  438. re.Ok = blogService.UpdateUserBlogComment(c.GetUserId(), userBlog)
  439. return c.RenderJson(re)
  440. }
  441. func (c MemberBlog) SetUserBlogStyle(userBlog info.UserBlogStyle) revel.Result {
  442. re := info.NewRe()
  443. re.Ok = blogService.UpdateUserBlogStyle(c.GetUserId(), userBlog)
  444. return c.RenderJson(re)
  445. }
  446. func (c MemberBlog) SetUserBlogPaging(perPageSize int, sortField string, isAsc bool) revel.Result {
  447. re := info.NewRe()
  448. re.Ok, re.Msg = blogService.UpdateUserBlogPaging(c.GetUserId(), perPageSize, sortField, isAsc)
  449. return c.RenderRe(re)
  450. }