Files
learn-file-storage-s3-golan…/handler_get_thumbnail.go
Daniel Hjartland 75be79c6cf initial commit
2025-01-02 16:32:57 +01:00

33 lines
727 B
Go

package main
import (
"fmt"
"net/http"
"github.com/google/uuid"
)
func (cfg *apiConfig) handlerThumbnailGet(w http.ResponseWriter, r *http.Request) {
videoIDString := r.PathValue("videoID")
videoID, err := uuid.Parse(videoIDString)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid video ID", err)
return
}
tn, ok := videoThumbnails[videoID]
if !ok {
respondWithError(w, http.StatusNotFound, "Thumbnail not found", nil)
return
}
w.Header().Set("Content-Type", tn.mediaType)
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(tn.data)))
_, err = w.Write(tn.data)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Error writing response", err)
return
}
}