diff options
Diffstat (limited to 'internal/api')
| -rw-r--r-- | internal/api/api_test.go | 186 | ||||
| -rw-r--r-- | internal/api/config.go | 7 | ||||
| -rw-r--r-- | internal/api/error.go | 23 | ||||
| -rw-r--r-- | internal/api/health.go | 6 | ||||
| -rw-r--r-- | internal/api/new.go | 21 | ||||
| -rw-r--r-- | internal/api/photo.go | 43 | ||||
| -rw-r--r-- | internal/api/photos.go | 16 | ||||
| -rw-r--r-- | internal/api/server.go | 3 | ||||
| -rw-r--r-- | internal/api/thumb.go | 58 | ||||
| -rw-r--r-- | internal/api/thumbnail.go | 16 | ||||
| -rw-r--r-- | internal/api/url.go | 9 |
11 files changed, 368 insertions, 20 deletions
diff --git a/internal/api/api_test.go b/internal/api/api_test.go new file mode 100644 index 0000000..c1980fd --- /dev/null +++ b/internal/api/api_test.go @@ -0,0 +1,186 @@ +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" + "github.com/RanXom/galleryd/internal/thumbnail" +) + +type fakeGalleryService struct { + photos []gallery.Photo + 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 +} + +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, + "/api/photos", + nil, + ) + + rec := httptest.NewRecorder() + + srv.mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + 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, + ) + } +} + +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 d82147c..04057a4 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -1,10 +1,13 @@ package api -import "github.com/RanXom/galleryd/internal/service" +import ( + "github.com/RanXom/galleryd/internal/service" +) // Config configures the HTTP server type Config struct { Address string - Gallery service.GalleryService + Gallery service.GalleryService + Thumbnails ThumbnailGenerator } 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/new.go b/internal/api/new.go index a7c122b..7febfe7 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, + thumbnails: config.Thumbnails, } srv.mux.HandleFunc( @@ -27,5 +30,15 @@ func New(config Config) *Server { srv.handlePhotos, ) + srv.mux.HandleFunc( + "GET /thumb/{id}", + 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, + ) +} diff --git a/internal/api/photos.go b/internal/api/photos.go index 2d492b1..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 } @@ -30,18 +30,20 @@ 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), }) } 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", ) } } diff --git a/internal/api/server.go b/internal/api/server.go index c12c5bd..b1daf0d 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -13,5 +13,6 @@ type Server struct { mux *http.ServeMux http *http.Server - gallery service.GalleryService + gallery service.GalleryService + thumbnails ThumbnailGenerator } 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, + ) +} 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) +} 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 +} |