mirror of
https://github.com/Kugelschieber/marvinblum.git
synced 2026-01-18 06:40:27 +00:00
Download blog file attachments when caching articles.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
.idea/
|
||||
static/blog/
|
||||
|
||||
56
blog/blog.go
56
blog/blog.go
@@ -1,28 +1,34 @@
|
||||
package blog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Kugelschieber/marvinblum.de/tpl"
|
||||
emvi "github.com/emvi/api-go"
|
||||
"github.com/emvi/logbuch"
|
||||
"github.com/gorilla/mux"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
blogCacheTime = time.Hour
|
||||
blogFileCache = "static/blog"
|
||||
maxLatestArticles = 3
|
||||
)
|
||||
|
||||
var (
|
||||
blog Blog
|
||||
linkRegex = regexp.MustCompile(`(?iU)href="/read/([^"]+)"`)
|
||||
attachmentRegex = regexp.MustCompile(`(?iU)(href|src)="([^"]+)/api/v1/content/([^"]+)"`)
|
||||
attachmentURLRegex = regexp.MustCompile(`(?iU)(href|src)="([^"]+/api/v1/content/)([^"]+)"`)
|
||||
)
|
||||
|
||||
// TODO cache files
|
||||
// TODO fix mentions
|
||||
type Blog struct {
|
||||
client *emvi.Client
|
||||
articles map[string]emvi.Article // id -> article
|
||||
@@ -37,6 +43,11 @@ func InitBlog() {
|
||||
os.Getenv("MB_EMVI_ORGA"),
|
||||
nil)
|
||||
blog.nextUpdate = time.Now().Add(blogCacheTime)
|
||||
|
||||
if err := os.MkdirAll(blogFileCache, 0755); err != nil {
|
||||
logbuch.Error("Error creating blog file cache directory", logbuch.Fields{"err": err})
|
||||
}
|
||||
|
||||
blog.loadArticles()
|
||||
}
|
||||
|
||||
@@ -87,10 +98,47 @@ func (blog *Blog) loadArticle(article emvi.Article) *emvi.ArticleContent {
|
||||
return nil
|
||||
}
|
||||
|
||||
blog.downloadAttachments(article.Id, content.Content)
|
||||
content.Content = linkRegex.ReplaceAllString(content.Content, `href="/blog/$1"`)
|
||||
content.Content = attachmentRegex.ReplaceAllString(content.Content, fmt.Sprintf(`$1="/static/blog/%s/$3"`, article.Id))
|
||||
logbuch.Debug("Article loaded", logbuch.Fields{"id": article.Id})
|
||||
return content
|
||||
}
|
||||
|
||||
func (blog *Blog) downloadAttachments(id, content string) {
|
||||
if _, err := os.Stat(filepath.Join(blogFileCache, id)); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(filepath.Join(blogFileCache, id), 0755); err != nil {
|
||||
logbuch.Error("Error creating article file cache directory", logbuch.Fields{"err": err, "id": id})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
results := attachmentURLRegex.FindAllStringSubmatch(content, -1)
|
||||
|
||||
for _, attachment := range results {
|
||||
if len(attachment) == 4 {
|
||||
resp, err := http.Get(attachment[2] + attachment[3])
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error downloading blog attachment", logbuch.Fields{"err": err, "id": id})
|
||||
continue
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading blog attachment body", logbuch.Fields{"err": err, "id": id})
|
||||
continue
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(filepath.Join(blogFileCache, id, attachment[3]), data, 0755); err != nil {
|
||||
logbuch.Error("Error saving blog attachment on disk", logbuch.Fields{"err": err, "id": id})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (blog *Blog) setArticles(articles map[string]emvi.Article) {
|
||||
blog.articles = articles
|
||||
blog.articlesYear = make(map[int][]emvi.Article)
|
||||
@@ -176,11 +224,11 @@ func ServeBlogArticle() http.HandlerFunc {
|
||||
data := struct {
|
||||
Title string
|
||||
Content template.HTML
|
||||
Published string
|
||||
Published time.Time
|
||||
}{
|
||||
article.LatestArticleContent.Title,
|
||||
template.HTML(article.LatestArticleContent.Content),
|
||||
article.Published.Format("2. January 2006"),
|
||||
article.Published,
|
||||
}
|
||||
|
||||
if err := tpl.Get().ExecuteTemplate(w, "article.html", data); err != nil {
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
{{range $article := .Articles}}
|
||||
<p>
|
||||
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
|
||||
<small>{{format $article.Published "2. January 2006"}}</small>
|
||||
</p>
|
||||
{{else}}
|
||||
<p>There are no blog posts yet...</p>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<section>
|
||||
<h1>{{.Title}}</h1>
|
||||
<small>Published on {{.Published}}</small>
|
||||
<small>Published on {{format .Published "2. January 2006"}}</small>
|
||||
{{.Content}}
|
||||
</section>
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
{{range $article := $articles}}
|
||||
<p>
|
||||
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
|
||||
<small>{{format $article.Published "2. January 2006"}}</small>
|
||||
</p>
|
||||
{{end}}
|
||||
{{else}}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -21,6 +22,9 @@ var (
|
||||
|
||||
var funcMap = template.FuncMap{
|
||||
"slug": slug.Make,
|
||||
"format": func(t time.Time, layout string) string {
|
||||
return t.Format(layout)
|
||||
},
|
||||
}
|
||||
|
||||
func LoadTemplate() {
|
||||
|
||||
Reference in New Issue
Block a user