aboutsummaryrefslogtreecommitdiff
path: root/internal/api/thumb.go
blob: 0e18adede9bd8be1a4f4271242b874d111e489d7 (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
54
55
56
57
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,
	)
}