all repos — weimar @ 74fc3e1e24b47e370bcf5ea6b4e016cb50630920

Unnamed repository; edit this file 'description' to name the repository.

internal/api/thumbnail.go (view raw)

 1package api
 2
 3import (
 4	"net/http"
 5	"strconv"
 6)
 7
 8func (a *API) HandleThumbnail(w http.ResponseWriter, r *http.Request) {
 9	idStr := r.PathValue("id")
10	id, err := strconv.ParseInt(idStr, 10, 64)
11	if err != nil {
12		writeError(w, http.StatusBadRequest, "invalid image id")
13		return
14	}
15
16	img, err := a.DB.GetImageByID(r.Context(), id)
17	if err != nil {
18		writeError(w, http.StatusNotFound, "image not found")
19		return
20	}
21
22	if img.ThumbnailPath == nil || *img.ThumbnailPath == "" {
23		writeError(w, http.StatusNotFound, "thumbnail not available")
24		return
25	}
26
27	f, err := a.Storage.Open(*img.ThumbnailPath)
28	if err != nil {
29		writeError(w, http.StatusNotFound, "thumbnail not found on disk")
30		return
31	}
32	defer f.Close()
33
34	w.Header().Set("Content-Type", "image/jpeg")
35	http.ServeContent(w, r, "thumb.jpg", img.CreatedAt, f)
36}