add images from memos

This commit is contained in:
2026-02-12 11:24:09 +03:00
parent ed04c11eff
commit c083461d38
6 changed files with 117 additions and 22 deletions
+42 -7
View File
@@ -6,6 +6,7 @@ import (
"html/template"
"log/slog"
"net/http"
"strings"
"time"
"git.vakhrushev.me/av/remembos/internal/memory"
@@ -16,11 +17,18 @@ var templateFS embed.FS
var templates = template.Must(template.ParseFS(templateFS, "templates/*.html"))
type imageData struct {
URL string
Alt string
}
type memoryData struct {
DateFormatted string
AgoText string
Content string
Tags []string
Images []imageData
MemoURL string
Tier int
ShowCount int
}
@@ -30,16 +38,24 @@ type errorData struct {
}
type Handler struct {
service *memory.Service
logger *slog.Logger
mux *http.ServeMux
service *memory.Service
logger *slog.Logger
mux *http.ServeMux
memosURL string // internal Memos URL (for attachment files)
publicURL string // public Memos URL (for memo links)
}
func NewHandler(service *memory.Service, logger *slog.Logger) *Handler {
func NewHandler(service *memory.Service, memosURL, publicURL string, logger *slog.Logger) *Handler {
pub := publicURL
if pub == "" {
pub = memosURL
}
h := &Handler{
service: service,
logger: logger,
mux: http.NewServeMux(),
service: service,
memosURL: strings.TrimRight(memosURL, "/"),
publicURL: strings.TrimRight(pub, "/"),
logger: logger,
mux: http.NewServeMux(),
}
h.mux.HandleFunc("GET /", h.handleMemory)
h.mux.HandleFunc("GET /health", h.handleHealth)
@@ -74,11 +90,30 @@ func (h *Handler) handleMemory(w http.ResponseWriter, r *http.Request) {
return
}
var images []imageData
for _, att := range mem.Memo.Attachments {
if !att.IsImage() {
continue
}
var imgURL string
if att.ExternalLink != "" {
imgURL = att.ExternalLink
} else {
imgURL = fmt.Sprintf("%s/file/%s/%s", h.memosURL, att.Name, att.Filename)
}
images = append(images, imageData{URL: imgURL, Alt: att.Filename})
}
// Link to original memo: {publicURL}/{memoName}
memoURL := fmt.Sprintf("%s/%s", h.publicURL, mem.Memo.Name)
data := memoryData{
DateFormatted: formatDate(mem.Date),
AgoText: agoText(mem.Date),
Content: mem.Memo.Content,
Tags: mem.Memo.Tags,
Images: images,
MemoURL: memoURL,
Tier: mem.Tier,
ShowCount: mem.ShowCount,
}