diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-07 00:05:40 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-07 00:05:40 +0300 |
| commit | 8db759e3f7be6ddbbc1738d285eef565b24d94bc (patch) | |
| tree | 5d7392eddf48fb6db258309217e8191e5929d619 /internal/api | |
| parent | 973c3108b71063ea932aed6c67e16e393b5e6c41 (diff) | |
Handle share page marshal errors (a1)
Diffstat (limited to 'internal/api')
| -rw-r--r-- | internal/api/handlers_more_test.go | 22 | ||||
| -rw-r--r-- | internal/api/handlers_share.go | 18 |
2 files changed, 37 insertions, 3 deletions
diff --git a/internal/api/handlers_more_test.go b/internal/api/handlers_more_test.go index 2675483..4e698b8 100644 --- a/internal/api/handlers_more_test.go +++ b/internal/api/handlers_more_test.go @@ -1084,6 +1084,28 @@ func TestServer_SharePage(t *testing.T) { }) } +func TestInjectShareMedia(t *testing.T) { + t.Run("injects marshaled metadata", func(t *testing.T) { + html, err := injectShareMedia(`<script><!--SHARE_MEDIA--></script>`, map[string]string{"stream_url": "/s/abc/stream"}) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if !strings.Contains(html, `"stream_url":"/s/abc/stream"`) { + t.Fatalf("expected injected JSON, got %q", html) + } + }) + + t.Run("returns marshal error", func(t *testing.T) { + _, err := injectShareMedia(`<script><!--SHARE_MEDIA--></script>`, map[string]any{"bad": make(chan int)}) + if err == nil { + t.Fatal("expected marshal error") + } + if !strings.Contains(err.Error(), "marshal share metadata") { + t.Fatalf("expected wrapped marshal error, got %v", err) + } + }) +} + func TestServer_ShareStream(t *testing.T) { path := makeTempFile(t, "shared") cfg := &internal.Config{SessionTimeoutHours: 24} diff --git a/internal/api/handlers_share.go b/internal/api/handlers_share.go index 9aa3e8d..ed6ae20 100644 --- a/internal/api/handlers_share.go +++ b/internal/api/handlers_share.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" "errors" + "fmt" "io" "net/http" "strings" @@ -107,13 +108,24 @@ func (s *Server) handleSharePage(w http.ResponseWriter, r *http.Request) { http.Error(w, "internal error", http.StatusInternalServerError) return } - html := buf.String() - data, _ := json.Marshal(res) - html = strings.Replace(html, "<!--SHARE_MEDIA-->", string(data), 1) + html, err := injectShareMedia(buf.String(), res) + if err != nil { + s.logger.Error("marshal share page metadata", "err", err) + http.Error(w, "internal error", http.StatusInternalServerError) + return + } w.Header().Set("Content-Type", "text/html; charset=utf-8") http.ServeContent(w, r, "share.html", stat.ModTime(), strings.NewReader(html)) } +func injectShareMedia(html string, data any) (string, error) { + encoded, err := json.Marshal(data) + if err != nil { + return "", fmt.Errorf("marshal share metadata: %w", err) + } + return strings.Replace(html, "<!--SHARE_MEDIA-->", string(encoded), 1), nil +} + func (s *Server) handleShareThumbnail(w http.ResponseWriter, r *http.Request) { if !requireService(w, s.shareSvc) { return |
