forked from bootdotdev/learn-file-storage-s3-golang-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_upload_thumbnail.go
More file actions
121 lines (104 loc) · 3.23 KB
/
handler_upload_thumbnail.go
File metadata and controls
121 lines (104 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"mime"
"net/http"
"os"
"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)
// TODO: implement the upload here
const maxMemory = 10 * 1024 * 1024 // 10mb
err = r.ParseMultipartForm(maxMemory) // 10mb
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't parse multipart form", err)
return
}
formFile, fileHeader, err := r.FormFile("thumbnail")
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't get thumbnail file", err)
return
}
defer formFile.Close()
mediaType := fileHeader.Header.Get("Content-Type")
fileExtensions := map[string]string{
"image/jpeg": ".jpg",
"image/png": ".png",
}
mimeType, _, err := mime.ParseMediaType(mediaType)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Unsupported media type", err)
return
}
if mimeType != "image/jpeg" && mimeType != "image/png" && mimeType != "image/gif" && mimeType != "image/webp" && mimeType != "image/svg+xml" {
respondWithError(w, http.StatusBadRequest, "Unsupported media type", nil)
return
}
fileExtension, ok := fileExtensions[mimeType]
if !ok {
respondWithError(w, http.StatusBadRequest, "Unsupported media type", nil)
return
}
randomBytes := make([]byte, 32)
_, err = rand.Read(randomBytes)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't generate random string", err)
return
}
fileName := hex.EncodeToString(randomBytes)
filePath := cfg.assetsRoot + "/" + fileName + fileExtension
file, err := os.Create(filePath)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create file", err)
return
}
defer file.Close()
_, err = io.Copy(file, formFile)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't copy file", err)
return
}
video, err := cfg.db.GetVideo(videoID)
if err != nil {
respondWithError(w, http.StatusNotFound, "Couldn't find video", err)
return
}
if video.UserID != userID {
respondWithError(w, http.StatusForbidden, "You are not authorized to upload a thumbnail for this video", nil)
return
}
cleanFilePath := filePath
if len(cleanFilePath) > 0 && cleanFilePath[0] == '.' {
cleanFilePath = cleanFilePath[1:]
}
thumbnailURL := "http://localhost:" + cfg.port + cleanFilePath
nextVideo := video
nextVideo.ThumbnailURL = &thumbnailURL
err = cfg.db.UpdateVideo(nextVideo)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't update video", err)
return
}
respondWithJSON(w, http.StatusOK, nextVideo)
}