This commit is contained in:
deepzz0
2016-10-03 15:51:54 +08:00
parent abda7b511e
commit 1ff66bdd16
8 changed files with 125 additions and 143 deletions

110
api.go
View File

@@ -152,12 +152,12 @@ func apiPostAdd(c *gin.Context) {
var cid int var cid int
defer func() { defer func() {
if !publish { if !publish {
// {"success":1,"time":"10:40:46 AM","cid":"4"}
if err == nil { if err == nil {
c.JSON(http.StatusOK, gin.H{"success": SUCCESS, "time": time.Now().Format("15:04:05 PM"), "cid": cid}) c.JSON(http.StatusOK, gin.H{"success": SUCCESS, "time": time.Now().Format("15:04:05 PM"), "cid": cid})
return } else {
logd.Error(err)
c.JSON(http.StatusOK, gin.H{"fail": FAIL, "time": time.Now().Format("15:04:05 PM"), "cid": cid})
} }
logd.Error(err)
} }
if err == nil { if err == nil {
c.Redirect(http.StatusFound, "/admin/manage-posts") c.Redirect(http.StatusFound, "/admin/manage-posts")
@@ -183,17 +183,18 @@ func apiPostAdd(c *gin.Context) {
t := CheckDate(date) t := CheckDate(date)
serieid := CheckSerieID(serie) serieid := CheckSerieID(serie)
publish = CheckPublish(do) publish = CheckPublish(do)
if cid, err = strconv.Atoi(c.PostForm("cid")); err != nil || cid < 1 { artc := &Article{
artc := &Article{ Title: title,
Title: title, Content: text,
Content: text, Slug: slug,
Slug: slug, CreateTime: t,
CreateTime: t, IsDraft: !publish,
IsDraft: !publish, Author: Ei.Username,
Author: Ei.Username, SerieID: serieid,
SerieID: serieid, Tags: tags,
Tags: tags, }
} cid, err = strconv.Atoi(c.PostForm("cid"))
if err != nil || cid < 1 {
err = AddArticle(artc) err = AddArticle(artc)
if err != nil { if err != nil {
logd.Error(err) logd.Error(err)
@@ -203,53 +204,44 @@ func apiPostAdd(c *gin.Context) {
if publish { if publish {
ElasticIndex(artc) ElasticIndex(artc)
} }
} else { return
artc := QueryArticle(int32(cid)) }
if artc == nil { artc.ID = int32(cid)
err = errors.New("没有发现该文章") if CheckBool(c.PostForm("update")) {
return
}
if publish {
if Ei.MapArticles[artc.Slug] != nil {
i, a := GetArticle(artc.ID)
DelFromLinkedList(a)
Ei.Articles[i] = artc
if a.SerieID != serieid {
ManageSeriesArticle(a, false, DELETE)
}
if strings.Join(a.Tags, ",") != tag {
ManageTagsArticle(a, false, DELETE)
}
ManageArchivesArticle(a, false, DELETE)
} else {
Ei.Articles = append(Ei.Articles, artc)
artc.IsDraft = false
}
Ei.MapArticles[artc.Slug] = artc
}
artc.Title = title
artc.Slug = slug
artc.Content = text
artc.CreateTime = t
artc.UpdateTime = time.Now() artc.UpdateTime = time.Now()
artc.SerieID = serieid }
artc.Tags = tags i, a := GetArticle(artc.ID)
err = UpdateArticle(bson.M{"id": artc.ID}, artc) if a != nil {
if err != nil { artc.IsDraft = false
logd.Error(err) artc.Count = a.Count
return artc.UpdateTime = a.UpdateTime
}
err = UpdateArticle(bson.M{"id": artc.ID}, artc)
if err != nil {
logd.Error(err)
return
}
if !artc.IsDraft {
if a != nil {
Ei.Articles = append(Ei.Articles[0:i], Ei.Articles[i+1:]...)
DelFromLinkedList(a)
ManageTagsArticle(a, false, DELETE)
ManageSeriesArticle(a, false, DELETE)
ManageArchivesArticle(a, false, DELETE)
delete(Ei.MapArticles, a.Slug)
a = nil
} }
if publish { Ei.MapArticles[artc.Slug] = artc
sort.Sort(Ei.Articles) Ei.Articles = append(Ei.Articles, artc)
GenerateExcerptAndRender(artc) sort.Sort(Ei.Articles)
// elasticsearch 索引 GenerateExcerptAndRender(artc)
ElasticIndex(artc) // elasticsearch 索引
if artc.ID >= setting.Conf.StartID { ElasticIndex(artc)
ManageTagsArticle(artc, true, ADD) if artc.ID >= setting.Conf.StartID {
ManageSeriesArticle(artc, true, ADD) ManageTagsArticle(artc, true, ADD)
ManageArchivesArticle(artc, true, ADD) ManageSeriesArticle(artc, true, ADD)
AddToLinkedList(artc.ID) ManageArchivesArticle(artc, true, ADD)
} AddToLinkedList(artc.ID)
} }
} }
} }

View File

@@ -40,7 +40,7 @@ func CheckSerieID(sid string) int32 {
} }
func CheckBool(str string) bool { func CheckBool(str string) bool {
return str == "true" return str == "true" || str == "1"
} }
func CheckPublish(do string) bool { func CheckPublish(do string) bool {

View File

@@ -13,7 +13,7 @@ pagesize: 20
# 自动截取预览, 字符数 # 自动截取预览, 字符数
length: 200 length: 200
# 截取预览标识 # 截取预览标识
identifier: <!-- more --> identifier: <!--more-->
# favicon # favicon
favicon: //st.deepzz.com/static/img/favicon.ico favicon: //st.deepzz.com/static/img/favicon.ico
# 起始ID预留id不时之需, 不用管 # 起始ID预留id不时之需, 不用管

146
db.go
View File

@@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"sort" "sort"
// "strings"
"sync" "sync"
"time" "time"
@@ -262,24 +261,50 @@ func PageList(p, n int) (prev int, next int, artcs []*Article) {
return return
} }
func ManageTagsArticle(artc *Article, s bool, dos ...string) { func ManageTagsArticle(artc *Article, s bool, do string) {
for _, do := range dos { switch do {
switch do { case ADD:
case ADD: for _, tag := range artc.Tags {
for _, tag := range artc.Tags { Ei.Tags[tag] = append(Ei.Tags[tag], artc)
Ei.Tags[tag] = append(Ei.Tags[tag], artc) if s {
if s { sort.Sort(Ei.Tags[tag])
sort.Sort(Ei.Tags[tag]) }
}
case DELETE:
for _, tag := range artc.Tags {
for i, v := range Ei.Tags[tag] {
if v == artc {
Ei.Tags[tag] = append(Ei.Tags[tag][0:i], Ei.Tags[tag][i+1:]...)
if len(Ei.Tags[tag]) == 0 {
delete(Ei.Tags, tag)
}
return
} }
} }
case DELETE: }
for _, tag := range artc.Tags { }
for i, v := range Ei.Tags[tag] { }
func ManageSeriesArticle(artc *Article, s bool, do string) {
switch do {
case ADD:
for i, serie := range Ei.Series {
if serie.ID == artc.SerieID {
Ei.Series[i].Articles = append(Ei.Series[i].Articles, artc)
if s {
sort.Sort(Ei.Series[i].Articles)
Ei.CH <- SERIES_MD
return
}
}
}
case DELETE:
for i, serie := range Ei.Series {
if serie.ID == artc.SerieID {
for j, v := range serie.Articles {
if v == artc { if v == artc {
Ei.Tags[tag] = append(Ei.Tags[tag][0:i], Ei.Tags[tag][i+1:]...) Ei.Series[i].Articles = append(Ei.Series[i].Articles[0:j], Ei.Series[i].Articles[j+1:]...)
if len(Ei.Tags[tag]) == 0 { Ei.CH <- SERIES_MD
delete(Ei.Tags, tag)
}
return return
} }
} }
@@ -288,78 +313,40 @@ func ManageTagsArticle(artc *Article, s bool, dos ...string) {
} }
} }
func ManageSeriesArticle(artc *Article, s bool, dos ...string) { func ManageArchivesArticle(artc *Article, s bool, do string) {
for _, do := range dos { switch do {
switch do { case ADD:
case ADD: add := false
if artc.SerieID != 0 { y, m, _ := artc.CreateTime.Date()
for i, serie := range Ei.Series { for i, archive := range Ei.Archives {
if serie.ID == artc.SerieID { ay, am, _ := archive.Time.Date()
Ei.Series[i].Articles = append(Ei.Series[i].Articles, artc) if y == ay && m == am {
if s { add = true
sort.Sort(Ei.Series[i].Articles) Ei.Archives[i].Articles = append(Ei.Archives[i].Articles, artc)
Ei.CH <- SERIES_MD if s {
return sort.Sort(Ei.Archives[i].Articles)
} Ei.CH <- ARCHIVE_MD
} break
}
}
case DELETE:
if artc.SerieID != 0 {
for i, serie := range Ei.Series {
if serie.ID == artc.SerieID {
for j, v := range serie.Articles {
if v == artc {
Ei.Series[i].Articles = append(Ei.Series[i].Articles[0:j], Ei.Series[i].Articles[j+1:]...)
Ei.CH <- SERIES_MD
return
}
}
}
} }
} }
} }
} if !add {
Ei.Archives = append(Ei.Archives, &Archive{Time: artc.CreateTime, Articles: SortArticles{artc}})
} }
case DELETE:
func ManageArchivesArticle(artc *Article, s bool, dos ...string) { for i, archive := range Ei.Archives {
for _, do := range dos { ay, am, _ := archive.Time.Date()
switch do { if y, m, _ := artc.CreateTime.Date(); ay == y && am == m {
case ADD: for j, v := range archive.Articles {
add := false if v == artc {
y, m, _ := artc.CreateTime.Date() Ei.Archives[i].Articles = append(Ei.Archives[i].Articles[0:j], Ei.Archives[i].Articles[j+1:]...)
for i, archive := range Ei.Archives {
ay, am, _ := archive.Time.Date()
if y == ay && m == am {
add = true
Ei.Archives[i].Articles = append(Ei.Archives[i].Articles, artc)
if s {
sort.Sort(Ei.Archives[i].Articles)
Ei.CH <- ARCHIVE_MD Ei.CH <- ARCHIVE_MD
break return
}
}
}
if !add {
Ei.Archives = append(Ei.Archives, &Archive{Time: artc.CreateTime, Articles: SortArticles{artc}})
}
case DELETE:
for i, archive := range Ei.Archives {
ay, am, _ := archive.Time.Date()
if y, m, _ := artc.CreateTime.Date(); ay == y && am == m {
for j, v := range archive.Articles {
if v == artc {
Ei.Archives[i].Articles = append(Ei.Archives[i].Articles[0:j], Ei.Archives[i].Articles[j+1:]...)
Ei.CH <- ARCHIVE_MD
return
}
} }
} }
} }
} }
} }
} }
// 渲染markdown操作和截取摘要操作 // 渲染markdown操作和截取摘要操作
@@ -438,6 +425,7 @@ func DelArticles(ids ...int32) error {
if err != nil { if err != nil {
return err return err
} }
artc = nil
} }
Ei.CH <- ARCHIVE_MD Ei.CH <- ARCHIVE_MD
Ei.CH <- SERIES_MD Ei.CH <- SERIES_MD

View File

@@ -12,7 +12,7 @@ services:
volumes: volumes:
- /data/eiblog/conf/es/config:/usr/share/elasticsearch/config - /data/eiblog/conf/es/config:/usr/share/elasticsearch/config
- /data/eiblog/conf/es/plugins:/usr/share/elasticsearch/plugins - /data/eiblog/conf/es/plugins:/usr/share/elasticsearch/plugins
- /data/eiblog/conf/es/data:/usr/share/elasticsearch/data - /data/eiblog/esdata/data:/usr/share/elasticsearch/data
- /data/eiblog/conf/es/logs:/usr/share/elasticsearch/logs - /data/eiblog/conf/es/logs:/usr/share/elasticsearch/logs
environment: environment:
ES_JAVA_OPTS: "-Xms512m -Xmx512m" ES_JAVA_OPTS: "-Xms512m -Xmx512m"

View File

@@ -132,6 +132,8 @@ func HandleArticlePage(c *gin.Context) {
h["Copyright"] = Ei.Copyright h["Copyright"] = Ei.Copyright
if !artc.UpdateTime.IsZero() { if !artc.UpdateTime.IsZero() {
h["Days"] = int(time.Now().Sub(artc.UpdateTime).Hours()) / 24 h["Days"] = int(time.Now().Sub(artc.UpdateTime).Hours()) / 24
} else {
h["Days"] = int(time.Now().Sub(artc.CreateTime).Hours()) / 24
} }
if artc.SerieID > 0 { if artc.SerieID > 0 {
h["Serie"] = QuerySerie(artc.SerieID) h["Serie"] = QuerySerie(artc.SerieID)

View File

@@ -61,7 +61,7 @@
<td> <td>
<input type="checkbox" value="{{.ID}}" name="cid[]" /> <input type="checkbox" value="{{.ID}}" name="cid[]" />
</td> </td>
<td><a href="/admin/manage-comments?cid={{.ID}}" class="balloon-button size-1">{{.Count}}</a></td> <td><a href="/post/{{.Slug}}.html#comments" class="balloon-button size-1">{{.Count}}</a></td>
<td> <td>
<a href="/admin/write-post?cid={{.ID}}">{{.Title}}</a> <a href="/admin/write-post?cid={{.ID}}">{{.Title}}</a>
<a target="_black" href="/post/{{.Slug}}.html" title="浏览 {{.Title}}"><i class="i-exlink"></i></a> <a target="_black" href="/post/{{.Slug}}.html" title="浏览 {{.Title}}"><i class="i-exlink"></i></a>

View File

@@ -1 +1 @@
{{define "article"}}<div id=content class=inner>{{with .Article}}<article class="post post-{{.ID}}" itemscope itemtype="http://schema.org/Article"><div class=meta><div class=date><time itemprop=datePublished content="{{dateformat .CreateTime " 2006-01-02 "}}">{{dateformat .CreateTime "Jan 02, 2006"}}</time></div><div class=comment><a href="#comments">{{.Count}} Comments</a></div></div><h1 class=title itemprop=headline>{{.Title}}</h1><div class="entry-content" itemprop=articleBody>{{str2html .Content}}<p>本文链接:<a rel="bookmark" title="Permalink to {{.Title}}" href="//{{$.Domain}}/post/{{.Slug}}.html" itemprop="url">https://{{$.Domain}}/post/{{.Slug}}.html</a><a href="//{{$.Domain}}/post/{{.Slug}}.html#comments">参与评论 »</a></p><p>--<acronym title="End of File">EOF</acronym>--</p><p class="post-info">发表于 <span class="date">{{dateformat .CreateTime "2006-01-02 15:04:05"}}</span>{{if gt (.Tags|len) 0}},并被添加「{{range $index, $elem := .Tags}}{{if gt $index 0}}、{{end}}<a href="/search.html?q=tag:{{$elem}}">{{$elem}}</a>{{end}}」标签{{end}}{{if .UpdateTime|isnotzero}},最后修改于 <span class="date">{{dateformat .UpdateTime "2006-01-02 15:04:05"}}</span>{{end}}。</p>{{with $.Copyright}}<p class="copyright-info">{{str2html $.Copyright}}<a href="//{{$.Domain}}/post/about.html#toc-1">更多说明 »</a></p>{{end}} {{with $.Serie}}{{if gt $.Days 100}}<p class="expire-tips">提醒:本文最后更新于 {{$.Days}} 天前,文中所描述的信息可能已发生改变,请谨慎使用。</p>{{end}}<div class="entry-series"><h3>专题「{{.Name}}」的其它文章 <a href="/series.html#toc-{{.ID}}" title="更多">»</a></h3><ul>{{range .Articles}}{{if ne .ID $.Article.ID}}<li><a href="/post/{{.Slug}}.html">{{.Title}}</a> <span class="date">({{dateformat .CreateTime "Jan 02, 2006"}})</span></li>{{end}}{{end}}</ul></div>{{end}}</div></article><nav class="page-navi">{{with .Prev}}<a href="/post/{{.Slug}}.html" class=prev>« {{.Title}}</a>{{end}}{{with .Next}}<a href="/post/{{.Slug}}.html" class=next>{{.Title}} »</a>{{end}}</nav><section id=comments><h1 class=title>Comments<span id=switch_thread_mode></h1><div id=disqus_thread class=ds-thread data-identifier="post-{{.Slug}}" data-url="https://{{$.Domain}}/post/{{.Slug}}.html"></div><div id=simple_thread data-id="{{.Slug}}"></div></section>{{end}}</div>{{end}} {{define "article"}}<div id=content class=inner>{{with .Article}}<article class="post post-{{.ID}}" itemscope itemtype="http://schema.org/Article"><div class=meta><div class=date><time itemprop=datePublished content="{{dateformat .CreateTime " 2006-01-02 "}}">{{dateformat .CreateTime "Jan 02, 2006"}}</time></div><div class=comment><a href="#comments">{{.Count}} Comments</a></div></div><h1 class=title itemprop=headline>{{.Title}}</h1><div class="entry-content" itemprop=articleBody>{{str2html .Content}}<p>本文链接:<a rel="bookmark" title="Permalink to {{.Title}}" href="//{{$.Domain}}/post/{{.Slug}}.html" itemprop="url">https://{{$.Domain}}/post/{{.Slug}}.html</a><a href="//{{$.Domain}}/post/{{.Slug}}.html#comments">参与评论 »</a></p><p>--<acronym title="End of File">EOF</acronym>--</p><p class="post-info">发表于 <span class="date">{{dateformat .CreateTime "2006-01-02 15:04:05"}}</span>{{if gt (.Tags|len) 0}},并被添加「{{range $index, $elem := .Tags}}{{if gt $index 0}}、{{end}}<a href="/search.html?q=tag:{{$elem}}">{{$elem}}</a>{{end}}」标签{{end}}{{if .UpdateTime|isnotzero}},最后修改于 <span class="date">{{dateformat .UpdateTime "2006-01-02 15:04:05"}}</span>{{end}}。</p>{{with $.Copyright}}<p class="copyright-info">{{str2html $.Copyright}}<a href="//{{$.Domain}}/post/about.html#toc-1">更多说明 »</a></p>{{end}}{{if gt $.Days 100}}<p class="expire-tips">提醒:本文最后更新于 {{$.Days}} 天前,文中所描述的信息可能已发生改变,请谨慎使用。</p>{{end}}{{with $.Serie}}<div class="entry-series"><h3>专题「{{.Name}}」的其它文章 <a href="/series.html#toc-{{.ID}}" title="更多">»</a></h3><ul>{{range .Articles}}{{if ne .ID $.Article.ID}}<li><a href="/post/{{.Slug}}.html">{{.Title}}</a> <span class="date">({{dateformat .CreateTime "Jan 02, 2006"}})</span></li>{{end}}{{end}}</ul></div>{{end}}</div></article><nav class="page-navi">{{with .Prev}}<a href="/post/{{.Slug}}.html" class=prev>« {{.Title}}</a>{{end}}{{with .Next}}<a href="/post/{{.Slug}}.html" class=next>{{.Title}} »</a>{{end}}</nav><section id=comments><h1 class=title>Comments<span id=switch_thread_mode></h1><div id=disqus_thread class=ds-thread data-identifier="post-{{.Slug}}" data-url="https://{{$.Domain}}/post/{{.Slug}}.html"></div><div id=simple_thread data-id="{{.Slug}}"></div></section>{{end}}</div>{{end}}