forked from dionyself/golang-cms
/
view.go
189 lines (170 loc) · 4.79 KB
/
view.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package controllers
import (
"log"
"os"
"strconv"
"strings"
"github.com/chuongnx/beego"
"github.com/chuongnx/golang-cms/models"
"github.com/chuongnx/golang-cms/utils"
)
// ArticleController ...
type ViewController struct {
BaseController
}
// Get Displays Article by id
func (CTRL *ViewController) Get() {
ArtID, err := strconv.Atoi(CTRL.Ctx.Input.Param(":id"))
if err != nil {
CTRL.Abort("403")
}
FileID, errfile := strconv.Atoi(CTRL.Ctx.Input.Param(":fileId"))
if errfile != nil {
CTRL.Abort("403")
}
db := CTRL.GetDB("default")
if ArtID != 0 {
Art := new(models.Article)
Art.Id = ArtID
db.Read(Art, "Id")
CTRL.Data["Article"] = Art
var ArtFile []models.ArticleFile
num, err := db.QueryTable("article_file").Filter("article_id__contains", ArtID).OrderBy("code").All(&ArtFile)
println("ArtFile.num", FileID, num, err)
if FileID == 0 {
if Art.Type == 0 {
//Art.FileName = "IndependenceDayResurgence2016720pHDRip.mp4"
for _, row := range ArtFile {
//fmt.Println(row)
Art.FileName, err = utils.Encrypt(row.FileName, []byte("phimphimphimphimphimphimphimphim"))
if err != nil {
// TODO: Properly handle error
log.Fatal(err)
}
}
} else {
for _, row := range ArtFile {
Art.FileName, err = utils.Encrypt(row.FileName, []byte("phimphimphimphimphimphimphimphim"))
if err != nil {
// TODO: Properly handle error
log.Fatal(err)
}
break
}
}
} else {
for _, row := range ArtFile {
if row.Id == FileID {
Art.FileName, err = utils.Encrypt(row.FileName, []byte("phimphimphimphimphimphimphimphim"))
if err != nil {
// TODO: Properly handle error
log.Fatal(err)
}
break
}
}
}
CTRL.Data["FileName"] = Art.FileName + ".mp4"
CTRL.Data["ArticleFile"] = ArtFile
CTRL.ConfigPage("view.html")
}
}
func (CTRL *ViewController) Video() {
const bufferSize = 1024 * 100
videoid := CTRL.Ctx.Input.Param(":videoid")
arrvideoid := strings.Split(videoid, ".")
filename, err := utils.Decrypt(arrvideoid[0], []byte("phimphimphimphimphimphimphimphim"))
if err != nil {
CTRL.Abort("403")
}
videodir := beego.AppConfig.String("VideoDir")
fullPath := videodir + filename
println("path", fullPath)
file, err := os.Open(fullPath)
if err != nil {
CTRL.Abort("500")
return
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
CTRL.Abort("500")
return
}
fileSize := int(fi.Size())
if len(CTRL.Ctx.Input.Header("Range")) == 0 {
contentLength := strconv.Itoa(fileSize)
contentEnd := strconv.Itoa(fileSize - 1)
CTRL.Ctx.ResponseWriter.Header().Set("Content-Type", "video/mp4")
CTRL.Ctx.ResponseWriter.Header().Set("Accept-Ranges", "bytes")
CTRL.Ctx.ResponseWriter.Header().Set("Content-Length", contentLength)
CTRL.Ctx.ResponseWriter.Header().Set("Content-Range", "bytes 0-"+contentEnd+"/"+contentLength)
CTRL.Ctx.ResponseWriter.WriteHeader(200)
buffer := make([]byte, bufferSize)
for {
n, err := file.Read(buffer)
if n == 0 {
break
}
if err != nil {
break
}
data := buffer[:n]
//w.Write(data)
//w.(http.Flusher).Flush()
CTRL.Ctx.ResponseWriter.Write(data)
CTRL.Ctx.ResponseWriter.Flush()
}
} else {
rangeParam := strings.Split(CTRL.Ctx.Input.Header("Range"), "=")[1]
splitParams := strings.Split(rangeParam, "-")
// response values
contentStartValue := 0
contentStart := strconv.Itoa(contentStartValue)
contentEndValue := fileSize - 1
contentEnd := strconv.Itoa(contentEndValue)
contentSize := strconv.Itoa(fileSize)
if len(splitParams) > 0 {
contentStartValue, err = strconv.Atoi(splitParams[0])
if err != nil {
contentStartValue = 0
}
contentStart = strconv.Itoa(contentStartValue)
}
if len(splitParams) > 1 {
contentEndValue, err = strconv.Atoi(splitParams[1])
if err != nil {
contentEndValue = fileSize - 1
}
contentEnd = strconv.Itoa(contentEndValue)
}
contentLength := strconv.Itoa(contentEndValue - contentStartValue + 1)
CTRL.Ctx.ResponseWriter.Header().Set("Content-Type", "video/mp4")
CTRL.Ctx.ResponseWriter.Header().Set("Accept-Ranges", "bytes")
CTRL.Ctx.ResponseWriter.Header().Set("Content-Length", contentLength)
CTRL.Ctx.ResponseWriter.Header().Set("Content-Range", "bytes "+contentStart+"-"+contentEnd+"/"+contentSize)
CTRL.Ctx.ResponseWriter.WriteHeader(206)
buffer := make([]byte, bufferSize)
file.Seek(int64(contentStartValue), 0)
writeBytes := 0
for {
n, err := file.Read(buffer)
writeBytes += n
if n == 0 {
break
}
if err != nil {
break
}
if writeBytes >= contentEndValue {
data := buffer[:bufferSize-writeBytes+contentEndValue+1]
CTRL.Ctx.ResponseWriter.Write(data)
CTRL.Ctx.ResponseWriter.Flush()
break
}
data := buffer[:n]
CTRL.Ctx.ResponseWriter.Write(data)
CTRL.Ctx.ResponseWriter.Flush()
}
}
}