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/
|
.idea/
|
||||||
|
static/blog/
|
||||||
|
|||||||
58
blog/blog.go
58
blog/blog.go
@@ -1,28 +1,34 @@
|
|||||||
package blog
|
package blog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/Kugelschieber/marvinblum.de/tpl"
|
"github.com/Kugelschieber/marvinblum.de/tpl"
|
||||||
emvi "github.com/emvi/api-go"
|
emvi "github.com/emvi/api-go"
|
||||||
"github.com/emvi/logbuch"
|
"github.com/emvi/logbuch"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
blogCacheTime = time.Hour
|
blogCacheTime = time.Hour
|
||||||
|
blogFileCache = "static/blog"
|
||||||
maxLatestArticles = 3
|
maxLatestArticles = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
blog Blog
|
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 {
|
type Blog struct {
|
||||||
client *emvi.Client
|
client *emvi.Client
|
||||||
articles map[string]emvi.Article // id -> article
|
articles map[string]emvi.Article // id -> article
|
||||||
@@ -37,6 +43,11 @@ func InitBlog() {
|
|||||||
os.Getenv("MB_EMVI_ORGA"),
|
os.Getenv("MB_EMVI_ORGA"),
|
||||||
nil)
|
nil)
|
||||||
blog.nextUpdate = time.Now().Add(blogCacheTime)
|
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()
|
blog.loadArticles()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,10 +98,47 @@ func (blog *Blog) loadArticle(article emvi.Article) *emvi.ArticleContent {
|
|||||||
return nil
|
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})
|
logbuch.Debug("Article loaded", logbuch.Fields{"id": article.Id})
|
||||||
return content
|
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) {
|
func (blog *Blog) setArticles(articles map[string]emvi.Article) {
|
||||||
blog.articles = articles
|
blog.articles = articles
|
||||||
blog.articlesYear = make(map[int][]emvi.Article)
|
blog.articlesYear = make(map[int][]emvi.Article)
|
||||||
@@ -176,11 +224,11 @@ func ServeBlogArticle() http.HandlerFunc {
|
|||||||
data := struct {
|
data := struct {
|
||||||
Title string
|
Title string
|
||||||
Content template.HTML
|
Content template.HTML
|
||||||
Published string
|
Published time.Time
|
||||||
}{
|
}{
|
||||||
article.LatestArticleContent.Title,
|
article.LatestArticleContent.Title,
|
||||||
template.HTML(article.LatestArticleContent.Content),
|
template.HTML(article.LatestArticleContent.Content),
|
||||||
article.Published.Format("2. January 2006"),
|
article.Published,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tpl.Get().ExecuteTemplate(w, "article.html", data); err != nil {
|
if err := tpl.Get().ExecuteTemplate(w, "article.html", data); err != nil {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
{{range $article := .Articles}}
|
{{range $article := .Articles}}
|
||||||
<p>
|
<p>
|
||||||
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
|
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
|
||||||
|
<small>{{format $article.Published "2. January 2006"}}</small>
|
||||||
</p>
|
</p>
|
||||||
{{else}}
|
{{else}}
|
||||||
<p>There are no blog posts yet...</p>
|
<p>There are no blog posts yet...</p>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
<section>
|
<section>
|
||||||
<h1>{{.Title}}</h1>
|
<h1>{{.Title}}</h1>
|
||||||
<small>Published on {{.Published}}</small>
|
<small>Published on {{format .Published "2. January 2006"}}</small>
|
||||||
{{.Content}}
|
{{.Content}}
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
{{range $article := $articles}}
|
{{range $article := $articles}}
|
||||||
<p>
|
<p>
|
||||||
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
|
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
|
||||||
|
<small>{{format $article.Published "2. January 2006"}}</small>
|
||||||
</p>
|
</p>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{else}}
|
{{else}}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -21,6 +22,9 @@ var (
|
|||||||
|
|
||||||
var funcMap = template.FuncMap{
|
var funcMap = template.FuncMap{
|
||||||
"slug": slug.Make,
|
"slug": slug.Make,
|
||||||
|
"format": func(t time.Time, layout string) string {
|
||||||
|
return t.Format(layout)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadTemplate() {
|
func LoadTemplate() {
|
||||||
|
|||||||
Reference in New Issue
Block a user