-
Notifications
You must be signed in to change notification settings - Fork 0
/
filescan.go
81 lines (74 loc) · 1.95 KB
/
filescan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package app
import (
"goblog/app/models"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"goblog/app/htmlconv"
"github.com/bradfitz/slice"
"github.com/revel/revel"
)
var reloadDelay = 5000
var ArticleCache map[string]*models.ArticleInfo
var ArticleList []models.ArticleInfo
func LoadContent(a *models.ArticleInfo) error {
ftype := filepath.Ext(a.Path)
conv, err := htmlconv.GetConv(ftype)
if err != nil {
return err
}
article, err := ioutil.ReadFile(a.Path)
if err != nil {
return err
}
a.Content = article
rawHtml := conv.ToHTML(article)
a.HTMLContent = htmlconv.PostProcessHTML(rawHtml)
return nil
}
func reloadArticleList() {
for {
// revel.INFO.Printf("Reloading article list ...")
var l []models.ArticleInfo = make([]models.ArticleInfo, 0, 1000)
var infoCacheNew = make(map[string]*models.ArticleInfo)
err := filepath.Walk(DocBaseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
revel.ERROR.Printf("Cannot access file %s, %s", path, err)
return nil
}
if info.IsDir() {
return nil
}
name := strings.Split(info.Name(), ".")[0]
inCache := ArticleCache[name]
if inCache == nil || inCache.MTime.Before(info.ModTime()) {
article := models.ArticleInfo{Title: name, Path: path, MTime: info.ModTime()}
err := LoadContent(&article)
if err != nil {
revel.ERROR.Println("Cannot load article content from ", article.Path, "Reason:", err)
}
infoCacheNew[name] = &article
} else {
infoCacheNew[name] = inCache
}
l = append(l, *infoCacheNew[name])
return nil
})
if err != nil {
revel.ERROR.Printf("Error reloading article list: %s", err)
} else {
ArticleCache = infoCacheNew
slice.Sort(l, func(i, j int) bool {
return l[i].MTime.After(l[j].MTime)
})
ArticleList = l
// revel.INFO.Printf("Article list reloaded.")
}
time.Sleep(time.Duration(reloadDelay) * time.Millisecond)
}
}
func reloadArticlesRoutine() {
go reloadArticleList()
}