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

@ -3,14 +3,22 @@ package uploads
import (
"context"
"fmt"
"html/template"
"io"
"log"
"strings"
"lmika.dev/lmika/weiro/models"
)
var (
uploadCopyTemplate = template.Must(template.New("upload-copy").Parse(`<img src="/uploads/{{.Slug}}" alt="{{.Alt}}">`))
)
type UploadWithURL struct {
Upload models.Upload
URL string
Upload models.Upload
CopyTemplate string
URL string
}
func (s *Service) FetchUpload(ctx context.Context, uploadID int64) (res UploadWithURL, _ error) {
@ -25,11 +33,22 @@ func (s *Service) FetchUpload(ctx context.Context, uploadID int64) (res UploadWi
}
return UploadWithURL{
Upload: upload,
URL: fmt.Sprintf("/sites/%v/uploads/%v/raw", site.ID, upload.ID),
Upload: upload,
CopyTemplate: s.renderCopyTemplate(upload),
URL: fmt.Sprintf("/sites/%v/uploads/%v/raw", site.ID, upload.ID),
}, nil
}
func (s *Service) renderCopyTemplate(upload models.Upload) string {
var sb strings.Builder
if err := uploadCopyTemplate.Execute(&sb, upload); err != nil {
log.Printf("error rendering upload copy template: %v", err)
return ""
}
return sb.String()
}
func (s *Service) ListUploads(ctx context.Context) (res []UploadWithURL, _ error) {
site, _, err := s.fetchSiteAndUser(ctx)
if err != nil {
@ -44,8 +63,9 @@ func (s *Service) ListUploads(ctx context.Context) (res []UploadWithURL, _ error
res = make([]UploadWithURL, len(uploads))
for i, upload := range uploads {
res[i] = UploadWithURL{
Upload: upload,
URL: fmt.Sprintf("/sites/%v/uploads/%v/raw", site.ID, upload.ID),
Upload: upload,
CopyTemplate: s.renderCopyTemplate(upload),
URL: fmt.Sprintf("/sites/%v/uploads/%v/raw", site.ID, upload.ID),
}
}