Skip to content

Commit e236ce6

Browse files
committed
合并dev分支
2 parents aa2469e + 03880e9 commit e236ce6

File tree

11 files changed

+216
-72
lines changed

11 files changed

+216
-72
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
4242
> 提示:DocHub移动端模板已经放出来,[链接地址](#tpl-mobile)
4343
44+
> 说明(于2018-06-08):`DocHub`项目是我学完Go语言之后的第二个独立开发的Go语言项目,项目是在2016年12月份开始开发的。当时对Go语言和Beego框架都还不怎么熟。`DocHub`项目开源出来之后,看到`star`飙升的有点快(相对个人之前的开源项目),非常感谢大家的认可和厚爱,然后自己诚惶诚恐地认真review了一遍代码...
45+
路由在模板页面写死了...
46+
数据表一些数据字段也出现了冗余...
47+
有些函数和方法封装,难以使用的一塌糊涂...
48+
请大家给我点时间,我把整套程序都在兼容现有的程序数据的基础上推倒重做的,当然,现在的版本
49+
50+
4451
<a name="intro"></a>
4552
## 文库简介
4653

conf/app.conf.example

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ SessionName=dochub
2121
# 静态文件扩展名【注意:不要把.conf设置为扩展名,以避免泄露数据库账号密码】
2222
StaticExt=.txt,.html,.ico,.jpeg,.png,.gif
2323

24+
25+
2426
cookieSecret=sfgowtrfgjqwetryfqef8sd
2527

2628
#允许上传的文件大小(单位:字节),默认50MB=52428800
@@ -41,8 +43,8 @@ UrlExpire=60
4143
#libreoffice的可执行文件路径(注意,windows下要用\\代替原路径的\)
4244
#如:“C:\Program Files (x86)\LibreOffice 4\program\soffice”要换成“C:\\Program Files (x86)\\LibreOffice 4\\program\\soffice”
4345
#soffice=C:\\Program Files (x86)\\LibreOffice 5\\program\\soffice
44-
soffice=/Applications/LibreOffice.app/Contents/MacOS/soffice
45-
#soffice=soffice
46+
#soffice=/Applications/LibreOffice.app/Contents/MacOS/soffice
47+
soffice=soffice
4648

4749
#imagick命令行
4850
imagick=convert
@@ -53,6 +55,9 @@ pdf2svg=pdf2svg
5355
#calibre转化命令[下一版本再加上这个配置项]
5456
calibre=ebook-convert
5557

58+
#提取PDF文本内容:`apt install poppler-utils`
59+
pdftotext=pdftotext
60+
5661
# 是否压缩svg(建议压缩)
5762
compressSvg=true
5863

controllers/HomeControllers/BaseController.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (this *BaseController) Prepare() {
5353
if beego.AppConfig.String("runmode") != "prod" {
5454
version = fmt.Sprintf("v%v.%v", version, time.Now().Unix())
5555
}
56-
//this.Sys, _ = models.ModelSys.Get()
57-
this.Sys = models.GlobalSys
56+
this.Sys, _ = models.ModelSys.Get()
57+
//this.Sys = models.GlobalSys//暂时先不用这个,先每次都从数据库查询
5858
this.Data["Version"] = version
5959
this.Data["Sys"] = this.Sys
6060
this.Data["PreviewDomain"] = beego.AppConfig.String("oss::PreviewUrl")

controllers/HomeControllers/SearchController.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package HomeControllers
33
import (
44
"strings"
55

6+
"time"
7+
68
"github.com/TruthHun/DocHub/helper"
79
"github.com/TruthHun/DocHub/helper/conv"
810
"github.com/TruthHun/DocHub/models"
@@ -17,6 +19,8 @@ func (this *SearchController) Get() {
1719
var (
1820
p int = 1 //默认页码
1921
listRows int = 10 //默认每页显示记录数
22+
res models.Result
23+
start = time.Now().UnixNano()
2024
)
2125
//path中的参数
2226
params := conv.Path2Map(this.GetString(":splat"))
@@ -47,32 +51,31 @@ func (this *SearchController) Get() {
4751

4852
//页码处理
4953
p = helper.NumberRange(p, 1, 100)
50-
res := models.Search(params["wd"], params["type"], params["sort"], p, listRows, 1)
51-
if res.Total > 0 && len(res.Ids) > 0 {
52-
data := models.ModelDoc.GetDocsByIds(res.Ids)
53-
if len(data) > 0 {
54-
for index, val := range data {
55-
if len(strings.TrimSpace(val["Description"].(string))) == 0 {
56-
if desc := models.ModelDocText.GetDescByMd5(val["Md5"], 120); len(desc) == 0 {
57-
data[index]["Description"] = val["Title"]
58-
} else {
59-
data[index]["Description"] = desc + "..."
60-
}
61-
}
62-
}
63-
}
64-
this.Data["Data"] = data
54+
//res := models.Search(params["wd"], params["type"], params["sort"], p, listRows, 1)
55+
this.Data["Data"], res.TotalFound = models.SearchByMysql(params["wd"], params["type"], params["sort"], p, listRows)
56+
res.Word = []string{params["wd"]}
57+
if res.TotalFound > 1000 {
58+
res.Total = 1000
59+
} else {
60+
res.Total = res.TotalFound
6561
}
66-
6762
if p == 1 {
6863
wdSlice := strings.Split(this.Sys.DirtyWord, " ")
64+
insert := true
6965
for _, wd := range wdSlice {
70-
if !strings.Contains(params["wd"], wd) {
71-
models.ReplaceInto(models.TableSearchLog, map[string]interface{}{"Wd": params["wd"]})
66+
//如果含有非法关键字,则不录入搜索词库
67+
if wd != "" && strings.Contains(params["wd"], wd) {
68+
insert = false
7269
break
7370
}
7471
}
72+
if insert {
73+
models.ReplaceInto(models.TableSearchLog, map[string]interface{}{"Wd": params["wd"]})
74+
}
7575
}
76+
end := time.Now().UnixNano()
77+
res.Time = float64(end-start) / 1000000000
78+
7679
this.Data["Seo"] = models.ModelSeo.GetByPage("PC-Search", params["wd"], "文档搜索,"+params["wd"], "文档搜索,"+params["wd"], this.Sys.Site)
7780
this.Data["Page"] = helper.Paginations(6, int(res.Total), listRows, p, "/search/", "type", params["type"], "sort", params["sort"], "p", p, "wd", params["wd"])
7881
this.Data["Params"] = params

helper/helper.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ import (
4242
)
4343

4444
var (
45-
Debug = false
45+
Debug = beego.AppConfig.String("runmode") == "dev"
4646
StaticExt = make(map[string]bool)
4747
)
4848

4949
func init() {
50-
Debug = beego.AppConfig.String("runmode") == "dev"
5150
exts := strings.Split(beego.AppConfig.String("StaticExt"), ",")
5251
for _, ext := range exts {
5352
StaticExt[strings.ToLower(strings.TrimSpace(ext))] = true
@@ -784,3 +783,27 @@ func UpperFirst(str string) string {
784783
}
785784
return str
786785
}
786+
787+
//获取PDF中指定页面的文本内容
788+
//@param file PDF文件
789+
//@param from 起始页
790+
//@param to 截止页
791+
func ExtractPdfText(file string, from, to int) (content string) {
792+
pdftotext := beego.AppConfig.DefaultString("pdftotext", "pdftotext")
793+
textfile := file + ".txt"
794+
defer os.Remove(textfile)
795+
args := []string{"-f", strconv.Itoa(from), "-l", strconv.Itoa(to), file, textfile}
796+
if err := exec.Command(pdftotext, args...).Run(); err != nil {
797+
Logger.Error(err.Error())
798+
} else {
799+
if b, err := ioutil.ReadFile(textfile); err == nil {
800+
content = string(b)
801+
content = strings.Replace(content, "\t", " ", -1)
802+
content = strings.Replace(content, "\n", " ", -1)
803+
content = strings.Replace(content, "\r", " ", -1)
804+
} else {
805+
Logger.Error(err.Error())
806+
}
807+
}
808+
return
809+
}

helper/logs.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package helper
22

33
import (
4-
"github.com/astaxie/beego"
4+
"os"
5+
6+
"fmt"
7+
58
"github.com/astaxie/beego/logs"
69
)
710

@@ -10,10 +13,18 @@ var Logger = logs.NewLogger()
1013

1114
//日志初始化
1215
func InitLogs() {
16+
//创建日志目录
17+
if _, err := os.Stat("logs"); err != nil {
18+
os.Mkdir("logs", os.ModePerm)
19+
}
20+
var level = 7
21+
if !Debug {
22+
level = 4
23+
}
1324
//初始化日志各种配置
14-
LogsConf := `{"filename":"logs/dochub.log","level":7,"maxlines":5000,"maxsize":0,"daily":true,"maxdays":15}`
25+
LogsConf := fmt.Sprintf(`{"filename":"logs/dochub.log","level":%v,"maxlines":5000,"maxsize":0,"daily":true,"maxdays":7}`, level)
1526
Logger.SetLogger(logs.AdapterFile, LogsConf)
16-
if beego.AppConfig.String("runmode") == "dev" {
27+
if Debug {
1728
Logger.SetLogger("console")
1829
} else {
1930
//是否异步输出日志

main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import (
1515

1616
//初始化函数
1717
func init() {
18+
fmt.Println("")
19+
fmt.Println("Powered By DocHub")
20+
fmt.Println("Author:进击的皇虫([email protected])")
21+
fmt.Println("")
22+
1823
//数据库初始化
1924
models.Init()
2025
//初始化日志
@@ -49,10 +54,6 @@ func init() {
4954
}
5055

5156
func main() {
52-
fmt.Println("")
53-
fmt.Println("Powered By DocHub")
54-
fmt.Println("Author:进击的皇虫([email protected])")
55-
fmt.Println("")
5657
//定义错误和异常处理控制器
5758
beego.ErrorController(&HomeControllers.BaseController{})
5859
beego.Run()

models/Install.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ package models
33
import (
44
"time"
55

6+
"io/ioutil"
7+
"os"
8+
69
"github.com/TruthHun/DocHub/helper"
710
"github.com/astaxie/beego"
811
)
912

1013
func install() {
11-
//数据初始化,如果数据已经存在,则不会继续写入(因为数据已存在,继续写入会报错,所以没影响)
12-
installAdmin()
13-
installCategory()
14-
installFriendlinks()
15-
installPages()
16-
installSeo()
17-
installSys()
14+
lockfile := "conf/install.lock"
15+
if _, err := os.Stat(lockfile); err != nil {
16+
//数据初始化,如果数据已经存在,则不会继续写入(因为数据已存在,继续写入会报错,所以没影响)
17+
installAdmin()
18+
installCategory()
19+
installFriendlinks()
20+
installPages()
21+
installSeo()
22+
installSys()
23+
ioutil.WriteFile(lockfile, []byte(""), os.ModePerm)
24+
}
25+
1826
}
1927

2028
//安装管理员初始数据
@@ -50,14 +58,16 @@ func installSys() {
5058
<p>您好,非常感谢您对DocHub文库(<a href="https://github.com/TruthHun/DocHub" target="_blank" title="DocHub文库">DocHub</a>)的关注和热爱</p>
5159
<p>您本次申请找回密码的邮箱验证码是: <strong style="font-size: 30px;color: red;">{code}</strong></p>
5260
<p>如果非您本人操作,请忽略该邮件。</p>`,
53-
HomeCates: "5,2,1,3,4",
54-
Trends: "1,2,3,4,5",
55-
Site: "DocHub(多哈)文库",
56-
Reward: 5,
57-
Sign: 5,
58-
Question: "DocHub文库的中文名是什么?",
59-
Answer: "多哈",
60-
ListRows: 10,
61+
HomeCates: "5,2,1,3,4",
62+
Trends: "1,2,3,4,5",
63+
Site: "DocHub(多哈)文库",
64+
Reward: 5,
65+
Sign: 5,
66+
Question: "DocHub文库的中文名是什么?",
67+
Answer: "多哈",
68+
ListRows: 10,
69+
TimeExpireHotspot: 604800,
70+
TimeExpireRelate: 604800,
6171
}
6272
beego.Info("初始化系统数据")
6373
if _, _, err := O.ReadOrCreate(&sys, "Id"); err != nil {
@@ -115,6 +125,12 @@ func installFriendlinks() {
115125
//安装单页初始数据
116126
//存在唯一索引Alias,已存在的数据不会继续写入
117127
func installPages() {
128+
//存在单页了,则表明已经初始化过数据
129+
var page = new(Pages)
130+
if O.QueryTable(page).Filter("id__gt", 0).One(page); page.Id > 0 {
131+
return
132+
}
133+
118134
now := int(time.Now().Unix())
119135
var pages = []Pages{
120136
Pages{
@@ -268,6 +284,12 @@ func installSeo() {
268284
//安装分类初始数据
269285
//带有主键id数据的初始化,如果已经存在数据,则不会继续写入
270286
func installCategory() {
287+
//存在分类了,则表明已经初始化过数据
288+
var cate = new(Category)
289+
if O.QueryTable(cate).Filter("id__gt", 0).One(cate); cate.Id > 0 {
290+
return
291+
}
292+
271293
sql := `INSERT INTO hc_category (Id, Pid, Title, Cnt, Sort, Alias, Status) VALUES
272294
(1, 0, '教育频道', 0, 0, 'edu', 1),
273295
(2, 0, '专业资料', 0, 1, 'pro', 1),

0 commit comments

Comments
 (0)