126 lines
3.8 KiB
Go
126 lines
3.8 KiB
Go
package web_video
|
|
|
|
import (
|
|
"bytes"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"io"
|
|
"net/http"
|
|
"videoweb/pub/common"
|
|
"videoweb/pub/common/m3u8"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/models/cfg"
|
|
"videoweb/web/webg"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type fileHandler func(ctx *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error)
|
|
|
|
func Play(ctx *gin.Context) {
|
|
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
|
m3u8Name := ctx.Param("key")
|
|
|
|
var req m3u8.Req
|
|
if err = ctx.BindQuery(&req); err != nil {
|
|
return
|
|
}
|
|
req.Name = m3u8Name
|
|
cfgSys, _ := cfg.FindOneSysCfg(ctx, bson.M{})
|
|
cdnDomain := webg.Conf.CdnDomain
|
|
if cfgSys != nil {
|
|
cdnDomain = cfgSys.MediaWebCdn
|
|
}
|
|
origDomain := webg.Conf.OrigDomain
|
|
useNewKey := ctx.Query("useNewKey") == "1" || ctx.Query("newKey") == "1"
|
|
var buff *bytes.Buffer
|
|
if useNewKey {
|
|
// 短剧原片:不追加媒资版本前缀,并使用 conf/newm3u8.key
|
|
_, buff, err = m3u8.TransformExOriginalPath(req.Name, cdnDomain, origDomain, 3, webg.Conf.TsAuthKey, webg.Conf.LsjMediaConf.M3u8Yuan, "", "/api/web/newenkey")
|
|
} else {
|
|
_, buff, err = m3u8.TransFormWithSafely(&req, cdnDomain, origDomain, true, webg.Conf.TsAuthKey, webg.Conf.LsjMediaConf.M3u8Yuan, "")
|
|
}
|
|
if err != nil {
|
|
return
|
|
}
|
|
contentType = "application/vnd.apple.mpegurl"
|
|
reader = bytes.NewReader(buff.Bytes())
|
|
contentLength = int64(buff.Len())
|
|
extraHeaders = map[string]string{}
|
|
return
|
|
}
|
|
|
|
handlerFile(ctx, fn)
|
|
}
|
|
|
|
func Enkey(ctx *gin.Context) {
|
|
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
|
buffer, err := m3u8.Enkey(c)
|
|
if err != nil {
|
|
return
|
|
}
|
|
contentLength = int64(buffer.Len())
|
|
contentType = "application/octet-stream"
|
|
reader = buffer
|
|
extraHeaders = map[string]string{}
|
|
return
|
|
}
|
|
handlerFile(ctx, fn)
|
|
}
|
|
|
|
// NewEnkey 返回短剧原片使用的新 M3U8 解密密钥(conf/newm3u8.key)。
|
|
func NewEnkey(ctx *gin.Context) {
|
|
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
|
buffer, err := m3u8.NewEnkey(c)
|
|
if err != nil {
|
|
return
|
|
}
|
|
contentLength = int64(buffer.Len())
|
|
contentType = "application/octet-stream"
|
|
reader = buffer
|
|
extraHeaders = map[string]string{}
|
|
return
|
|
}
|
|
handlerFile(ctx, fn)
|
|
}
|
|
|
|
// TranscodeM3u8 转码专用 m3u8 接口(不鉴权)。
|
|
func TranscodeM3u8(ctx *gin.Context) {
|
|
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
|
name := ctx.Param("key")
|
|
_, buff, err := m3u8.TransformForTranscode(name, webg.Conf.OrigDomain, webg.Conf.CdnDomain, webg.Conf.WebM3u8Host, webg.Conf.LsjMediaConf.M3u8Yuan, webg.Conf.LsjMediaConf.TsYuan)
|
|
if err != nil {
|
|
return
|
|
}
|
|
contentType = "application/vnd.apple.mpegurl"
|
|
reader = bytes.NewReader(buff.Bytes())
|
|
contentLength = int64(buff.Len())
|
|
extraHeaders = map[string]string{}
|
|
return
|
|
}
|
|
handlerFile(ctx, fn)
|
|
}
|
|
|
|
func LsjEnkey(ctx *gin.Context) {
|
|
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
|
buffer, err := m3u8.LsjEnkey(c)
|
|
if err != nil {
|
|
return
|
|
}
|
|
contentLength = int64(buffer.Len())
|
|
contentType = "application/octet-stream"
|
|
reader = buffer
|
|
extraHeaders = map[string]string{}
|
|
return
|
|
}
|
|
handlerFile(ctx, fn)
|
|
}
|
|
|
|
func handlerFile(ctx *gin.Context, h fileHandler) {
|
|
contentLength, contentType, reader, extraHeaders, err := h(ctx)
|
|
if err != nil {
|
|
common.ServerJSON(ctx, srvdef.DownloadM3u8File, err)
|
|
return
|
|
}
|
|
ctx.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
|
|
}
|