mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 13:36:09 +05:00
refactor
This commit is contained in:
51
server/ffprobe/ffprobe.go
Normal file
51
server/ffprobe/ffprobe.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package ffprobe
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gopkg.in/vansante/go-ffprobe.v2"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
var binFile = "ffprobe"
|
||||
|
||||
func init() {
|
||||
path, err := exec.LookPath("ffprobe")
|
||||
if err == nil {
|
||||
ffprobe.SetFFProbeBinPath(path)
|
||||
binFile = path
|
||||
} else {
|
||||
// working dir
|
||||
if _, err := os.Stat("ffprobe"); os.IsNotExist(err) {
|
||||
ffprobe.SetFFProbeBinPath(filepath.Dir(os.Args[0]) + "/ffprobe")
|
||||
binFile = filepath.Dir(os.Args[0]) + "/ffprobe"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Exists() bool {
|
||||
_, err := os.Stat(binFile)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func ProbeUrl(link string) (*ffprobe.ProbeData, error) {
|
||||
data, err := ffprobe.ProbeURL(getCtx(), link)
|
||||
return data, err
|
||||
}
|
||||
|
||||
func ProbeReader(reader io.Reader) (*ffprobe.ProbeData, error) {
|
||||
data, err := ffprobe.ProbeReader(getCtx(), reader)
|
||||
return data, err
|
||||
}
|
||||
|
||||
func getCtx() context.Context {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
time.Sleep(5 * time.Minute)
|
||||
cancel()
|
||||
}()
|
||||
return ctx
|
||||
}
|
||||
@@ -1,25 +1,16 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"server/ffprobe"
|
||||
|
||||
"server/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gopkg.in/vansante/go-ffprobe.v2"
|
||||
)
|
||||
|
||||
func commandExists(cmd string) bool {
|
||||
_, err := exec.LookPath(cmd)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func ffp(c *gin.Context) {
|
||||
hash := c.Param("hash")
|
||||
indexStr := c.Param("id")
|
||||
@@ -32,23 +23,7 @@ func ffp(c *gin.Context) {
|
||||
host := utils.GetScheme(c) + "://" + c.Request.Host + "/stream?link=" + hash + "&index=" + indexStr + "&play"
|
||||
// log.Println("ffprobe", host)
|
||||
|
||||
ctx, cancelFn := context.WithCancel(context.Background())
|
||||
defer cancelFn()
|
||||
|
||||
// path lookup
|
||||
path, err := exec.LookPath("ffprobe")
|
||||
if err == nil {
|
||||
// log.Println("ffprobe found in", path)
|
||||
ffprobe.SetFFProbeBinPath(path)
|
||||
} else {
|
||||
// log.Println("ffprobe not found in $PATH")
|
||||
// working dir
|
||||
if _, err := os.Stat("ffprobe"); os.IsNotExist(err) {
|
||||
ffprobe.SetFFProbeBinPath(filepath.Dir(os.Args[0]) + "/ffprobe")
|
||||
}
|
||||
}
|
||||
|
||||
data, err := ffprobe.ProbeURL(ctx, host)
|
||||
data, err := ffprobe.ProbeUrl(host)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("error getting data: %v", err))
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user