Files
learn-file-storage-s3-golan…/handler_upload_thumbnail.go

134 lines
3.1 KiB
Go

package main
import (
"bytes"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"mime"
"net/http"
"os"
"path/filepath"
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/auth"
"github.com/google/uuid"
)
func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Request) {
videoIDString := r.PathValue("videoID")
videoID, err := uuid.Parse(videoIDString)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid ID", err)
return
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find JWT", err)
return
}
userID, err := auth.ValidateJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't validate JWT", err)
return
}
fmt.Println("uploading thumbnail for video", videoID, "by user", userID)
var maxMemory int64
maxMemory = 10 << 20
err = r.ParseMultipartForm(maxMemory)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't parse multipart form", err)
return
}
image, header, err := r.FormFile("thumbnail")
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't get file", err)
return
}
mediaType := header.Header.Get("Content-Type")
imageData, err := io.ReadAll(image)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't read image", err)
return
}
video, err := cfg.db.GetVideo(videoID)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't find video", err)
return
}
if video.UserID != userID {
respondWithError(w, http.StatusUnauthorized, "You are not authorized to upload this video", err)
return
}
var extension string
mimeType, _, err := mime.ParseMediaType(mediaType)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't parse multipart form", err)
return
}
switch mimeType {
case "image/jpeg":
extension = ".jpg"
break
case "image/png":
extension = ".png"
break
default:
respondWithError(w, http.StatusBadRequest, "Couldn't recognize file type", err)
return
}
thumbnailName := make([]byte, 32)
_, err = rand.Read(thumbnailName)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't generate random name", err)
}
thumbnailPathName := base64.RawURLEncoding.EncodeToString(thumbnailName)
path := filepath.Join(cfg.assetsRoot, thumbnailPathName+extension)
f, err := os.Create(path)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create file", err)
return
}
_, err = io.Copy(f, bytes.NewReader(imageData))
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create file", err)
return
}
url := fmt.Sprintf("http://localhost:%s/assets/%s%s", cfg.port, thumbnailPathName, extension)
video.ThumbnailURL = &url
err = cfg.db.UpdateVideo(video)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't update video", err)
return
}
video, err = cfg.db.GetVideo(videoID)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't find video", err)
return
}
respondWithJSON(w, http.StatusOK, video)
}