Added publishing of uploads to built site

This commit is contained in:
Leon Mika 2026-03-03 22:36:24 +11:00
parent 48f39133d7
commit d0cebe6564
13 changed files with 145 additions and 20 deletions

View file

@ -5,7 +5,6 @@ import (
"fmt"
"html/template"
"io"
"log"
"os"
"path/filepath"
"sort"
@ -15,6 +14,7 @@ import (
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"golang.org/x/sync/errgroup"
"lmika.dev/lmika/weiro/models"
"lmika.dev/lmika/weiro/models/pubmodel"
)
@ -57,13 +57,32 @@ func (b *Builder) BuildSite(outDir string) error {
return err
}
for _, post := range b.site.Posts {
if err := b.writePost(buildCtx, post); err != nil {
eg := errgroup.Group{}
eg.Go(func() error {
for _, post := range b.site.Posts {
if err := b.writePost(buildCtx, post); err != nil {
return err
}
}
return nil
})
eg.Go(func() error {
if err := b.renderPostList(buildCtx, b.site.Posts); err != nil {
return err
}
}
return nil
})
if err := b.renderPostList(buildCtx, b.site.Posts); err != nil {
// Copy uploads
eg.Go(func() error {
if err := b.writeUploads(buildCtx, b.site.Uploads); err != nil {
return err
}
return nil
})
if err := eg.Wait(); err != nil {
return err
}
@ -130,7 +149,6 @@ func (b *Builder) createAtPath(ctx buildContext, path string, fn func(f io.Write
if filepath.Ext(outFile) == "" {
outFile = filepath.Join(outFile, "index.html")
}
log.Printf("Writing %s\n", outFile)
// Render it within the template
if err := os.MkdirAll(filepath.Dir(outFile), 0755); err != nil {
@ -158,6 +176,37 @@ func (b *Builder) renderTemplate(w io.Writer, name string, data interface{}) err
})
}
func (b *Builder) writeUploads(ctx buildContext, uploads []models.Upload) error {
for _, u := range uploads {
fullPath := filepath.Join(ctx.outDir, "uploads", u.Slug)
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
return err
}
if err := func() error {
r, err := b.site.OpenUpload(u)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(fullPath)
if err != nil {
return err
}
defer w.Close()
if _, err := io.Copy(w, r); err != nil {
return err
}
return nil
}(); err != nil {
return err
}
}
return nil
}
type buildContext struct {
outDir string
}

View file

@ -37,6 +37,10 @@ func (p *Provider) OpenUpload(site models.Site, up models.Upload) (io.ReadCloser
return os.Open(fullPath)
}
func (p *Provider) UploadDir(site models.Site) string {
return filepath.Join(p.baseDir, site.GUID)
}
func (p *Provider) uploadFileName(site models.Site, up models.Upload) string {
return filepath.Join(p.baseDir, site.GUID, up.Slug)
}