33 lines
727 B
Go
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
|
|
}
|
|
}
|