-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_media.go
98 lines (95 loc) · 2.95 KB
/
handle_media.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
package main
import (
"context"
"net/http"
"os"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// handleMediaData 함수는 영상데이터를 전송한다.
func handleMediaData(w http.ResponseWriter, r *http.Request) {
_, err := GetTokenFromHeader(w, r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
q := r.URL.Query()
id := q.Get("id")
if id == "" {
http.Error(w, "id 값이 빈 문자열 입니다", http.StatusInternalServerError)
return
}
typ := q.Get("type")
if !(typ == "mp4" || typ == "ogg" || typ == "mov" || typ == "png") {
http.Error(w, "type 값은 mp4, ogg, mov, png 값만 지원합니다", http.StatusInternalServerError)
return
}
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
item, err := GetItem(client, id)
if err != nil {
if err == mongo.ErrNoDocuments {
http.Error(w, id+" ID를 가진 아이템이 존재하지 않습니다", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
switch typ {
case "mp4":
if _, err := os.Stat(item.OutputThumbnailMp4Path); os.IsNotExist(err) {
http.Error(w, item.OutputThumbnailMp4Path+" 파일이 존재하지 않습니다", http.StatusNotFound)
return
}
http.ServeFile(w, r, item.OutputThumbnailMp4Path)
return
case "mov":
if _, err := os.Stat(item.OutputThumbnailMovPath); os.IsNotExist(err) {
http.Error(w, item.OutputThumbnailMovPath+" 파일이 존재하지 않습니다", http.StatusNotFound)
return
}
w.Header().Add("Content-Type", "video/quicktime")
w.WriteHeader(http.StatusOK)
http.ServeFile(w, r, item.OutputThumbnailMovPath)
return
case "ogg":
if _, err := os.Stat(item.OutputThumbnailOggPath); os.IsNotExist(err) {
http.Error(w, item.OutputThumbnailOggPath+" 파일이 존재하지 않습니다", http.StatusNotFound)
return
}
w.Header().Add("Content-Type", "video/ogg")
w.WriteHeader(http.StatusOK)
http.ServeFile(w, r, item.OutputThumbnailOggPath)
return
case "png":
if _, err := os.Stat(item.OutputThumbnailPngPath); os.IsNotExist(err) {
http.Error(w, item.OutputThumbnailPngPath+" 파일이 존재하지 않습니다", http.StatusNotFound)
return
}
w.Header().Add("Content-Type", "image/png")
http.ServeFile(w, r, item.OutputThumbnailPngPath)
return
default:
http.Error(w, "지원하지 않는 형식입니다", http.StatusNotFound)
return
}
}