forked from bootdotdev/learn-file-storage-s3-golang-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_upload_thumbnail.go
104 lines (92 loc) · 2.76 KB
/
handler_upload_thumbnail.go
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
package main
import (
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
"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)
const maxMemory = 10 << 20
err = r.ParseMultipartForm(maxMemory)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Coundn't parse multi part form", err)
return
}
file, header, err := r.FormFile("thumbnail")
if err != nil {
respondWithError(w, http.StatusBadRequest, "Unable to parse form file", err)
return
}
defer file.Close()
mediatype := header.Header.Get("Content-Type")
mediatype, _, err = mime.ParseMediaType(mediatype)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't parse mediatype", err)
return
}
fileextention := strings.Split(mediatype, "/")
if fileextention[0] != "image" {
respondWithError(w, http.StatusBadRequest, "Invalid file type", errors.New("need image/png or image/jpeg"))
return
}
randid := make([]byte, 32)
_, err = rand.Read(randid)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't rand read", err)
return
}
fn := base64.RawURLEncoding.EncodeToString(randid)
filename := fmt.Sprintf("%s.%s", fn, fileextention[1])
dstfile, err := os.Create(filepath.Join(cfg.assetsRoot, filename))
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't create file", err)
return
}
defer dstfile.Close()
if _, err = io.Copy(dstfile, file); err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't copy file", err)
return
}
video, err := cfg.db.GetVideo(videoID)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Coundn't get video from database", err)
return
}
if video.UserID != userID {
respondWithError(w, http.StatusUnauthorized, "User ID doesn't match", err)
return
}
tURL := fmt.Sprintf("http://localhost:%v/assets/%v", cfg.port, filename)
video.ThumbnailURL = &tURL
err = cfg.db.UpdateVideo(video)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Coundn't update the video thumbnail", err)
return
}
respondWithJSON(w, http.StatusOK, video)
}