Download blog file attachments when caching articles.

This commit is contained in:
2020-06-14 15:16:44 +02:00
parent bdb3261959
commit 6fe95443a0
6 changed files with 61 additions and 6 deletions

View File

@@ -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 {