2014-05-07 13:06:24 +08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/revel/revel"
|
2015-10-17 17:15:33 +08:00
|
|
|
// "encoding/json"
|
2014-05-07 13:06:24 +08:00
|
|
|
"github.com/leanote/leanote/app/info"
|
2015-10-17 17:15:33 +08:00
|
|
|
. "github.com/leanote/leanote/app/lea"
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
2015-03-31 14:27:26 +08:00
|
|
|
"strings"
|
2015-10-17 17:15:33 +08:00
|
|
|
"time"
|
2017-07-27 15:08:34 +08:00
|
|
|
"runtime"
|
2015-10-17 17:15:33 +08:00
|
|
|
// "github.com/leanote/leanote/app/types"
|
|
|
|
// "io/ioutil"
|
2015-11-28 13:47:59 +08:00
|
|
|
"fmt"
|
2015-10-17 17:15:33 +08:00
|
|
|
// "bytes"
|
|
|
|
// "os"
|
2014-05-07 13:06:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Note struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
|
|
|
// 笔记首页, 判断是否已登录
|
|
|
|
// 已登录, 得到用户基本信息(notebook, shareNotebook), 跳转到index.html中
|
|
|
|
// 否则, 转向登录页面
|
2015-10-10 13:46:58 +08:00
|
|
|
func (c Note) Index(noteId, online string) revel.Result {
|
2014-05-07 13:06:24 +08:00
|
|
|
c.SetLocale()
|
2015-10-10 16:10:54 +08:00
|
|
|
userInfo := c.GetUserAndBlogUrl()
|
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
userId := userInfo.UserId.Hex()
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// 没有登录
|
|
|
|
if userId == "" {
|
|
|
|
return c.Redirect("/login")
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["openRegister"] = configService.IsOpenRegister()
|
2014-12-09 23:17:36 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// 已登录了, 那么得到所有信息
|
|
|
|
notebooks := notebookService.GetNotebooks(userId)
|
|
|
|
shareNotebooks, sharedUserInfos := shareService.GetShareNotebooks(userId)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// 还需要按时间排序(DESC)得到notes
|
|
|
|
notes := []info.Note{}
|
|
|
|
noteContent := info.NoteContent{}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
if len(notebooks) > 0 {
|
2014-12-09 23:17:36 +08:00
|
|
|
// noteId是否存在
|
|
|
|
// 是否传入了正确的noteId
|
|
|
|
hasRightNoteId := false
|
|
|
|
if IsObjectId(noteId) {
|
|
|
|
note := noteService.GetNoteById(noteId)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-12-09 23:17:36 +08:00
|
|
|
if note.NoteId != "" {
|
2015-06-15 18:01:48 +08:00
|
|
|
var noteOwner = note.UserId.Hex()
|
|
|
|
noteContent = noteService.GetNoteContent(noteId, noteOwner)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-12-09 23:17:36 +08:00
|
|
|
hasRightNoteId = true
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["curNoteId"] = noteId
|
|
|
|
c.ViewArgs["curNotebookId"] = note.NotebookId.Hex()
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-12-09 23:17:36 +08:00
|
|
|
// 打开的是共享的笔记, 那么判断是否是共享给我的默认笔记
|
|
|
|
if noteOwner != c.GetUserId() {
|
|
|
|
if shareService.HasReadPerm(noteOwner, c.GetUserId(), noteId) {
|
|
|
|
// 不要获取notebook下的笔记
|
|
|
|
// 在前端下发请求
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["curSharedNoteNotebookId"] = note.NotebookId.Hex()
|
|
|
|
c.ViewArgs["curSharedUserId"] = noteOwner
|
2015-11-13 17:58:41 +08:00
|
|
|
// 没有读写权限
|
2014-12-09 23:17:36 +08:00
|
|
|
} else {
|
|
|
|
hasRightNoteId = false
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-13 17:58:41 +08:00
|
|
|
_, notes = noteService.ListNotes(c.GetUserId(), note.NotebookId.Hex(), false, c.GetPage(), 50, defaultSortField, false, false)
|
|
|
|
|
2014-12-09 23:17:36 +08:00
|
|
|
// 如果指定了某笔记, 则该笔记放在首位
|
|
|
|
lenNotes := len(notes)
|
|
|
|
if lenNotes > 1 {
|
|
|
|
notes2 := make([]info.Note, len(notes))
|
|
|
|
notes2[0] = note
|
|
|
|
i := 1
|
|
|
|
for _, note := range notes {
|
|
|
|
if note.NoteId.Hex() != noteId {
|
|
|
|
if i == lenNotes { // 防止越界
|
2015-11-13 17:58:41 +08:00
|
|
|
break
|
2014-12-09 23:17:36 +08:00
|
|
|
}
|
|
|
|
notes2[i] = note
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notes = notes2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-12-09 23:17:36 +08:00
|
|
|
// 得到最近的笔记
|
2015-11-13 17:58:41 +08:00
|
|
|
_, latestNotes := noteService.ListNotes(c.GetUserId(), "", false, c.GetPage(), 50, defaultSortField, false, false)
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["latestNotes"] = latestNotes
|
2014-12-09 23:17:36 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-12-09 23:17:36 +08:00
|
|
|
// 没有传入笔记
|
|
|
|
// 那么得到最新笔记
|
|
|
|
if !hasRightNoteId {
|
2015-11-13 17:58:41 +08:00
|
|
|
_, notes = noteService.ListNotes(c.GetUserId(), "", false, c.GetPage(), 50, defaultSortField, false, false)
|
2014-12-09 23:17:36 +08:00
|
|
|
if len(notes) > 0 {
|
|
|
|
noteContent = noteService.GetNoteContent(notes[0].NoteId.Hex(), userId)
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["curNoteId"] = notes[0].NoteId.Hex()
|
2014-12-09 23:17:36 +08:00
|
|
|
}
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// 当然, 还需要得到第一个notes的content
|
|
|
|
//...
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["isAdmin"] = configService.GetAdminUsername() == userInfo.Username
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["userInfo"] = userInfo
|
|
|
|
c.ViewArgs["notebooks"] = notebooks
|
|
|
|
c.ViewArgs["shareNotebooks"] = shareNotebooks // note信息在notes列表中
|
|
|
|
c.ViewArgs["sharedUserInfos"] = sharedUserInfos
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["notes"] = notes
|
|
|
|
c.ViewArgs["noteContentJson"] = noteContent
|
|
|
|
c.ViewArgs["noteContent"] = noteContent.Content
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["tags"] = tagService.GetTags(c.GetUserId())
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["globalConfigs"] = configService.GetGlobalConfigForUser()
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-09-06 10:36:10 +08:00
|
|
|
// return c.RenderTemplate("note/note.html")
|
2015-10-10 16:55:36 +08:00
|
|
|
|
2015-10-10 13:46:58 +08:00
|
|
|
if isDev, _ := revel.Config.Bool("mode.dev"); isDev && online == "" {
|
2014-05-07 13:06:24 +08:00
|
|
|
return c.RenderTemplate("note/note-dev.html")
|
|
|
|
} else {
|
|
|
|
return c.RenderTemplate("note/note.html")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 首页, 判断是否已登录
|
|
|
|
// 已登录, 得到用户基本信息(notebook, shareNotebook), 跳转到index.html中
|
|
|
|
// 否则, 转向登录页面
|
|
|
|
func (c Note) ListNotes(notebookId string) revel.Result {
|
2015-10-10 16:10:54 +08:00
|
|
|
_, notes := noteService.ListNotes(c.GetUserId(), notebookId, false, c.GetPage(), pageSize, defaultSortField, false, false)
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(notes)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 得到trash
|
|
|
|
func (c Note) ListTrashNotes() revel.Result {
|
2015-10-10 16:10:54 +08:00
|
|
|
_, notes := noteService.ListNotes(c.GetUserId(), "", true, c.GetPage(), pageSize, defaultSortField, false, false)
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(notes)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 22:23:35 +08:00
|
|
|
// 得到note和内容
|
|
|
|
func (c Note) GetNoteAndContent(noteId string) revel.Result {
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(noteService.GetNoteAndContent(noteId, c.GetUserId()))
|
2014-05-14 22:23:35 +08:00
|
|
|
}
|
|
|
|
|
2017-02-18 20:01:14 +08:00
|
|
|
func (c Note) GetNoteAndContentBySrc(src string) revel.Result {
|
|
|
|
noteId, noteAndContent := noteService.GetNoteAndContentBySrc(src, c.GetUserId())
|
|
|
|
ret := info.Re{}
|
|
|
|
if noteId != "" {
|
|
|
|
ret.Ok = true
|
|
|
|
ret.Item = noteAndContent
|
|
|
|
}
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(ret)
|
2017-02-18 20:01:14 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// 得到内容
|
|
|
|
func (c Note) GetNoteContent(noteId string) revel.Result {
|
|
|
|
noteContent := noteService.GetNoteContent(noteId, c.GetUserId())
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(noteContent)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 这里不能用json, 要用post
|
2015-11-28 21:26:29 +08:00
|
|
|
func (c Note) UpdateNoteOrContent(noteOrContent info.NoteOrContent) revel.Result {
|
2014-05-07 13:06:24 +08:00
|
|
|
// 新添加note
|
|
|
|
if noteOrContent.IsNew {
|
2015-11-13 17:58:41 +08:00
|
|
|
userId := c.GetObjectUserId()
|
|
|
|
// myUserId := userId
|
2014-05-07 13:06:24 +08:00
|
|
|
// 为共享新建?
|
|
|
|
if noteOrContent.FromUserId != "" {
|
|
|
|
userId = bson.ObjectIdHex(noteOrContent.FromUserId)
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
|
|
|
note := info.Note{UserId: userId,
|
|
|
|
NoteId: bson.ObjectIdHex(noteOrContent.NoteId),
|
|
|
|
NotebookId: bson.ObjectIdHex(noteOrContent.NotebookId),
|
|
|
|
Title: noteOrContent.Title,
|
2017-02-18 20:01:14 +08:00
|
|
|
Src: noteOrContent.Src, // 来源
|
2015-11-13 17:58:41 +08:00
|
|
|
Tags: strings.Split(noteOrContent.Tags, ","),
|
|
|
|
Desc: noteOrContent.Desc,
|
|
|
|
ImgSrc: noteOrContent.ImgSrc,
|
|
|
|
IsBlog: noteOrContent.IsBlog,
|
2014-05-07 13:06:24 +08:00
|
|
|
IsMarkdown: noteOrContent.IsMarkdown,
|
2015-11-13 17:58:41 +08:00
|
|
|
}
|
|
|
|
noteContent := info.NoteContent{NoteId: note.NoteId,
|
|
|
|
UserId: userId,
|
|
|
|
IsBlog: note.IsBlog,
|
|
|
|
Content: noteOrContent.Content,
|
|
|
|
Abstract: noteOrContent.Abstract}
|
|
|
|
|
2014-11-09 22:26:38 +08:00
|
|
|
note = noteService.AddNoteAndContentForController(note, noteContent, c.GetUserId())
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(note)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
noteUpdate := bson.M{}
|
|
|
|
needUpdateNote := false
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// Desc前台传来
|
|
|
|
if c.Has("Desc") {
|
|
|
|
needUpdateNote = true
|
2015-11-13 17:58:41 +08:00
|
|
|
noteUpdate["Desc"] = noteOrContent.Desc
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
|
|
|
if c.Has("ImgSrc") {
|
|
|
|
needUpdateNote = true
|
2015-11-13 17:58:41 +08:00
|
|
|
noteUpdate["ImgSrc"] = noteOrContent.ImgSrc
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
|
|
|
if c.Has("Title") {
|
|
|
|
needUpdateNote = true
|
2015-11-13 17:58:41 +08:00
|
|
|
noteUpdate["Title"] = noteOrContent.Title
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
if c.Has("Tags") {
|
2014-05-07 13:06:24 +08:00
|
|
|
needUpdateNote = true
|
2015-11-13 17:58:41 +08:00
|
|
|
noteUpdate["Tags"] = strings.Split(noteOrContent.Tags, ",")
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
// web端不控制
|
2015-11-13 17:58:41 +08:00
|
|
|
if needUpdateNote {
|
|
|
|
noteService.UpdateNote(c.GetUserId(),
|
2015-03-31 14:27:26 +08:00
|
|
|
noteOrContent.NoteId, noteUpdate, -1)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
//-------------
|
2015-03-31 14:27:26 +08:00
|
|
|
afterContentUsn := 0
|
|
|
|
contentOk := false
|
|
|
|
contentMsg := ""
|
2014-05-07 13:06:24 +08:00
|
|
|
if c.Has("Content") {
|
2015-11-13 17:58:41 +08:00
|
|
|
// noteService.UpdateNoteContent(noteOrContent.UserId, c.GetUserId(),
|
|
|
|
// noteOrContent.NoteId, noteOrContent.Content, noteOrContent.Abstract)
|
2015-03-31 14:27:26 +08:00
|
|
|
contentOk, contentMsg, afterContentUsn = noteService.UpdateNoteContent(c.GetUserId(),
|
2015-11-28 13:47:59 +08:00
|
|
|
noteOrContent.NoteId, noteOrContent.Content, noteOrContent.Abstract,
|
|
|
|
needUpdateNote, -1, time.Now())
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
Log(afterContentUsn)
|
|
|
|
Log(contentOk)
|
|
|
|
Log(contentMsg)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(true)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 删除note/ 删除别人共享给我的笔记
|
|
|
|
// userId 是note.UserId
|
2015-10-27 23:04:39 +08:00
|
|
|
func (c Note) DeleteNote(noteIds []string, isShared bool) revel.Result {
|
|
|
|
if !isShared {
|
|
|
|
for _, noteId := range noteIds {
|
|
|
|
trashService.DeleteNote(noteId, c.GetUserId())
|
|
|
|
}
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(true)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-10-27 23:04:39 +08:00
|
|
|
|
|
|
|
for _, noteId := range noteIds {
|
|
|
|
trashService.DeleteSharedNote(noteId, c.GetUserId())
|
|
|
|
}
|
|
|
|
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(true)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-10-27 23:04:39 +08:00
|
|
|
|
|
|
|
// 删除trash, 已弃用, 用DeleteNote
|
2014-05-07 13:06:24 +08:00
|
|
|
func (c Note) DeleteTrash(noteId string) revel.Result {
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(trashService.DeleteTrash(noteId, c.GetUserId()))
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-10-27 23:04:39 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// 移动note
|
2015-10-27 23:04:39 +08:00
|
|
|
func (c Note) MoveNote(noteIds []string, notebookId string) revel.Result {
|
|
|
|
userId := c.GetUserId()
|
|
|
|
for _, noteId := range noteIds {
|
|
|
|
noteService.MoveNote(noteId, notebookId, userId)
|
|
|
|
}
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(true)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-10-27 23:04:39 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// 复制note
|
2015-10-27 23:04:39 +08:00
|
|
|
func (c Note) CopyNote(noteIds []string, notebookId string) revel.Result {
|
|
|
|
copyNotes := make([]info.Note, len(noteIds))
|
|
|
|
userId := c.GetUserId()
|
|
|
|
for i, noteId := range noteIds {
|
|
|
|
copyNotes[i] = noteService.CopyNote(noteId, notebookId, userId)
|
|
|
|
}
|
|
|
|
re := info.NewRe()
|
|
|
|
re.Ok = true
|
|
|
|
re.Item = copyNotes
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(re)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-10-27 23:04:39 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// 复制别人共享的笔记给我
|
2015-10-27 23:04:39 +08:00
|
|
|
func (c Note) CopySharedNote(noteIds []string, notebookId, fromUserId string) revel.Result {
|
|
|
|
copyNotes := make([]info.Note, len(noteIds))
|
|
|
|
userId := c.GetUserId()
|
|
|
|
for i, noteId := range noteIds {
|
|
|
|
copyNotes[i] = noteService.CopySharedNote(noteId, notebookId, fromUserId, userId)
|
|
|
|
}
|
|
|
|
re := info.NewRe()
|
|
|
|
re.Ok = true
|
|
|
|
re.Item = copyNotes
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(re)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------
|
|
|
|
// search
|
|
|
|
// 通过title搜索
|
|
|
|
func (c Note) SearchNote(key string) revel.Result {
|
|
|
|
_, blogs := noteService.SearchNote(key, c.GetUserId(), c.GetPage(), pageSize, "UpdatedTime", false, false)
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(blogs)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
2015-10-27 23:04:39 +08:00
|
|
|
|
2014-05-07 13:06:24 +08:00
|
|
|
// 通过tags搜索
|
|
|
|
func (c Note) SearchNoteByTags(tags []string) revel.Result {
|
|
|
|
_, blogs := noteService.SearchNoteByTags(tags, c.GetUserId(), c.GetPage(), pageSize, "UpdatedTime", false)
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(blogs)
|
2014-05-07 13:06:24 +08:00
|
|
|
}
|
|
|
|
|
2015-10-17 17:15:33 +08:00
|
|
|
// 生成PDF
|
|
|
|
func (c Note) ToPdf(noteId, appKey string) revel.Result {
|
|
|
|
// 虽然传了cookie但是这里还是不能得到userId, 所以还是通过appKey来验证之
|
|
|
|
appKeyTrue, _ := revel.Config.String("app.secret")
|
|
|
|
if appKeyTrue != appKey {
|
2015-11-28 15:17:36 +08:00
|
|
|
return c.RenderText("auth error")
|
2015-10-17 17:15:33 +08:00
|
|
|
}
|
|
|
|
note := noteService.GetNoteById(noteId)
|
|
|
|
if note.NoteId == "" {
|
2015-11-28 15:17:36 +08:00
|
|
|
return c.RenderText("no note")
|
2015-10-17 17:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
noteUserId := note.UserId.Hex()
|
|
|
|
content := noteService.GetNoteContent(noteId, noteUserId)
|
|
|
|
userInfo := userService.GetUserInfo(noteUserId)
|
|
|
|
|
|
|
|
//------------------
|
|
|
|
// 将content的图片转换为base64
|
|
|
|
contentStr := content.Content
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-10-17 17:15:33 +08:00
|
|
|
siteUrlPattern := configService.GetSiteUrl()
|
|
|
|
if strings.Contains(siteUrlPattern, "https") {
|
|
|
|
siteUrlPattern = strings.Replace(siteUrlPattern, "https", "https*", 1)
|
|
|
|
} else {
|
|
|
|
siteUrlPattern = strings.Replace(siteUrlPattern, "http", "https*", 1)
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2017-01-07 15:53:22 +08:00
|
|
|
siteUrlPattern = "(?:" + siteUrlPattern + ")*"
|
|
|
|
|
2015-10-17 17:15:33 +08:00
|
|
|
regImage, _ := regexp.Compile(`<img .*?(src=('|")` + siteUrlPattern + `/(file/outputImage|api/file/getImage)\?fileId=([a-z0-9A-Z]{24})("|'))`)
|
|
|
|
|
|
|
|
findsImage := regImage.FindAllStringSubmatch(contentStr, -1) // 查找所有的
|
|
|
|
// [<img src="http://leanote.com/api/getImage?fileId=3354672e8d38f411286b000069" alt="" width="692" height="302" data-mce-src="http://leanote.com/file/outputImage?fileId=54672e8d38f411286b000069" src="http://leanote.com/file/outputImage?fileId=54672e8d38f411286b000069" " file/outputImage 54672e8d38f411286b000069 "]
|
|
|
|
for _, eachFind := range findsImage {
|
|
|
|
if len(eachFind) == 6 {
|
|
|
|
fileId := eachFind[4]
|
|
|
|
// 得到base64编码文件
|
|
|
|
fileBase64 := fileService.GetImageBase64(noteUserId, fileId)
|
|
|
|
if fileBase64 == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1
|
|
|
|
// src="http://leanote.com/file/outputImage?fileId=54672e8d38f411286b000069"
|
|
|
|
allFixed := strings.Replace(eachFind[0], eachFind[1], "src=\""+fileBase64+"\"", -1)
|
|
|
|
contentStr = strings.Replace(contentStr, eachFind[0], allFixed, -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// markdown
|
|
|
|
if note.IsMarkdown {
|
|
|
|
// 
|
|
|
|
regImageMarkdown, _ := regexp.Compile(`!\[.*?\]\(` + siteUrlPattern + `/(file/outputImage|api/file/getImage)\?fileId=([a-z0-9A-Z]{24})\)`)
|
|
|
|
findsImageMarkdown := regImageMarkdown.FindAllStringSubmatch(contentStr, -1) // 查找所有的
|
|
|
|
for _, eachFind := range findsImageMarkdown {
|
|
|
|
if len(eachFind) == 3 {
|
|
|
|
fileId := eachFind[2]
|
|
|
|
// 得到base64编码文件
|
|
|
|
fileBase64 := fileService.GetImageBase64(noteUserId, fileId)
|
|
|
|
if fileBase64 == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1
|
|
|
|
// src="http://leanote.com/file/outputImage?fileId=54672e8d38f411286b000069"
|
|
|
|
allFixed := ""
|
|
|
|
contentStr = strings.Replace(contentStr, eachFind[0], allFixed, -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if note.Tags != nil && len(note.Tags) > 0 && note.Tags[0] != "" {
|
|
|
|
} else {
|
|
|
|
note.Tags = nil
|
|
|
|
}
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["blog"] = note
|
|
|
|
c.ViewArgs["content"] = contentStr
|
|
|
|
c.ViewArgs["userInfo"] = userInfo
|
2015-10-17 17:15:33 +08:00
|
|
|
userBlog := blogService.GetUserBlog(noteUserId)
|
2017-04-08 18:55:42 +08:00
|
|
|
c.ViewArgs["userBlog"] = userBlog
|
2015-10-17 17:15:33 +08:00
|
|
|
|
|
|
|
return c.RenderTemplate("file/pdf.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 导出成PDF
|
|
|
|
func (c Note) ExportPdf(noteId string) revel.Result {
|
|
|
|
re := info.NewRe()
|
|
|
|
userId := c.GetUserId()
|
|
|
|
note := noteService.GetNoteById(noteId)
|
|
|
|
if note.NoteId == "" {
|
|
|
|
re.Msg = "No Note"
|
|
|
|
return c.RenderText("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
noteUserId := note.UserId.Hex()
|
|
|
|
// 是否有权限
|
|
|
|
if noteUserId != userId {
|
|
|
|
// 是否是有权限协作的
|
|
|
|
if !note.IsBlog && !shareService.HasReadPerm(noteUserId, userId, noteId) {
|
|
|
|
re.Msg = "No Perm"
|
2015-11-28 15:17:36 +08:00
|
|
|
return c.RenderText("No Perm")
|
2015-10-17 17:15:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// path 判断是否需要重新生成之
|
|
|
|
guid := NewGuid()
|
2015-11-28 14:21:23 +08:00
|
|
|
fileUrlPath := "files/export_pdf"
|
2015-10-17 17:15:33 +08:00
|
|
|
dir := revel.BasePath + "/" + fileUrlPath
|
|
|
|
if !MkdirAll(dir) {
|
|
|
|
return c.RenderText("error, no dir")
|
|
|
|
}
|
|
|
|
filename := guid + ".pdf"
|
|
|
|
path := dir + "/" + filename
|
|
|
|
|
|
|
|
// leanote.com的secret
|
|
|
|
appKey, _ := revel.Config.String("app.secretLeanote")
|
|
|
|
if appKey == "" {
|
|
|
|
appKey, _ = revel.Config.String("app.secret")
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-10-17 17:15:33 +08:00
|
|
|
// 生成之
|
|
|
|
binPath := configService.GetGlobalStringConfig("exportPdfBinPath")
|
|
|
|
// 默认路径
|
|
|
|
if binPath == "" {
|
2017-07-27 20:00:53 +08:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
binPath = `C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe`
|
|
|
|
} else {
|
|
|
|
binPath = "/usr/local/bin/wkhtmltopdf"
|
|
|
|
}
|
2015-10-17 17:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
url := configService.GetSiteUrl() + "/note/toPdf?noteId=" + noteId + "&appKey=" + appKey
|
|
|
|
// cc := binPath + " --no-stop-slow-scripts --javascript-delay 10000 \"" + url + "\" \"" + path + "\"" // \"" + cookieDomain + "\" \"" + cookieName + "\" \"" + cookieValue + "\""
|
|
|
|
// cc := binPath + " \"" + url + "\" \"" + path + "\"" // \"" + cookieDomain + "\" \"" + cookieName + "\" \"" + cookieValue + "\""
|
|
|
|
// 等待--window-status为done的状态
|
|
|
|
// http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html
|
|
|
|
// wkhtmltopdf参数大全
|
|
|
|
var cc string
|
2017-07-27 20:00:53 +08:00
|
|
|
// var cc []string
|
|
|
|
var ccWindows []string
|
2015-10-17 17:15:33 +08:00
|
|
|
if note.IsMarkdown {
|
2015-12-04 17:50:14 +08:00
|
|
|
cc = binPath + " --lowquality --window-status done \"" + url + "\" \"" + path + "\"" // \"" + cookieDomain + "\" \"" + cookieName + "\" \"" + cookieValue + "\""
|
2017-07-27 20:00:53 +08:00
|
|
|
// cc = []string{binPath, "--lowquality", "--window-status", "done", "\"" + url + "\"", "\"" + path + "\""}
|
|
|
|
ccWindows = []string{"/C", binPath, "--lowquality", "--window-status", "done", url, path}
|
2015-10-17 17:15:33 +08:00
|
|
|
} else {
|
2015-12-04 17:50:14 +08:00
|
|
|
cc = binPath + " --lowquality \"" + url + "\" \"" + path + "\"" // \"" + cookieDomain + "\" \"" + cookieName + "\" \"" + cookieValue + "\""
|
2017-07-27 20:00:53 +08:00
|
|
|
// cc = []string{binPath, "--lowquality", "\"" + url + "\"", "\"" + path + "\""}
|
|
|
|
ccWindows = []string{"/C", binPath, "--lowquality", url, path}
|
2015-10-17 17:15:33 +08:00
|
|
|
}
|
|
|
|
|
2017-07-27 20:00:53 +08:00
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
|
|
|
// fmt.Println("-------1", runtime.GOOS, ccWindows)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
fmt.Println(ccWindows)
|
|
|
|
// cmd = exec.Command("cmd", ccWindows...)
|
|
|
|
cmd = exec.Command(ccWindows[1], ccWindows[2:]...)
|
|
|
|
} else {
|
|
|
|
fmt.Println(cc)
|
|
|
|
cmd = exec.Command("/bin/sh", "-c", cc)
|
2017-07-27 15:08:34 +08:00
|
|
|
}
|
2015-10-17 17:15:33 +08:00
|
|
|
_, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return c.RenderText("export pdf error. " + fmt.Sprintf("%v", err))
|
|
|
|
}
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return c.RenderText("export pdf error. " + fmt.Sprintf("%v", err))
|
|
|
|
}
|
|
|
|
// http://stackoverflow.com/questions/8588818/chrome-pdf-display-duplicate-headers-received-from-the-server
|
|
|
|
// filenameReturn = strings.Replace(filenameReturn, ",", "-", -1)
|
|
|
|
filenameReturn := note.Title
|
|
|
|
filenameReturn = FixFilename(filenameReturn)
|
|
|
|
if filenameReturn == "" {
|
|
|
|
filenameReturn = "Untitled.pdf"
|
|
|
|
} else {
|
|
|
|
filenameReturn += ".pdf"
|
|
|
|
}
|
|
|
|
return c.RenderBinary(file, filenameReturn, revel.Attachment, time.Now()) // revel.Attachment
|
|
|
|
}
|
|
|
|
|
2014-11-09 22:26:38 +08:00
|
|
|
// 设置/取消Blog; 置顶
|
2015-10-27 23:04:39 +08:00
|
|
|
func (c Note) SetNote2Blog(noteIds []string, isBlog, isTop bool) revel.Result {
|
|
|
|
for _, noteId := range noteIds {
|
|
|
|
noteService.ToBlog(c.GetUserId(), noteId, isBlog, isTop)
|
|
|
|
}
|
2017-04-08 18:55:42 +08:00
|
|
|
return c.RenderJSON(true)
|
2014-11-09 22:26:38 +08:00
|
|
|
}
|