Skip to content

Commit 53f8574

Browse files
committed
Merge branch 'ne' into dev-ne
2 parents 24c79c9 + a814d95 commit 53f8574

27 files changed

+1418
-135
lines changed

commands/command.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ func RegisterFunction() {
193193
}
194194
return cdn + p
195195
})
196+
beego.AddFuncMap("parseICON", func(icon string) string {
197+
icon = strings.TrimSpace(icon)
198+
if strings.Contains(icon, "fa-") {
199+
return fmt.Sprintf(`<i class="%s"></i>`, icon)
200+
}
201+
return fmt.Sprintf(`<img src="%s" class="icon-img" alt="icon"/>`, icon)
202+
})
196203
beego.AddFuncMap("getUsernameByUid", func(id interface{}) string {
197204
return new(models.Member).GetUsernameByUid(id)
198205
})

controllers/BaseController.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ func (this *BaseController) SetMember(member models.Member) {
216216

217217
// JsonResult 响应 json 结果
218218
func (this *BaseController) JsonResult(errCode int, errMsg string, data ...interface{}) {
219+
if !this.Ctx.Input.IsAjax() {
220+
this.Data["Message"] = errMsg
221+
this.Abort("404")
222+
}
223+
219224
jsonData := make(map[string]interface{}, 3)
220225
jsonData["errcode"] = errCode
221226
jsonData["message"] = errMsg

controllers/BookController.go

Lines changed: 112 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313
"regexp"
14+
"sort"
1415
"strconv"
1516
"strings"
1617
"time"
@@ -195,7 +196,6 @@ func (this *BookController) Setting() {
195196

196197
// SaveBook 保存书籍信息
197198
func (this *BookController) SaveBook() {
198-
199199
bookResult, err := this.IsPermission()
200200
if err != nil {
201201
this.JsonResult(6001, err.Error())
@@ -239,6 +239,7 @@ func (this *BookController) SaveBook() {
239239
book.Lang = this.GetString("lang")
240240
book.AdTitle = this.GetString("ad_title")
241241
book.AdLink = this.GetString("ad_link")
242+
_, book.NavJSON = this.parseBookNav()
242243

243244
if err := book.Update(); err != nil {
244245
this.JsonResult(6006, "保存失败")
@@ -272,6 +273,47 @@ func (this *BookController) SaveBook() {
272273
this.JsonResult(0, "ok", bookResult)
273274
}
274275

276+
func (this *BookController) parseBookNav() (navs models.BookNavs, navStr string) {
277+
var data struct {
278+
Name []string `json:"name"`
279+
URL []string `json:"url"`
280+
Sort []string `json:"sort"`
281+
Icon []string `json:"icon"`
282+
Color []string `json:"color"`
283+
Target []string `json:"target"`
284+
}
285+
b, _ := json.Marshal(this.Ctx.Request.PostForm)
286+
json.Unmarshal(b, &data)
287+
lenName := len(data.Name)
288+
if lenName == 0 || lenName != len(data.URL) || lenName != len(data.Sort) || lenName != len(data.Icon) || lenName != len(data.Color) || lenName != len(data.Target) {
289+
return
290+
}
291+
292+
for idx, name := range data.Name {
293+
name = strings.TrimSpace(name)
294+
url := strings.TrimSpace(data.URL[idx])
295+
if url == "" || name == "" {
296+
continue
297+
}
298+
nav := models.BookNav{
299+
Name: name,
300+
URL: url,
301+
Color: strings.TrimSpace(data.Color[idx]),
302+
Icon: strings.TrimSpace(data.Icon[idx]),
303+
Target: strings.TrimSpace(data.Target[idx]),
304+
}
305+
nav.Sort, _ = strconv.Atoi(strings.TrimSpace(data.Sort[idx]))
306+
navs = append(navs, nav)
307+
}
308+
if len(navs) > 0 {
309+
// 排序
310+
sort.Sort(navs)
311+
b, _ := json.Marshal(navs)
312+
navStr = string(b)
313+
}
314+
return
315+
}
316+
275317
//设置书籍私有状态.
276318
func (this *BookController) PrivatelyOwned() {
277319

@@ -610,6 +652,47 @@ func (this *BookController) Create() {
610652
this.JsonResult(0, "ok", bookResult)
611653
}
612654

655+
// Create 创建书籍.
656+
func (this *BookController) Copy() {
657+
if opt, err := models.NewOption().FindByKey("ALL_CAN_WRITE_BOOK"); err == nil {
658+
if opt.OptionValue == "false" && this.Member.Role == conf.MemberGeneralRole { // 读者无权限创建书籍
659+
this.JsonResult(1, "普通读者无法创建书籍,如需创建书籍,请向管理员申请成为作者")
660+
}
661+
}
662+
identify := strings.TrimSpace(this.GetString("identify", ""))
663+
sourceIdentify := strings.TrimSpace(this.GetString("source_identify", ""))
664+
sourceBook, err := models.NewBook().FindByIdentify(sourceIdentify)
665+
if err != nil {
666+
this.JsonResult(1, err.Error())
667+
}
668+
existBook, _ := models.NewBook().FindByIdentify(identify, "book_id")
669+
if existBook != nil && existBook.BookId > 0 {
670+
this.JsonResult(1, "请更换新的书籍标识")
671+
}
672+
673+
// 如果是私有书籍,且不是团队的人,不允许拷贝该项目
674+
if sourceBook.PrivatelyOwned == 1 {
675+
rel, err := models.NewRelationship().FindByBookIdAndMemberId(sourceBook.BookId, this.Member.MemberId)
676+
if err != nil || rel == nil || rel.RelationshipId == 0 {
677+
this.JsonResult(1, "无拷贝书籍权限")
678+
}
679+
}
680+
sourceBook.BookId = 0
681+
sourceBook.BookName = strings.TrimSpace(this.GetString("book_name", ""))
682+
sourceBook.Identify = identify
683+
sourceBook.Description = strings.TrimSpace(this.GetString("description", ""))
684+
sourceBook.Author = strings.TrimSpace(this.GetString("author", ""))
685+
sourceBook.AuthorURL = strings.TrimSpace(this.GetString("author_url", ""))
686+
sourceBook.PrivatelyOwned, _ = strconv.Atoi(this.GetString("privately_owned"))
687+
sourceBook.MemberId = this.Member.MemberId
688+
err = sourceBook.Copy(sourceIdentify)
689+
if err != nil {
690+
this.JsonResult(1, "拷贝书籍失败:"+err.Error())
691+
}
692+
693+
this.JsonResult(0, "拷贝书籍成功")
694+
}
695+
613696
// CreateToken 创建访问来令牌.
614697
func (this *BookController) CreateToken() {
615698
if this.forbidGeneralRole() {
@@ -702,6 +785,7 @@ func (this *BookController) Delete() {
702785
// 发布书籍.
703786
func (this *BookController) Release() {
704787
identify := this.GetString("identify")
788+
force, _ := this.GetBool("force")
705789
bookId := 0
706790
if this.Member.IsAdministrator() {
707791
book, err := models.NewBook().FindByFieldFirst("identify", identify)
@@ -727,13 +811,7 @@ func (this *BookController) Release() {
727811
bookId = book.BookId
728812
}
729813

730-
if exist := utils.BooksRelease.Exist(bookId); exist {
731-
this.JsonResult(1, "上次内容发布正在执行中,请稍后再操作")
732-
}
733-
734-
go func(identify string) {
735-
models.NewDocument().ReleaseContent(bookId, this.BaseUrl())
736-
}(identify)
814+
go models.NewDocument().ReleaseContent(bookId, this.BaseUrl(), force)
737815

738816
this.JsonResult(0, "发布任务已推送到任务队列,稍后将在后台执行。")
739817
}
@@ -1262,3 +1340,29 @@ func (this *BookController) Comment() {
12621340
}
12631341
this.JsonResult(1, "书籍不存在")
12641342
}
1343+
1344+
// ExportMarkdown 将书籍导出为markdown
1345+
// 注意:系统管理员和书籍参与者有权限导出
1346+
func (this *BookController) Export2Markdown() {
1347+
identify := this.GetString("identify")
1348+
if this.Member.MemberId == 0 {
1349+
this.JsonResult(1, "请先登录")
1350+
}
1351+
if !this.Member.IsAdministrator() {
1352+
if _, err := models.NewBookResult().FindByIdentify(identify, this.Member.MemberId); err != nil {
1353+
this.JsonResult(1, "无操作权限")
1354+
}
1355+
}
1356+
path, err := models.NewBook().Export2Markdown(identify)
1357+
if err != nil {
1358+
this.JsonResult(1, err.Error())
1359+
}
1360+
defer func() {
1361+
os.Remove(path)
1362+
}()
1363+
attchmentName := filepath.Base(path)
1364+
if book, _ := models.NewBook().FindByIdentify(identify, "book_name", "book_id"); book != nil && book.BookId > 0 {
1365+
attchmentName = book.BookName + ".zip"
1366+
}
1367+
this.Ctx.Output.Download(strings.TrimLeft(path, "./"), attchmentName)
1368+
}

controllers/DocumentController.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ func (this *DocumentController) Create() {
568568
parentId, _ := this.GetInt("parent_id", 0)
569569
docId, _ := this.GetInt("doc_id", 0)
570570
bookIdentify := strings.TrimSpace(this.GetString(":key"))
571+
571572
o := orm.NewOrm()
572573

573574
if identify == "" {
@@ -577,7 +578,6 @@ func (this *DocumentController) Create() {
577578
this.JsonResult(6004, "文档名称不能为空")
578579
}
579580
if docIdentify != "" {
580-
581581
if ok, err := regexp.MatchString(`^[a-zA-Z0-9_\-\.]*$`, docIdentify); !ok || err != nil {
582582
this.JsonResult(6003, "文档标识只能是数字、字母,以及“-”、“_”和“.”等字符,并且不能是纯数字")
583583
}
@@ -600,7 +600,7 @@ func (this *DocumentController) Create() {
600600
this.JsonResult(6006, "文档标识已被使用")
601601
}
602602
} else {
603-
docIdentify = fmt.Sprintf("date-%v", time.Now().Format("2006.01.02.15.04.05"))
603+
docIdentify = fmt.Sprintf("date%v", time.Now().Format("20060102150405.md"))
604604
}
605605

606606
bookId := 0
@@ -633,6 +633,7 @@ func (this *DocumentController) Create() {
633633

634634
document.MemberId = this.Member.MemberId
635635
document.BookId = bookId
636+
oldIdentify := document.Identify
636637
if docIdentify != "" {
637638
document.Identify = docIdentify
638639
}
@@ -653,6 +654,9 @@ func (this *DocumentController) Create() {
653654
beego.Error(err)
654655
}
655656
}
657+
if docId > 0 && oldIdentify != docIdentify {
658+
go document.ReplaceIdentify(bookId, oldIdentify, docIdentify)
659+
}
656660
this.JsonResult(0, "ok", document)
657661
}
658662

@@ -1241,7 +1245,7 @@ func (this *DocumentController) ExportOld() {
12411245
book, err := new(models.Book).FindByIdentify(identify)
12421246
if err != nil {
12431247
beego.Error(err.Error())
1244-
this.JsonResult(1, "下载失败,您要下载的文档当前并未生成可下载文档。")
1248+
this.JsonResult(1, "下载失败,您要下载的书籍当前并未生成可下载的电子书。")
12451249
}
12461250
if book.PrivatelyOwned == 1 && this.Member.MemberId != book.MemberId {
12471251
this.JsonResult(1, "私有文档,只有文档创建人可导出")
@@ -1253,14 +1257,14 @@ func (this *DocumentController) ExportOld() {
12531257
case utils.StoreOss:
12541258
if err := store.ModelStoreOss.IsObjectExist(obj); err != nil {
12551259
beego.Error(err, obj)
1256-
this.JsonResult(1, "下载失败,您要下载的文档当前并未生成可下载文档。")
1260+
this.JsonResult(1, "下载失败,您要下载的书籍当前并未生成可下载的电子书。")
12571261
}
12581262
link = this.OssDomain + "/" + obj
12591263
case utils.StoreLocal:
12601264
obj = "uploads/" + obj
12611265
if err := store.ModelStoreLocal.IsObjectExist(obj); err != nil {
12621266
beego.Error(err, obj)
1263-
this.JsonResult(1, "下载失败,您要下载的文档当前并未生成可下载文档。")
1267+
this.JsonResult(1, "下载失败,您要下载的书籍当前并未生成可下载的电子书。")
12641268
}
12651269
link = "/" + obj
12661270
}
@@ -1276,7 +1280,7 @@ func (this *DocumentController) ExportOld() {
12761280
}
12771281
this.JsonResult(0, "获取文档下载链接成功", map[string]interface{}{"url": link})
12781282
}
1279-
this.JsonResult(1, "下载失败,您要下载的文档当前并未生成可下载文档。")
1283+
this.JsonResult(1, "下载失败,您要下载的书籍当前并未生成可下载的电子书。")
12801284
}
12811285

12821286
//导出文件

controllers/StaticController.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func (this *StaticController) APP() {
3232
this.Abort("404")
3333
}
3434

35+
func (this *StaticController) Test() {
36+
// fmt.Println(models.NewBook().Export2Markdown("4b98937398e612778711608a3d09a9c4"))
37+
this.Ctx.WriteString("this is a test")
38+
}
39+
3540
// Uploads 查询上传的静态资源。
3641
// 如果是音频和视频文件,需要根据后台设置而判断是否加密处理
3742
// 如果使用了OSS存储,则需要将文件处理好

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ replace github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8 => github.
66

77
require (
88
github.com/PuerkitoBio/goquery v1.6.0
9-
github.com/TruthHun/converter v0.0.0-20210616133600-fb06ddba4a35
9+
github.com/TruthHun/converter v0.0.0-20210623150616-172c168cdcda
1010
github.com/TruthHun/gotil v0.0.0-20210509080330-2470bb9be7cd
1111
github.com/TruthHun/html2article v0.0.0-20180202140721-67d6ff09647b
1212
github.com/TruthHun/html2json v0.0.0-20201228115506-dee0570dffa8

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github.com/TruthHun/converter v0.0.0-20180618131031-80268d67d7a8 h1:E+006K+eHyJk
1111
github.com/TruthHun/converter v0.0.0-20180618131031-80268d67d7a8/go.mod h1:bW/ArSdu0OB6XR3NcPdPBIEeGdQ1GLW9fQavW3Auy2A=
1212
github.com/TruthHun/converter v0.0.0-20210616133600-fb06ddba4a35 h1:g1zMxNHQVGA6LqlJ0r923Xft+GO6TXU399k/HBILpHM=
1313
github.com/TruthHun/converter v0.0.0-20210616133600-fb06ddba4a35/go.mod h1:n0OITYsMcCW3XbBVOBn3jMiXRuCmDwDXPsumL7D/JBY=
14+
github.com/TruthHun/converter v0.0.0-20210623150616-172c168cdcda h1:5J7hfnVImIf2MsBp7NZ+YrmcSrKUy5rW5sonkMxHkfY=
15+
github.com/TruthHun/converter v0.0.0-20210623150616-172c168cdcda/go.mod h1:n0OITYsMcCW3XbBVOBn3jMiXRuCmDwDXPsumL7D/JBY=
1416
github.com/TruthHun/gotil v0.0.0-20191003091818-17b80aad8a45 h1:o+hC+QvRJlpj6meIX4AC/LKi7UxdEWFs+6zOsLzGoBs=
1517
github.com/TruthHun/gotil v0.0.0-20191003091818-17b80aad8a45/go.mod h1:0V5BlPJa24R0PzuXVURUfjaQ8Z7cmqsVSW7ftuhesN0=
1618
github.com/TruthHun/gotil v0.0.0-20210509080330-2470bb9be7cd h1:WHNNuPS7Pc4rYhlN2mAPT4lXgGYNwX4tws8+DbfYSJY=

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66

77
"github.com/TruthHun/BookStack/utils"
8+
"github.com/astaxie/beego"
89

910
"github.com/TruthHun/BookStack/commands"
1011
"github.com/TruthHun/BookStack/commands/daemon"
@@ -14,6 +15,8 @@ import (
1415
)
1516

1617
func main() {
18+
// https://beego.me/docs/mvc/controller/config.md#app-%E9%85%8D%E7%BD%AE
19+
beego.BConfig.MaxMemory = 1 << 26 * 1024 // 设置最大内存,64GB
1720
if len(os.Args) >= 3 && os.Args[1] == "service" {
1821
if os.Args[2] == "install" {
1922
daemon.Install()

0 commit comments

Comments
 (0)