forked from qiniu/qshell
/
m3u8.go
242 lines (229 loc) · 6.14 KB
/
m3u8.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package qshell
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"qiniu/api.v6/auth/digest"
"qiniu/api.v6/conf"
fio "qiniu/api.v6/io"
"qiniu/api.v6/rs"
"qiniu/rpc"
"strings"
"time"
)
func M3u8FileList(mac *digest.Mac, bucket string, m3u8Key string) (slicesToDelete []rs.EntryPath, err error) {
client := rs.NewMac(mac)
//check m3u8 file exists
_, sErr := client.Stat(nil, bucket, m3u8Key)
if sErr != nil {
if v, ok := sErr.(*rpc.ErrorInfo); ok {
err = fmt.Errorf("stat m3u8 file error, %s", v.Err)
} else {
err = fmt.Errorf("stat m3u8 file error, %s", sErr)
}
return
}
//get domain list of bucket
bucketDomains, gErr := GetDomainsOfBucket(mac, bucket)
if gErr != nil {
err = gErr
return
}
if len(bucketDomains) == 0 {
err = errors.New("no domain found for the bucket")
return
}
var domain string
for _, d := range bucketDomains {
if strings.HasSuffix(d.Domain, "qiniudn.com") ||
strings.HasSuffix(d.Domain, "clouddn.com") ||
strings.HasSuffix(d.Domain, "qiniucdn.com") {
domain = d.Domain
break
}
}
//get first
if domain == "" {
domain = bucketDomains[0].Domain
}
if domain == "" {
err = errors.New("no valid domain found for the bucket")
return
}
//create downoad link
dnLink := fmt.Sprintf("http://%s/%s", domain, m3u8Key)
dnLink, _ = PrivateUrl(mac, dnLink, time.Now().Add(time.Second*3600).Unix())
//get m3u8 file content
dnLink = strings.Replace(dnLink, fmt.Sprintf("http://%s", domain), conf.IO_HOST, -1)
m3u8Req, reqErr := http.NewRequest("GET", dnLink, nil)
if reqErr != nil {
err = fmt.Errorf("new request for url %s error, %s", dnLink, reqErr)
return
}
m3u8Req.Host = domain
m3u8Resp, m3u8Err := http.DefaultClient.Do(m3u8Req)
if m3u8Err != nil {
err = fmt.Errorf("open url %s error, %s", dnLink, m3u8Err)
return
}
defer m3u8Resp.Body.Close()
if m3u8Resp.StatusCode != 200 {
err = fmt.Errorf("download m3u8 file error, %s", m3u8Resp.Status)
return
}
m3u8Bytes, readErr := ioutil.ReadAll(m3u8Resp.Body)
if readErr != nil {
err = fmt.Errorf("read m3u8 file content error, %s", readErr.Error())
return
}
//check content
if !strings.HasPrefix(string(m3u8Bytes), "#EXTM3U") {
err = errors.New("invalid m3u8 file")
return
}
slicesToDelete = make([]rs.EntryPath, 0)
bReader := bufio.NewScanner(bytes.NewReader(m3u8Bytes))
bReader.Split(bufio.ScanLines)
for bReader.Scan() {
line := strings.TrimSpace(bReader.Text())
if !strings.HasPrefix(line, "#") {
var sliceKey string
if strings.HasPrefix(line, "http://") ||
strings.HasPrefix(line, "https://") {
uri, pErr := url.Parse(line)
if pErr != nil {
fmt.Println("invalid url,", line)
continue
}
sliceKey = strings.TrimPrefix(uri.Path, "/")
} else {
sliceKey = strings.TrimPrefix(line, "/")
}
//append to delete list
slicesToDelete = append(slicesToDelete, rs.EntryPath{bucket, sliceKey})
}
}
slicesToDelete = append(slicesToDelete, rs.EntryPath{bucket, m3u8Key})
return
}
//replace and upload
func M3u8ReplaceDomain(mac *digest.Mac, bucket string, m3u8Key string, newDomain string) (err error) {
client := rs.NewMac(mac)
//check m3u8 file exists
_, sErr := client.Stat(nil, bucket, m3u8Key)
if sErr != nil {
if v, ok := sErr.(*rpc.ErrorInfo); ok {
err = fmt.Errorf("stat m3u8 file error, %s", v.Err)
} else {
err = fmt.Errorf("stat m3u8 file error, %s", sErr)
}
return
}
//get domain list of bucket
bucketDomains, gErr := GetDomainsOfBucket(mac, bucket)
if gErr != nil {
err = gErr
return
}
if len(bucketDomains) == 0 {
err = errors.New("no domain found for the bucket")
return
}
var domain string
for _, d := range bucketDomains {
if strings.HasSuffix(d.Domain, "qiniudn.com") ||
strings.HasSuffix(d.Domain, "clouddn.com") ||
strings.HasSuffix(d.Domain, "qiniucdn.com") {
domain = d.Domain
break
}
}
//get first
if domain == "" {
domain = bucketDomains[0].Domain
}
if domain == "" {
err = errors.New("no valid domain found for the bucket")
return
}
//create downoad link
dnLink := fmt.Sprintf("http://%s/%s", domain, m3u8Key)
dnLink, _ = PrivateUrl(mac, dnLink, time.Now().Add(time.Second*3600).Unix())
//get m3u8 file content
dnLink = strings.Replace(dnLink, fmt.Sprintf("http://%s", domain), conf.IO_HOST, -1)
m3u8Req, reqErr := http.NewRequest("GET", dnLink, nil)
if reqErr != nil {
err = fmt.Errorf("new request for url %s error, %s", dnLink, reqErr)
return
}
m3u8Req.Host = domain
m3u8Resp, m3u8Err := http.DefaultClient.Do(m3u8Req)
if m3u8Err != nil {
err = fmt.Errorf("open url %s error, %s", dnLink, m3u8Err)
return
}
defer m3u8Resp.Body.Close()
if m3u8Resp.StatusCode != 200 {
err = fmt.Errorf("download m3u8 file error, %s", m3u8Resp.Status)
return
}
m3u8Bytes, readErr := ioutil.ReadAll(m3u8Resp.Body)
if readErr != nil {
err = fmt.Errorf("read m3u8 file content error, %s", readErr.Error())
return
}
//check content
if !strings.HasPrefix(string(m3u8Bytes), "#EXTM3U") {
err = errors.New("invalid m3u8 file")
return
}
newM3u8Lines := make([]string, 0, 200)
var newLine string
bReader := bufio.NewScanner(bytes.NewReader(m3u8Bytes))
bReader.Split(bufio.ScanLines)
for bReader.Scan() {
line := strings.TrimSpace(bReader.Text())
if !strings.HasPrefix(line, "#") {
if strings.HasPrefix(line, "http://") ||
strings.HasPrefix(line, "https://") {
uri, pErr := url.Parse(line)
if pErr != nil {
fmt.Println("invalid url,", line)
continue
}
if newDomain != "" {
newLine = fmt.Sprintf("%s%s", newDomain, uri.Path)
} else {
newLine = uri.Path
}
} else {
if newDomain != "" {
newLine = fmt.Sprintf("%s%s", newDomain, line)
} else {
newLine = line
}
}
} else {
newLine = line
}
newM3u8Lines = append(newM3u8Lines, newLine)
}
//join and upload
newM3u8Data := []byte(strings.Join(newM3u8Lines, "\n"))
putPolicy := rs.PutPolicy{
Scope: fmt.Sprintf("%s:%s", bucket, m3u8Key),
}
upToken := putPolicy.Token(mac)
putClient := rpc.NewClient("")
putErr := fio.Put2(putClient, nil, nil, upToken, m3u8Key, bytes.NewReader(newM3u8Data),
int64(len(newM3u8Data)), nil)
if putErr != nil {
err = putErr
return
}
return
}