Saving thumbnails on disk
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/auth"
|
||||
"github.com/google/uuid"
|
||||
@@ -60,14 +63,38 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ
|
||||
respondWithError(w, http.StatusUnauthorized, "You are not authorized to upload this video", err)
|
||||
}
|
||||
|
||||
thumb := thumbnail{
|
||||
data: imageData,
|
||||
mediaType: mediaType,
|
||||
var extension string
|
||||
|
||||
switch mediaType {
|
||||
case "image/jpeg":
|
||||
extension = ".jpg"
|
||||
break
|
||||
case "image/png":
|
||||
extension = ".png"
|
||||
break
|
||||
case "image/gif":
|
||||
extension = ".gif"
|
||||
break
|
||||
case "image/webp":
|
||||
extension = ".webp"
|
||||
break
|
||||
default:
|
||||
respondWithError(w, http.StatusBadRequest, "Couldn't recognize file type", err)
|
||||
}
|
||||
|
||||
videoThumbnails[videoID] = thumb
|
||||
path := filepath.Join(cfg.assetsRoot, videoIDString+"."+extension)
|
||||
|
||||
url := "http://localhost:" + cfg.port + "/api/thumbnails/" + videoID.String()
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
respondWithError(w, http.StatusInternalServerError, "Couldn't create file", err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(f, bytes.NewReader(imageData))
|
||||
if err != nil {
|
||||
respondWithError(w, http.StatusInternalServerError, "Couldn't create file", err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("http://localhost:%s/assets/%s.%s", cfg.port, videoID, extension)
|
||||
|
||||
video.ThumbnailURL = &url
|
||||
|
||||
|
||||
4
main.go
4
main.go
@@ -6,7 +6,6 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
_ "github.com/lib/pq"
|
||||
@@ -29,8 +28,6 @@ type thumbnail struct {
|
||||
mediaType string
|
||||
}
|
||||
|
||||
var videoThumbnails = map[uuid.UUID]thumbnail{}
|
||||
|
||||
func main() {
|
||||
godotenv.Load(".env")
|
||||
|
||||
@@ -119,7 +116,6 @@ func main() {
|
||||
mux.HandleFunc("POST /api/video_upload/{videoID}", cfg.handlerUploadVideo)
|
||||
mux.HandleFunc("GET /api/videos", cfg.handlerVideosRetrieve)
|
||||
mux.HandleFunc("GET /api/videos/{videoID}", cfg.handlerVideoGet)
|
||||
mux.HandleFunc("GET /api/thumbnails/{videoID}", cfg.handlerThumbnailGet)
|
||||
mux.HandleFunc("DELETE /api/videos/{videoID}", cfg.handlerVideoMetaDelete)
|
||||
|
||||
mux.HandleFunc("POST /admin/reset", cfg.handlerReset)
|
||||
|
||||
Reference in New Issue
Block a user