web: картинки заметки отдаются через ресайз

Раньше страница ссылалась прямо на файлы Memos, и в браузер уезжали
исходники с телефона на десятки мегабайт. Теперь вложения идут через
эндпоинт /image/{uid}: скачиваем тем же клиентом, что и телеграм-часть,
ужимаем ImageMagick до 1600px по большей стороне и кэшируем результат
в памяти (FIFO на 20 картинок) плюс Cache-Control на час.

GIF и SVG отдаются без конверсии: анимация и вектор в JPEG теряются.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
av
2026-08-06 12:26:33 +03:00
co-authored by Claude Opus 5
parent 8e8cc990c4
commit 39f4a4dff9
4 changed files with 185 additions and 10 deletions
+9 -9
View File
@@ -10,6 +10,7 @@ import (
"time"
"git.vakhrushev.me/av/remembos/internal/memory"
"git.vakhrushev.me/av/remembos/internal/memos"
)
//go:embed templates/*.html
@@ -40,26 +41,31 @@ type errorData struct {
type Handler struct {
service *memory.Service
client *memos.Client
logger *slog.Logger
mux *http.ServeMux
publicURL string // public Memos URL (for images and memo links)
images *imageCache
publicURL string // public Memos URL (for memo links)
allowLoadMore bool
}
func NewHandler(service *memory.Service, memosURL, publicURL string, allowLoadMore bool, logger *slog.Logger) *Handler {
func NewHandler(service *memory.Service, client *memos.Client, memosURL, publicURL string, allowLoadMore bool, logger *slog.Logger) *Handler {
pub := publicURL
if pub == "" {
pub = memosURL
}
h := &Handler{
service: service,
client: client,
publicURL: strings.TrimRight(pub, "/"),
allowLoadMore: allowLoadMore,
logger: logger,
mux: http.NewServeMux(),
images: newImageCache(),
}
h.mux.HandleFunc("GET /", h.handleMemory)
h.mux.HandleFunc("GET /health", h.handleHealth)
h.mux.HandleFunc("GET /image/{uid}", h.handleImage)
h.mux.HandleFunc("POST /more", h.handleLoadMore)
return h
}
@@ -101,13 +107,7 @@ func (h *Handler) handleMemory(w http.ResponseWriter, r *http.Request) {
if !att.IsImage() {
continue
}
var imgURL string
if att.ExternalLink != "" {
imgURL = att.ExternalLink
} else {
imgURL = fmt.Sprintf("%s/file/%s/%s", h.publicURL, att.Name, att.Filename)
}
images = append(images, imageData{URL: imgURL, Alt: att.Filename})
images = append(images, imageData{URL: imageURL(att), Alt: att.Filename})
}
// Link to original memo: {publicURL}/{memoName}