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

@@ -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
}