From d87995c3629cac4671b05073915214f45d0c7112 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 17:49:32 +0530 Subject: refactor(api): centralize resource URLs --- internal/api/photos.go | 4 ++-- internal/api/url.go | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 internal/api/url.go diff --git a/internal/api/photos.go b/internal/api/photos.go index 2d492b1..92c5c04 100644 --- a/internal/api/photos.go +++ b/internal/api/photos.go @@ -30,8 +30,8 @@ func (s *Server) handlePhotos( DateTaken: photo.DateTaken, Width: photo.Width, Height: photo.Height, - ThumbnailURL: "/thumb/" + photo.ID, - PhotoURL: "/photo/" + photo.ID, + ThumbnailURL: thumbnailURL(photo.ID), + PhotoURL: photoURL(photo.ID), }) } diff --git a/internal/api/url.go b/internal/api/url.go new file mode 100644 index 0000000..06a944d --- /dev/null +++ b/internal/api/url.go @@ -0,0 +1,9 @@ +package api + +func thumbnailURL(id string) string { + return "/thumb/" + id +} + +func photoURL(id string) string { + return "/photo/" + id +} -- cgit v1.2.3 From f108043068c8818c616cc2e760f53839d4ba9b01 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 17:53:46 +0530 Subject: refactor(api): return structured error responses --- internal/api/error.go | 23 +++++++++++++++++++++++ internal/api/health.go | 6 +----- internal/api/photos.go | 12 +++++++----- 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 internal/api/error.go diff --git a/internal/api/error.go b/internal/api/error.go new file mode 100644 index 0000000..b00f326 --- /dev/null +++ b/internal/api/error.go @@ -0,0 +1,23 @@ +package api + +import ( + "encoding/json" + "net/http" +) + +type errorResponse struct { + Error string `json:"error"` +} + +func writeError( + w http.ResponseWriter, + status int, + message string, +) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + + _ = json.NewEncoder(w).Encode(errorResponse{ + Error: message, + }) +} diff --git a/internal/api/health.go b/internal/api/health.go index 61d5185..814fef5 100644 --- a/internal/api/health.go +++ b/internal/api/health.go @@ -20,10 +20,6 @@ func (s *Server) handleHealth( } if err := json.NewEncoder(w).Encode(response); err != nil { - http.Error( - w, - http.StatusText(http.StatusInternalServerError), - http.StatusInternalServerError, - ) + writeError(w, http.StatusInternalServerError, "failed to encode response") } } diff --git a/internal/api/photos.go b/internal/api/photos.go index 92c5c04..ca6b038 100644 --- a/internal/api/photos.go +++ b/internal/api/photos.go @@ -12,12 +12,12 @@ func (s *Server) handlePhotos( ) { photos, err := s.gallery.Gallery(r.Context()) if err != nil { - log.Printf("gallery: %v", err) + log.Printf("build gallery: %v", err) - http.Error( + writeError( w, - http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError, + "failed to build gallery", ) return } @@ -38,10 +38,12 @@ func (s *Server) handlePhotos( w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(response); err != nil { - http.Error( + log.Printf("encode photos response: %v", err) + + writeError( w, - http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError, + "failed to encode response", ) } } -- cgit v1.2.3 From 81e2a7cafaa16eaec0a08a693d223aa180fc406a Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 18:13:09 +0530 Subject: feat(service): introduce gallery index --- internal/service/gallery.go | 2 ++ internal/service/index.go | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 internal/service/index.go diff --git a/internal/service/gallery.go b/internal/service/gallery.go index f78e5df..30abefe 100644 --- a/internal/service/gallery.go +++ b/internal/service/gallery.go @@ -9,4 +9,6 @@ import ( type galleryService struct { scanner *scanner.Scanner builder *gallery.Builder + + index galleryIndex } diff --git a/internal/service/index.go b/internal/service/index.go new file mode 100644 index 0000000..dd39169 --- /dev/null +++ b/internal/service/index.go @@ -0,0 +1,9 @@ +package service + +import "github.com/RanXom/galleryd/internal/gallery" + +// galleryIndex stores an in-memory snapshot of the gallery. +type galleryIndex struct { + photos []gallery.Photo + byID map[string]gallery.Photo +} -- cgit v1.2.3 From a06e00d332aff7ddc667331c8f83b7317f5ca933 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 18:16:22 +0530 Subject: feat(service): build gallery index --- internal/service/gallery_service.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/service/gallery_service.go b/internal/service/gallery_service.go index 90e3c09..1145bb7 100644 --- a/internal/service/gallery_service.go +++ b/internal/service/gallery_service.go @@ -13,5 +13,21 @@ func (s *galleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { return nil, err } - return s.builder.Build(ctx, files) + photos, err := s.builder.Build(ctx, files) + if err != nil { + return nil, err + } + + index := galleryIndex{ + photos: photos, + byID: make(map[string]gallery.Photo, len(photos)), + } + + for _, photo := range photos { + index.byID[photo.ID] = photo + } + + s.index = index + + return s.index.photos, nil } -- cgit v1.2.3 From d754ce4c11af51e06515275b58a3f337a440ed9d Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 18:37:01 +0530 Subject: feat(service): introduce service lifecycle & load gallery during initialization --- cmd/galleryd/main.go | 8 ++++++-- internal/service/gallery_service.go | 24 +++++++----------------- internal/service/load.go | 33 +++++++++++++++++++++++++++++++++ internal/service/service.go | 4 ++++ 4 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 internal/service/load.go diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index 3b69474..5de4230 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -32,14 +32,18 @@ func main() { builder := gallery.New(reader) - gallerySerice := service.New( + galleryService := service.New( scanner, builder, ) + if err := galleryService.Load(ctx); err != nil { + log.Fatal(err) + } + server := api.New(api.Config{ Address: ":8082", - Gallery: gallerySerice, + Gallery: galleryService, }) log.Println("galleryd listening on :8082") diff --git a/internal/service/gallery_service.go b/internal/service/gallery_service.go index 1145bb7..e500bec 100644 --- a/internal/service/gallery_service.go +++ b/internal/service/gallery_service.go @@ -2,32 +2,22 @@ package service import ( "context" + "errors" "github.com/RanXom/galleryd/internal/gallery" ) // Gallery scans the configured roots and builds the gallery. func (s *galleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { - files, err := s.scanner.Scan(ctx) - if err != nil { - return nil, err + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: } - photos, err := s.builder.Build(ctx, files) - if err != nil { - return nil, err + if s.index.byID == nil { + return nil, errors.New("gallery not loaded") } - index := galleryIndex{ - photos: photos, - byID: make(map[string]gallery.Photo, len(photos)), - } - - for _, photo := range photos { - index.byID[photo.ID] = photo - } - - s.index = index - return s.index.photos, nil } diff --git a/internal/service/load.go b/internal/service/load.go new file mode 100644 index 0000000..09bbc20 --- /dev/null +++ b/internal/service/load.go @@ -0,0 +1,33 @@ +package service + +import ( + "context" + + "github.com/RanXom/galleryd/internal/gallery" +) + +// Load scans the configured roots and rebuilds the in-memory gallery. +func (s *galleryService) Load(ctx context.Context) error { + files, err := s.scanner.Scan(ctx) + if err != nil { + return err + } + + photos, err := s.builder.Build(ctx, files) + if err != nil { + return err + } + + index := galleryIndex{ + photos: photos, + byID: make(map[string]gallery.Photo, len(photos)), + } + + for _, photo := range photos { + index.byID[photo.ID] = photo + } + + s.index = index + + return nil +} diff --git a/internal/service/service.go b/internal/service/service.go index 6183e26..252abed 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -8,5 +8,9 @@ import ( // GalleryService provides gallery operations. type GalleryService interface { + // Load scans the configured roots and builds the in-memory gallery. + Load(ctx context.Context) error + + // Gallery returns the currently loaded gallery. Gallery(ctx context.Context) ([]gallery.Photo, error) } -- cgit v1.2.3 From ef5a8b157db8361d2a592f1481ba809ca49dc57d Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 18:41:45 +0530 Subject: feat(service): add photo lookup --- internal/service/photo.go | 29 +++++++++++++++++++++++++++++ internal/service/service.go | 3 +++ 2 files changed, 32 insertions(+) create mode 100644 internal/service/photo.go diff --git a/internal/service/photo.go b/internal/service/photo.go new file mode 100644 index 0000000..018ba6b --- /dev/null +++ b/internal/service/photo.go @@ -0,0 +1,29 @@ +package service + +import ( + "context" + "errors" + + "github.com/RanXom/galleryd/internal/gallery" +) + +var ErrPhotoNotFound = errors.New("photo not found") + +// Photo returns a single photo by its ID. +func (s *galleryService) Photo( + ctx context.Context, + id string, +) (gallery.Photo, error) { + select { + case <-ctx.Done(): + return gallery.Photo{}, ctx.Err() + default: + } + + photo, ok := s.index.byID[id] + if !ok { + return gallery.Photo{}, ErrPhotoNotFound + } + + return photo, nil +} diff --git a/internal/service/service.go b/internal/service/service.go index 252abed..68a764e 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -13,4 +13,7 @@ type GalleryService interface { // Gallery returns the currently loaded gallery. Gallery(ctx context.Context) ([]gallery.Photo, error) + + // Photo returns a single photo by its ID. + Photo(ctx context.Context, id string) (gallery.Photo, error) } -- cgit v1.2.3 From fdf1148db3c37d1be83ea4d52ac370c5da9593a4 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 18:45:47 +0530 Subject: feat(api): inject thumbnail generator --- cmd/galleryd/main.go | 12 +++++++++--- internal/api/config.go | 8 ++++++-- internal/api/new.go | 11 +++++++---- internal/api/server.go | 4 +++- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index 5de4230..5544544 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -12,6 +12,7 @@ import ( "github.com/RanXom/galleryd/internal/metadata" "github.com/RanXom/galleryd/internal/scanner" "github.com/RanXom/galleryd/internal/service" + "github.com/RanXom/galleryd/internal/thumbnail" ) func main() { @@ -36,14 +37,19 @@ func main() { scanner, builder, ) - if err := galleryService.Load(ctx); err != nil { log.Fatal(err) } + thumbnailGenerator, err := thumbnail.New(".cache/galleryd") + if err != nil { + log.Fatal(err) + } + server := api.New(api.Config{ - Address: ":8082", - Gallery: galleryService, + Address: ":8082", + Gallery: galleryService, + Thumbnails: thumbnailGenerator, }) log.Println("galleryd listening on :8082") diff --git a/internal/api/config.go b/internal/api/config.go index d82147c..a9a72ae 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -1,10 +1,14 @@ package api -import "github.com/RanXom/galleryd/internal/service" +import ( + "github.com/RanXom/galleryd/internal/service" + "github.com/RanXom/galleryd/internal/thumbnail" +) // Config configures the HTTP server type Config struct { Address string - Gallery service.GalleryService + Gallery service.GalleryService + Thumbnails *thumbnail.Generator } diff --git a/internal/api/new.go b/internal/api/new.go index a7c122b..7066d90 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -11,10 +11,13 @@ func New(config Config) *Server { } srv := &Server{ - config: config, - mux: mux, - http: server, - gallery: config.Gallery, + config: config, + + mux: mux, + http: server, + + gallery: config.Gallery, + thumbnail: config.Thumbnails, } srv.mux.HandleFunc( diff --git a/internal/api/server.go b/internal/api/server.go index c12c5bd..2bec8e2 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/RanXom/galleryd/internal/service" + "github.com/RanXom/galleryd/internal/thumbnail" ) // Server exposes the gallery over HTTP @@ -13,5 +14,6 @@ type Server struct { mux *http.ServeMux http *http.Server - gallery service.GalleryService + gallery service.GalleryService + thumbnail *thumbnail.Generator } -- cgit v1.2.3 From 87c5ba8c5f859fd26a038f6a704e856167c78758 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 18:52:32 +0530 Subject: feat(api): serve cached thumbnails --- internal/api/new.go | 9 ++++++-- internal/api/server.go | 4 ++-- internal/api/thumb.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 internal/api/thumb.go diff --git a/internal/api/new.go b/internal/api/new.go index 7066d90..e2399e5 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -16,8 +16,8 @@ func New(config Config) *Server { mux: mux, http: server, - gallery: config.Gallery, - thumbnail: config.Thumbnails, + gallery: config.Gallery, + thumbnails: config.Thumbnails, } srv.mux.HandleFunc( @@ -30,5 +30,10 @@ func New(config Config) *Server { srv.handlePhotos, ) + srv.mux.HandleFunc( + "GET /thumb/{id}", + srv.handleThumbnail, + ) + return srv } diff --git a/internal/api/server.go b/internal/api/server.go index 2bec8e2..ad2d776 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -14,6 +14,6 @@ type Server struct { mux *http.ServeMux http *http.Server - gallery service.GalleryService - thumbnail *thumbnail.Generator + gallery service.GalleryService + thumbnails *thumbnail.Generator } diff --git a/internal/api/thumb.go b/internal/api/thumb.go new file mode 100644 index 0000000..0e18ade --- /dev/null +++ b/internal/api/thumb.go @@ -0,0 +1,58 @@ +package api + +import ( + "errors" + "log" + "net/http" + + "github.com/RanXom/galleryd/internal/service" +) + +func (s *Server) handleThumbnail( + w http.ResponseWriter, + r *http.Request, +) { + id := r.PathValue("id") + + photo, err := s.gallery.Photo(r.Context(), id) + if err != nil { + if errors.Is(err, service.ErrPhotoNotFound) { + writeError( + w, + http.StatusNotFound, + "photo not found", + ) + return + } + + log.Printf("lookup photo: %v", err) + + writeError( + w, + http.StatusInternalServerError, + "failed to lookup photo", + ) + return + } + + thumb, err := s.thumbnails.Generate( + r.Context(), + photo, + ) + if err != nil { + log.Printf("generate thumbnail: %v", err) + + writeError( + w, + http.StatusInternalServerError, + "failed to generate thumbnail", + ) + return + } + + http.ServeFile( + w, + r, + thumb.Path, + ) +} -- cgit v1.2.3 From 887d212288db292dbbc557191ef1507b70ca8507 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:30:02 +0530 Subject: refactor(api): abstract thumbnail generator --- cmd/galleryd/main.go | 3 ++- internal/api/api_test.go | 27 +++++++++++++++++++++++++++ internal/api/server.go | 3 +-- internal/api/thumbnail.go | 16 ++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 internal/api/api_test.go create mode 100644 internal/api/thumbnail.go diff --git a/cmd/galleryd/main.go b/cmd/galleryd/main.go index 5544544..41b62cf 100644 --- a/cmd/galleryd/main.go +++ b/cmd/galleryd/main.go @@ -5,6 +5,7 @@ import ( "log" "os" "os/signal" + "path/filepath" "syscall" "github.com/RanXom/galleryd/internal/api" @@ -25,7 +26,7 @@ func main() { scanner := scanner.New(scanner.Config{ Roots: []string{ - ".", + filepath.Join(os.Getenv("HOME"), "Pictures"), }, }) diff --git a/internal/api/api_test.go b/internal/api/api_test.go new file mode 100644 index 0000000..3940f6e --- /dev/null +++ b/internal/api/api_test.go @@ -0,0 +1,27 @@ +package api + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestHealth(t *testing.T) { + server := New(Config{ + Address: ":0", + }) + + req := httptest.NewRequest( + http.MethodGet, + "/health", + nil, + ) + + rec := httptest.NewRecorder() + + server.mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d", rec.Code) + } +} diff --git a/internal/api/server.go b/internal/api/server.go index ad2d776..b1daf0d 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -4,7 +4,6 @@ import ( "net/http" "github.com/RanXom/galleryd/internal/service" - "github.com/RanXom/galleryd/internal/thumbnail" ) // Server exposes the gallery over HTTP @@ -15,5 +14,5 @@ type Server struct { http *http.Server gallery service.GalleryService - thumbnails *thumbnail.Generator + thumbnails ThumbnailGenerator } diff --git a/internal/api/thumbnail.go b/internal/api/thumbnail.go new file mode 100644 index 0000000..9cea4df --- /dev/null +++ b/internal/api/thumbnail.go @@ -0,0 +1,16 @@ +package api + +import ( + "context" + + "github.com/RanXom/galleryd/internal/gallery" + "github.com/RanXom/galleryd/internal/thumbnail" +) + +// ThumbnailGenerator generates thumbnails. +type ThumbnailGenerator interface { + Generate( + ctx context.Context, + photo gallery.Photo, + ) (thumbnail.Thumbnail, error) +} -- cgit v1.2.3 From b0857d4bf6f899426c5e1a115a60799332636afc Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:35:37 +0530 Subject: test(api): add photos endpoint tests --- internal/api/api_test.go | 107 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 5 deletions(-) diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 3940f6e..c54514d 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -1,27 +1,124 @@ package api import ( + "context" + "encoding/json" "net/http" "net/http/httptest" "testing" + "time" + + "github.com/RanXom/galleryd/internal/gallery" + "github.com/RanXom/galleryd/internal/metadata" + "github.com/RanXom/galleryd/internal/service" ) -func TestHealth(t *testing.T) { - server := New(Config{ +type fakeGalleryService struct { + photos []gallery.Photo + err error +} + +func (f fakeGalleryService) Load(ctx context.Context) error { + return nil +} + +func (f fakeGalleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { + return f.photos, f.err +} + +func (f fakeGalleryService) Photo( + ctx context.Context, + id string, +) (gallery.Photo, error) { + for _, photo := range f.photos { + if photo.ID == id { + return photo, nil + } + } + + return gallery.Photo{}, service.ErrPhotoNotFound +} + +func TestPhotos(t *testing.T) { + srv := New(Config{ Address: ":0", + + Gallery: fakeGalleryService{ + photos: []gallery.Photo{ + { + ID: "abc123", + + Metadata: metadata.Metadata{ + Width: 100, + Height: 50, + DateTaken: time.Date( + 2025, + 1, + 1, + 12, + 0, + 0, + 0, + time.UTC, + ), + }, + }, + }, + }, }) req := httptest.NewRequest( http.MethodGet, - "/health", + "/api/photos", nil, ) rec := httptest.NewRecorder() - server.mux.ServeHTTP(rec, req) + srv.mux.ServeHTTP(rec, req) if rec.Code != http.StatusOK { - t.Fatalf("expected 200, got %d", rec.Code) + t.Fatalf( + "expected %d, got %d", + http.StatusOK, + rec.Code, + ) + } + + var response []photoResponse + + if err := json.NewDecoder(rec.Body).Decode(&response); err != nil { + t.Fatalf("decode response: %v", err) + } + + if len(response) != 1 { + t.Fatalf( + "expected 1 photo, got %d", + len(response), + ) + } + + photo := response[0] + + if photo.ID != "abc123" { + t.Fatalf( + "expected id %q, got %q", + "abc123", + photo.ID, + ) + } + + if photo.ThumbnailURL != "/thumb/abc123" { + t.Fatalf( + "unexpected thumbnail url: %q", + photo.ThumbnailURL, + ) + } + + if photo.PhotoURL != "/photo/abc123" { + t.Fatalf( + "unexpected photo url: %q", + photo.PhotoURL, + ) } } -- cgit v1.2.3 From 5f9a07afe0fd42cd9870c864eb30941343bf51e9 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:39:06 +0530 Subject: test(api): add thumbnail endpoint tests --- internal/api/api_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ internal/api/config.go | 3 +-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/internal/api/api_test.go b/internal/api/api_test.go index c54514d..c1980fd 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -11,6 +11,7 @@ import ( "github.com/RanXom/galleryd/internal/gallery" "github.com/RanXom/galleryd/internal/metadata" "github.com/RanXom/galleryd/internal/service" + "github.com/RanXom/galleryd/internal/thumbnail" ) type fakeGalleryService struct { @@ -18,6 +19,24 @@ type fakeGalleryService struct { err error } +type fakeThumbnailGenerator struct { + path string + err error +} + +func (f fakeThumbnailGenerator) Generate( + ctx context.Context, + photo gallery.Photo, +) (thumbnail.Thumbnail, error) { + if f.err != nil { + return thumbnail.Thumbnail{}, f.err + } + + return thumbnail.Thumbnail{ + Path: f.path, + }, nil +} + func (f fakeGalleryService) Load(ctx context.Context) error { return nil } @@ -122,3 +141,46 @@ func TestPhotos(t *testing.T) { ) } } + +func TestThumbnail(t *testing.T) { + srv := New(Config{ + Address: ":0", + + Gallery: fakeGalleryService{ + photos: []gallery.Photo{ + { + ID: "abc123", + }, + }, + }, + + Thumbnails: fakeThumbnailGenerator{ + path: "../scanner/testdata/gallery/vac/image.webp", + }, + }) + + req := httptest.NewRequest( + http.MethodGet, + "/thumb/abc123", + nil, + ) + + rec := httptest.NewRecorder() + + srv.mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf( + "expected %d, got %d", + http.StatusOK, + rec.Code, + ) + } + + if got := rec.Header().Get("Content-Type"); got != "image/webp" { + t.Fatalf( + "expected image/webp, got %q", + got, + ) + } +} diff --git a/internal/api/config.go b/internal/api/config.go index a9a72ae..04057a4 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -2,7 +2,6 @@ package api import ( "github.com/RanXom/galleryd/internal/service" - "github.com/RanXom/galleryd/internal/thumbnail" ) // Config configures the HTTP server @@ -10,5 +9,5 @@ type Config struct { Address string Gallery service.GalleryService - Thumbnails *thumbnail.Generator + Thumbnails ThumbnailGenerator } -- cgit v1.2.3 From 8e8f0bd38f0cc0ee435ae2ed392a206e1ea73a32 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:42:27 +0530 Subject: feat(api): serve original photos --- internal/api/new.go | 5 +++++ internal/api/photo.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/internal/api/new.go b/internal/api/new.go index e2399e5..7febfe7 100644 --- a/internal/api/new.go +++ b/internal/api/new.go @@ -35,5 +35,10 @@ func New(config Config) *Server { srv.handleThumbnail, ) + srv.mux.HandleFunc( + "GET /photo/{id}", + srv.handlePhoto, + ) + return srv } diff --git a/internal/api/photo.go b/internal/api/photo.go index 7174491..37f9587 100644 --- a/internal/api/photo.go +++ b/internal/api/photo.go @@ -1,6 +1,13 @@ package api -import "time" +import ( + "errors" + "log" + "net/http" + "time" + + "github.com/RanXom/galleryd/internal/service" +) type photoResponse struct { ID string `json:"id"` @@ -10,3 +17,37 @@ type photoResponse struct { ThumbnailURL string `json:"thumbnail"` PhotoURL string `json:"photo"` } + +func (s *Server) handlePhoto( + w http.ResponseWriter, + r *http.Request, +) { + id := r.PathValue("id") + + photo, err := s.gallery.Photo(r.Context(), id) + if err != nil { + if errors.Is(err, service.ErrPhotoNotFound) { + writeError( + w, + http.StatusNotFound, + "photo not found", + ) + return + } + + log.Printf("lookup photo: %v", err) + + writeError( + w, + http.StatusInternalServerError, + "failed to lookup photo", + ) + return + } + + http.ServeFile( + w, + r, + photo.Path, + ) +} -- cgit v1.2.3