Different file type limiting and bug fixes

This commit is contained in:
2025-03-14 00:07:25 +02:00
parent 07720a3a2b
commit fcfa202162

View File

@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"mime"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@@ -41,11 +42,13 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ
if err != nil { if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't parse multipart form", err) respondWithError(w, http.StatusBadRequest, "Couldn't parse multipart form", err)
return
} }
image, header, err := r.FormFile("thumbnail") image, header, err := r.FormFile("thumbnail")
if err != nil { if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't get file", err) respondWithError(w, http.StatusBadRequest, "Couldn't get file", err)
return
} }
mediaType := header.Header.Get("Content-Type") mediaType := header.Header.Get("Content-Type")
@@ -53,33 +56,37 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ
imageData, err := io.ReadAll(image) imageData, err := io.ReadAll(image)
if err != nil { if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't read image", err) respondWithError(w, http.StatusBadRequest, "Couldn't read image", err)
return
} }
video, err := cfg.db.GetVideo(videoID) video, err := cfg.db.GetVideo(videoID)
if err != nil { if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't find video", err) respondWithError(w, http.StatusBadRequest, "Couldn't find video", err)
return
} }
if video.UserID != userID { if video.UserID != userID {
respondWithError(w, http.StatusUnauthorized, "You are not authorized to upload this video", err) respondWithError(w, http.StatusUnauthorized, "You are not authorized to upload this video", err)
return
} }
var extension string var extension string
switch mediaType { mimeType, _, err := mime.ParseMediaType(mediaType)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't parse multipart form", err)
return
}
switch mimeType {
case "image/jpeg": case "image/jpeg":
extension = ".jpg" extension = ".jpg"
break break
case "image/png": case "image/png":
extension = ".png" extension = ".png"
break break
case "image/gif":
extension = ".gif"
break
case "image/webp":
extension = ".webp"
break
default: default:
respondWithError(w, http.StatusBadRequest, "Couldn't recognize file type", err) respondWithError(w, http.StatusBadRequest, "Couldn't recognize file type", err)
return
} }
path := filepath.Join(cfg.assetsRoot, videoIDString+"."+extension) path := filepath.Join(cfg.assetsRoot, videoIDString+"."+extension)
@@ -87,11 +94,13 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ
f, err := os.Create(path) f, err := os.Create(path)
if err != nil { if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create file", err) respondWithError(w, http.StatusInternalServerError, "Couldn't create file", err)
return
} }
_, err = io.Copy(f, bytes.NewReader(imageData)) _, err = io.Copy(f, bytes.NewReader(imageData))
if err != nil { if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create file", err) respondWithError(w, http.StatusInternalServerError, "Couldn't create file", err)
return
} }
url := fmt.Sprintf("http://localhost:%s/assets/%s.%s", cfg.port, videoID, extension) url := fmt.Sprintf("http://localhost:%s/assets/%s.%s", cfg.port, videoID, extension)
@@ -101,11 +110,13 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ
err = cfg.db.UpdateVideo(video) err = cfg.db.UpdateVideo(video)
if err != nil { if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't update video", err) respondWithError(w, http.StatusBadRequest, "Couldn't update video", err)
return
} }
video, err = cfg.db.GetVideo(videoID) video, err = cfg.db.GetVideo(videoID)
if err != nil { if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't find video", err) respondWithError(w, http.StatusBadRequest, "Couldn't find video", err)
return
} }
respondWithJSON(w, http.StatusOK, video) respondWithJSON(w, http.StatusOK, video)