Files
web_huangguo2_h5/pub/common/m3u8/enkey.go
T
谢宇宁andClaude Opus 5.5 0fbdba5ccf Initial commit
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 11:47:47 +07:00

45 lines
751 B
Go

package m3u8
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"sync"
)
var (
cacheEnkey []byte
initOnce sync.Once
initErr error
)
// 安全初始化 m3u8 key(文件缺失时不再 panic,交由调用方降级)
func initEnkey() {
initOnce.Do(func() {
data, err := ioutil.ReadFile("conf/m3u8.key")
if err != nil {
initErr = err
return
}
if len(data) == 0 {
initErr = fmt.Errorf("conf/m3u8.key is empty")
return
}
cacheEnkey = data
})
}
// Enkey 密匙 key
func Enkey(ctx context.Context) (buffer *bytes.Buffer, err error) {
initEnkey()
if initErr != nil {
return nil, initErr
}
if len(cacheEnkey) == 0 {
return nil, fmt.Errorf("m3u8 key not loaded")
}
buffer = bytes.NewBuffer(cacheEnkey)
return
}