From 6fe95443a0aaccc576388e3ca8b347e2165106b4 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Sun, 14 Jun 2020 15:16:44 +0200 Subject: [PATCH] Download blog file attachments when caching articles. --- .gitignore | 1 + blog/blog.go | 58 +++++++++++++++++++++++++++++++++++++++---- template/about.html | 1 + template/article.html | 2 +- template/blog.html | 1 + tpl/template.go | 4 +++ 6 files changed, 61 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 9f11b75..df45cc1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea/ +static/blog/ diff --git a/blog/blog.go b/blog/blog.go index 19fc72a..0630747 100644 --- a/blog/blog.go +++ b/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 + 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 { diff --git a/template/about.html b/template/about.html index 9f9b943..42cce9e 100644 --- a/template/about.html +++ b/template/about.html @@ -22,6 +22,7 @@ {{range $article := .Articles}}

{{$article.LatestArticleContent.Title}} + {{format $article.Published "2. January 2006"}}

{{else}}

There are no blog posts yet...

diff --git a/template/article.html b/template/article.html index 4c49f22..405113d 100644 --- a/template/article.html +++ b/template/article.html @@ -3,7 +3,7 @@

{{.Title}}

- Published on {{.Published}} + Published on {{format .Published "2. January 2006"}} {{.Content}}
diff --git a/template/blog.html b/template/blog.html index 1da03c3..1550da0 100644 --- a/template/blog.html +++ b/template/blog.html @@ -9,6 +9,7 @@ {{range $article := $articles}}

{{$article.LatestArticleContent.Title}} + {{format $article.Published "2. January 2006"}}

{{end}} {{else}} diff --git a/tpl/template.go b/tpl/template.go index d37f20c..ecea8dc 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -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() {