diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/api/new.go | 5 | ||||
| -rw-r--r-- | internal/api/photo.go | 43 |
2 files changed, 47 insertions, 1 deletions
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, + ) +} |