-
Notifications
You must be signed in to change notification settings - Fork 40
/
uploadvideo.go
97 lines (70 loc) · 2.3 KB
/
uploadvideo.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
package handlers
import (
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/EngineerKamesh/gofullstack/volume4/section2/gopherface/common/utility"
)
type UploadVideoForm struct {
FieldNames []string
Fields map[string]string
Errors map[string]string
}
func DisplayUploadVideoForm(w http.ResponseWriter, r *http.Request, u *UploadVideoForm) {
RenderTemplate(w, WebAppRoot+"/templates/uploadvideoform.html", u)
}
func ProcessUploadVideo(w http.ResponseWriter, r *http.Request, u *UploadVideoForm) {
file, fileheader, err := r.FormFile("videofile")
if err != nil {
log.Println("Encountered error when attempting to read uploaded file: ", err)
}
randomFileName := utility.GenerateUUID()
if fileheader != nil {
extension := filepath.Ext(fileheader.Filename)
r.ParseMultipartForm(32 << 20)
defer file.Close()
videoFilePathWithoutExtension := "./static/uploads/videos/" + randomFileName
f, err := os.OpenFile(videoFilePathWithoutExtension+extension, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Println(err)
return
}
defer f.Close()
io.Copy(f, file)
thumbImageFilePath := videoFilePathWithoutExtension + "_thumb.png"
command := "ffmpeg -y -i " + videoFilePathWithoutExtension + extension + " -s 430x372 -f mjpeg -vframes 1 -ss 3 " + thumbImageFilePath
commandOutput, err := exec.Command("sh", "-c", command).Output()
fmt.Println("video thumbnail generation command output: ", commandOutput)
if err != nil {
log.Println(err)
return
}
m := make(map[string]string)
m["thumbnailPath"] = strings.TrimPrefix(videoFilePathWithoutExtension, ".") + "_thumb.png"
m["videoPath"] = strings.TrimPrefix(videoFilePathWithoutExtension, ".") + ".mp4"
RenderTemplate(w, WebAppRoot+"/templates/videopreview.html", m)
} else {
w.Write([]byte("Failed to process uploaded file!"))
}
}
func ValidateUploadVideoForm(w http.ResponseWriter, r *http.Request, u *UploadVideoForm) {
ProcessUploadVideo(w, r, u)
}
func UploadVideoHandler(w http.ResponseWriter, r *http.Request) {
u := UploadVideoForm{}
u.Fields = make(map[string]string)
u.Errors = make(map[string]string)
switch r.Method {
case "GET":
DisplayUploadVideoForm(w, r, &u)
case "POST":
ValidateUploadVideoForm(w, r, &u)
default:
DisplayUploadVideoForm(w, r, &u)
}
}