diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/api/new.go | 9 | ||||
| -rw-r--r-- | internal/api/server.go | 4 | ||||
| -rw-r--r-- | internal/api/thumb.go | 58 |
3 files changed, 67 insertions, 4 deletions
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, + ) +} |