Skip to content

Commit 46326da

Browse files
committed
合并dev
2 parents cbc8f7f + 2384bfe commit 46326da

File tree

99 files changed

+864066
-1036
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+864066
-1036
lines changed

commands/command.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func RegisterModel() {
9999
models.NewSign(),
100100
models.NewBookCounter(),
101101
models.NewDownloadCounter(),
102+
models.NewEbook(),
102103
)
103104
migrate.RegisterMigration()
104105
}
@@ -192,6 +193,13 @@ func RegisterFunction() {
192193
}
193194
return cdn + p
194195
})
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+
})
195203
beego.AddFuncMap("getUsernameByUid", func(id interface{}) string {
196204
return new(models.Member).GetUsernameByUid(id)
197205
})

commands/install.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/TruthHun/BookStack/conf"
1010
"github.com/TruthHun/BookStack/models"
11+
"github.com/TruthHun/BookStack/utils"
1112
"github.com/astaxie/beego"
1213
"github.com/astaxie/beego/orm"
1314
)
@@ -25,9 +26,31 @@ func Install() {
2526
os.Exit(1)
2627
}
2728
initSeo()
29+
resetCategoryUniqueIndex()
30+
migrateEbook()
2831
fmt.Println("Install Successfully!")
2932
os.Exit(0)
33+
}
3034

35+
// 删除分类中的title的唯一索引(兼容旧版本)
36+
func resetCategoryUniqueIndex() {
37+
var indexs []struct {
38+
Table string `orm:"column(Table)"`
39+
NonUnique int `orm:"column(Non_unique)"`
40+
KeyName string `orm:"column(Key_name)"`
41+
ColumnName string `orm:"column(Column_name)"`
42+
}
43+
showIndex := "SHOW INDEX FROM md_category"
44+
dropIndex := "ALTER TABLE `md_category` DROP INDEX `%s`;"
45+
addUniqueIndex := "ALTER TABLE `md_category` ADD UNIQUE( `pid`, `title`);"
46+
o := orm.NewOrm()
47+
o.Raw(showIndex).QueryRows(&indexs)
48+
for _, index := range indexs {
49+
if index.ColumnName == "title" {
50+
o.Raw(fmt.Sprintf(dropIndex, index.KeyName)).Exec()
51+
}
52+
}
53+
o.Raw(addUniqueIndex).Exec()
3154
}
3255

3356
func Version() {
@@ -133,3 +156,43 @@ func initSeo() {
133156
orm.NewOrm().Update(&item, "statement")
134157
}
135158
}
159+
160+
// 电子书数据迁移
161+
func migrateEbook() {
162+
// 1. 查找 md_ebook 表中不存在的书籍的电子书
163+
var (
164+
books []models.Book
165+
sqlQuery = "SELECT book_id,generate_time,book_name,identify,label,description FROM `md_books` WHERE `generate_time`>'2010' and book_id not in (select book_id from md_ebook where book_id>0 group by book_id)"
166+
)
167+
o := orm.NewOrm()
168+
o.Raw(sqlQuery).QueryRows(&books)
169+
if len(books) == 0 {
170+
beego.Info("不存在需要同步的电子书数据")
171+
return
172+
}
173+
exts := []string{".pdf", ".epub", ".mobi"}
174+
for _, book := range books {
175+
beego.Info("迁移书籍电子书:", book.BookName)
176+
var ebooks []models.Ebook
177+
bookPrefix := fmt.Sprintf("/projects/%v/books/%v", book.Identify, book.GenerateTime.Unix())
178+
if utils.StoreType == utils.StoreLocal {
179+
bookPrefix = "/uploads" + bookPrefix
180+
}
181+
ebook := models.Ebook{
182+
Title: book.BookName,
183+
Keywords: book.Label,
184+
Description: beego.Substr(book.Description, 0, 255),
185+
BookID: book.BookId,
186+
Status: models.EBookStatusSuccess,
187+
}
188+
for _, ext := range exts {
189+
ebook.Ext = ext
190+
ebook.Path = bookPrefix + ext
191+
ebooks = append(ebooks, ebook)
192+
}
193+
if _, err := o.InsertMulti(len(ebooks), &ebooks); err != nil {
194+
beego.Error(err)
195+
}
196+
}
197+
beego.Info("电子书数据同步中...")
198+
}

controllers/BaseController.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ func (this *BaseController) Prepare() {
150150
this.Data["CloseSubmitEnter"] = v == "true"
151151
}
152152

153+
if v, ok := this.Option["WIDESCREEN"]; ok {
154+
this.Data["IsWideScreen"] = v == "true"
155+
}
156+
153157
this.Data["SiteName"] = this.Sitename
154158

155159
// 默认显示创建书籍的入口
@@ -212,6 +216,11 @@ func (this *BaseController) SetMember(member models.Member) {
212216

213217
// JsonResult 响应 json 结果
214218
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+
215224
jsonData := make(map[string]interface{}, 3)
216225
jsonData["errcode"] = errCode
217226
jsonData["message"] = errMsg

0 commit comments

Comments
 (0)