forked from bootdotdev/learn-file-storage-s3-golang-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_upload_video.go
145 lines (124 loc) · 3.59 KB
/
handler_upload_video.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"fmt"
"io"
"mime"
"net/http"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/auth"
"github.com/google/uuid"
)
func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request) {
uploadLimit := 1 << 30
r.Body = http.MaxBytesReader(w, r.Body, int64(uploadLimit))
defer r.Body.Close()
videoString := r.PathValue("videoID")
videoID, err := uuid.Parse(videoString)
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
}
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
}
file, fileHeader, err := r.FormFile("video")
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't parse the video", err)
return
}
defer file.Close()
mediatype := fileHeader.Header.Get("Content-Type")
mediatype, _, err = mime.ParseMediaType(mediatype)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't parse media type", err)
return
}
if mediatype != "video/mp4" {
respondWithError(w, http.StatusBadRequest, "Wrong file type, need mp4", err)
return
}
fileExtention := strings.Split(mediatype, "/")
tmpFile, err := os.CreateTemp("", "tubely-upload.mp4")
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't create temp file", err)
return
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
_, err = io.Copy(tmpFile, file)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't copy file to tmp file", err)
return
}
_, err = tmpFile.Seek(0, io.SeekStart)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't seeking file", err)
return
}
aspectRatio, err := getVideoAspectRatio(tmpFile.Name())
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't get the video ratio", err)
return
}
fileKey, err := auth.MakeFileKey(fileExtention[1])
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't make file key", err)
return
}
switch aspectRatio {
case "16/9":
fileKey = "landscape/" + fileKey
case "9/16":
fileKey = "portrait/" + fileKey
default:
fileKey = "other/" + fileKey
}
outputPath, err := processVideoForFastStart(tmpFile.Name())
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't process video for fast start", err)
return
}
processedFile, err := os.Open(outputPath)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't open file", err)
return
}
defer processedFile.Close()
_, err = cfg.s3Client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: &cfg.s3Bucket,
Key: &fileKey,
Body: processedFile,
ContentType: &mediatype,
})
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't put video on s3", err)
return
}
videoURL := fmt.Sprintf("https://%s/%s", cfg.s3CfDistribution, fileKey)
video.VideoURL = &videoURL
err = cfg.db.UpdateVideo(video)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Couldn't update the video", err)
return
}
respondWithJSON(w, http.StatusOK, video)
}