mqtt加入认证,优化视频播放与浏览器

This commit is contained in:
2025-03-07 16:04:19 +08:00
parent c71e8bc13d
commit febcdfdbf7
13 changed files with 60 additions and 82 deletions

View File

@@ -5,8 +5,6 @@ import (
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/launcher/flags"
"github.com/go-rod/rod/lib/proto"
"go.uber.org/zap"
)
// OpenApp 用APP模式打开网页
@@ -26,18 +24,5 @@ func OpenApp(c context.Context, url string) {
b := rod.New().ControlURL(p).MustConnect()
defer b.MustClose()
s := make(chan struct{})
wait := b.EachEvent(func(e *proto.TargetTargetDestroyed) {
zap.S().Infoln("浏览器关闭事件")
s <- struct{}{}
})
go wait()
select {
case <-c.Done():
b.MustClose()
<-s
case <-s:
}
<-c.Done()
}

View File

@@ -2,16 +2,12 @@ package utils
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
)
// LinkVideo 链接视频,解析链接,网络文件会下载到临时目录并返回本地路径
func LinkVideo(link string) (local string, err error) {
func LinkVideo(link string) (path string, local bool, err error) {
if link == "" {
return
}
@@ -20,35 +16,14 @@ func LinkVideo(link string) (local string, err error) {
err = fmt.Errorf("URL 解析错误: %v", err)
} else {
if u.Scheme == "file" {
local, _ = strings.CutPrefix(link, "file://")
local = true
path, _ = 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 {
err = fmt.Errorf("链接文件获取失败: %v", err)
return
}
local = tmpLocal
local = false
path = link
} else {
err = fmt.Errorf("不支持的链接协议: %v", u.String())
}
}
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
}

View File

@@ -6,7 +6,7 @@ import (
libvlc "github.com/adrg/libvlc-go/v3"
)
func Play(ctx context.Context, file string) error {
func Play(ctx context.Context, path string, local bool) error {
// 1. 初始化 VLC
if err := libvlc.Init("--no-xlib"); err != nil {
return fmt.Errorf("VLC初始化失败: %w", err)
@@ -38,8 +38,14 @@ func Play(ctx context.Context, file string) error {
}
// 4. 加载并播放文件
if _, err := player.LoadMediaFromPath(file); err != nil {
return fmt.Errorf("文件加载失败: %w", err)
if local {
if _, err := player.LoadMediaFromPath(path); err != nil {
return fmt.Errorf("文件加载失败: %w", err)
}
} else {
if _, err := player.LoadMediaFromURL(path); err != nil {
return fmt.Errorf("文件加载失败: %w", err)
}
}
if err := player.Play(); err != nil {
@@ -57,7 +63,7 @@ func Play(ctx context.Context, file string) error {
}
// 5. 等待事件
fmt.Printf("正在播放: %s\n", file)
fmt.Printf("正在播放: %s\n", path)
select {
case <-ctx.Done():
return fmt.Errorf("播放被用户中断")