前三个点位所有功能已调通

This commit is contained in:
2024-11-08 15:37:36 +08:00
parent 239d542edc
commit 660ae1326f
13 changed files with 331 additions and 27 deletions

70
pkg/utils/link_audio.go Normal file
View File

@@ -0,0 +1,70 @@
package utils
import (
"bytes"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
)
type reader struct {
bytes.Reader
io.Closer
}
func (a *reader) Seek(offset int64, whence int) (int64, error) {
return a.Reader.Seek(offset, whence)
}
func toSeeker(src io.ReadCloser) io.ReadCloser {
buf := &bytes.Buffer{}
_, _ = io.Copy(buf, src)
return &reader{
Reader: *bytes.NewReader(buf.Bytes()),
Closer: src,
}
}
func open(u string) io.ReadCloser {
p, _ := strings.CutPrefix(u, "file://")
f, e := os.Open(p)
if e != nil {
log.Printf("音频文件 [%v] 打开错误: %v\n", u, e)
return nil
}
return f
}
func get(u string) io.ReadCloser {
resp, e := http.Get(u)
if e != nil {
log.Printf("音频文件 [%v] 下载失败: %v\n", u, e)
return nil
}
return resp.Body
}
// LinkAudio 链接音频,解析链接,直接提取为数据流
func LinkAudio(link string) (bgm io.ReadCloser) {
if link == "" {
return nil
}
u, err := url.Parse(link)
if err != nil {
log.Println("音频 URL 解析错误: ", err)
} else {
if u.Scheme == "file" {
bgm = open(link)
} else if u.Scheme == "http" || u.Scheme == "https" {
bgm = get(link)
} else {
log.Printf("不支持的音频文件协议: %v\n", u.String())
return
}
bgm = toSeeker(bgm)
}
return
}

55
pkg/utils/link_video.go Normal file
View File

@@ -0,0 +1,55 @@
package utils
import (
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
)
// LinkVideo 链接视频,解析链接,网络文件会下载到临时目录并返回本地路径
func LinkVideo(link string) (local string) {
if link == "" {
return
}
u, err := url.Parse(link)
if err != nil {
log.Println("音频 URL 解析错误: ", err)
} else {
if u.Scheme == "file" {
local, _ = strings.CutPrefix(link, "file://")
} else if u.Scheme == "http" || u.Scheme == "https" {
p, _ := url.PathUnescape(u.EscapedPath())
tmpLocal := path.Join(os.TempDir(), path.Base(p))
err = Download(link, tmpLocal)
if err != nil {
log.Println("音频文件下载失败: ", err)
return
}
local = tmpLocal
} else {
log.Printf("不支持的视频链接协议: %v\n", u.String())
return
}
}
return
}
// Download 下载文件
func Download(link string, local string) (err error) {
resp, err := http.Get(link)
if err != nil {
return
}
defer resp.Body.Close()
f, err := os.OpenFile(local, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
return
}