aboutsummaryrefslogtreecommitdiff
path: root/internal/api/photo.go
blob: 37f9587f9350814cfe976840f4cae2bd8514df58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package api

import (
	"errors"
	"log"
	"net/http"
	"time"

	"github.com/RanXom/galleryd/internal/service"
)

type photoResponse struct {
	ID           string    `json:"id"`
	DateTaken    time.Time `json:"dateTaken"`
	Width        int       `json:"width"`
	Height       int       `json:"height"`
	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,
	)
}