mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-21 06:26:10 +05:00
refactor and update
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"server/torr"
|
||||
"server/web/api/utils"
|
||||
@@ -12,12 +13,17 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// http://127.0.0.1:8090/stream/fname?link=...&index=1&stat
|
||||
// get stat
|
||||
// http://127.0.0.1:8090/stream/fname?link=...&stat
|
||||
// get m3u
|
||||
// http://127.0.0.1:8090/stream/fname?link=...&index=1&m3u
|
||||
// http://127.0.0.1:8090/stream/fname?link=...&index=1&m3u&fromlast
|
||||
// stream torrent
|
||||
// http://127.0.0.1:8090/stream/fname?link=...&index=1&play
|
||||
// http://127.0.0.1:8090/stream/fname?link=...&save&title=...&poster=...
|
||||
// http://127.0.0.1:8090/stream/fname?link=...&index=1&play&save
|
||||
// http://127.0.0.1:8090/stream/fname?link=...&index=1&play&save&title=...&poster=...
|
||||
// only save
|
||||
// http://127.0.0.1:8090/stream/fname?link=...&save&title=...&poster=...
|
||||
|
||||
func stream(c *gin.Context) {
|
||||
link := c.Query("link")
|
||||
@@ -26,6 +32,7 @@ func stream(c *gin.Context) {
|
||||
_, stat := c.GetQuery("stat")
|
||||
_, save := c.GetQuery("save")
|
||||
_, m3u := c.GetQuery("m3u")
|
||||
_, fromlast := c.GetQuery("fromlast")
|
||||
_, play := c.GetQuery("play")
|
||||
title := c.Query("title")
|
||||
poster := c.Query("poster")
|
||||
@@ -37,10 +44,13 @@ func stream(c *gin.Context) {
|
||||
|
||||
if title == "" {
|
||||
title = c.Param("fname")
|
||||
title, _ = url.PathUnescape(title)
|
||||
title = strings.TrimLeft(title, "/")
|
||||
} else {
|
||||
title, _ = url.QueryUnescape(title)
|
||||
}
|
||||
|
||||
link, _ = url.QueryUnescape(link)
|
||||
title, _ = url.QueryUnescape(title)
|
||||
poster, _ = url.QueryUnescape(poster)
|
||||
|
||||
spec, err := utils.ParseLink(link)
|
||||
@@ -49,43 +59,16 @@ func stream(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var tor *torr.Torrent
|
||||
|
||||
// find torrent in bts
|
||||
for _, torrent := range bts.ListTorrents() {
|
||||
if torrent.Hash().HexString() == spec.InfoHash.HexString() {
|
||||
tor = torrent
|
||||
}
|
||||
tor, err := torr.AddTorrent(spec, title, poster)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
// find in db
|
||||
for _, torrent := range utils.ListTorrents() {
|
||||
if torrent.Hash().HexString() == spec.InfoHash.HexString() {
|
||||
tor = torrent
|
||||
}
|
||||
}
|
||||
// add torrent to bts
|
||||
if tor != nil {
|
||||
tor, err = torr.NewTorrent(tor.TorrentSpec, bts)
|
||||
} else {
|
||||
tor, err = torr.NewTorrent(spec, bts)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
tor.Title = title
|
||||
tor.Poster = poster
|
||||
|
||||
// save to db
|
||||
if save {
|
||||
utils.AddTorrent(tor)
|
||||
c.Status(200)
|
||||
}
|
||||
// wait torrent info
|
||||
if !tor.GotInfo() {
|
||||
c.AbortWithError(http.StatusInternalServerError, errors.New("timeout torrent get info"))
|
||||
return
|
||||
torr.SaveTorrentToDB(tor)
|
||||
c.Status(200) // only set status, not return
|
||||
}
|
||||
|
||||
// find file
|
||||
@@ -98,7 +81,7 @@ func stream(c *gin.Context) {
|
||||
index = ind
|
||||
}
|
||||
}
|
||||
if index == -1 {
|
||||
if index == -1 && play { // if file index not set and play file exec
|
||||
c.AbortWithError(http.StatusBadRequest, errors.New("\"index\" is empty or wrong"))
|
||||
return
|
||||
}
|
||||
@@ -107,14 +90,14 @@ func stream(c *gin.Context) {
|
||||
tor.Preload(index, 0)
|
||||
}
|
||||
// return stat if query
|
||||
if stat || (!m3u && !play) {
|
||||
c.JSON(200, tor.Stats())
|
||||
if stat {
|
||||
c.JSON(200, tor.Status())
|
||||
return
|
||||
} else
|
||||
// return m3u if query
|
||||
if m3u {
|
||||
//TODO m3u
|
||||
c.JSON(200, tor.Stats())
|
||||
m3ulist := "#EXTM3U\n" + getM3uList(tor.Status(), "http://"+c.Request.Host, fromlast)
|
||||
sendM3U(c, tor.Name(), tor.Hash().HexString(), m3ulist)
|
||||
return
|
||||
} else
|
||||
// return play if query
|
||||
@@ -123,96 +106,3 @@ func stream(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func torrentPlay(c echo.Context) error {
|
||||
link := c.QueryParam("link")
|
||||
if link == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "link should not be empty")
|
||||
}
|
||||
if settings.Get().EnableDebug {
|
||||
fmt.Println("Play:", c.QueryParams()) // mute log flood on play
|
||||
}
|
||||
qsave := c.QueryParam("save")
|
||||
qpreload := c.QueryParam("preload")
|
||||
qfile := c.QueryParam("file")
|
||||
qstat := c.QueryParam("stat")
|
||||
mm3u := c.QueryParam("m3u")
|
||||
|
||||
preload := int64(0)
|
||||
stat := strings.ToLower(qstat) == "true"
|
||||
|
||||
if qpreload != "" {
|
||||
preload, _ = strconv.ParseInt(qpreload, 10, 64)
|
||||
if preload > 0 {
|
||||
preload *= 1024 * 1024
|
||||
}
|
||||
}
|
||||
|
||||
magnet, infoBytes, err := helpers.GetMagnet(link)
|
||||
if err != nil {
|
||||
fmt.Println("Error get magnet:", link, err)
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
tor := bts.GetTorrent(magnet.InfoHash)
|
||||
if tor == nil {
|
||||
tor, err = bts.AddTorrent(*magnet, infoBytes, nil)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if stat {
|
||||
return c.JSON(http.StatusOK, getTorPlayState(tor))
|
||||
}
|
||||
|
||||
if !tor.WaitInfo() {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "torrent closed befor get info")
|
||||
}
|
||||
|
||||
if strings.ToLower(qsave) == "true" {
|
||||
if t, err := settings.LoadTorrentDB(magnet.InfoHash.HexString()); t == nil && err == nil {
|
||||
torrDb := toTorrentDB(tor)
|
||||
if torrDb != nil {
|
||||
torrDb.InfoBytes = infoBytes
|
||||
settings.SaveTorrentDB(torrDb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if strings.ToLower(mm3u) == "true" {
|
||||
mt := tor.Torrent.Metainfo()
|
||||
m3u := helpers.MakeM3UPlayList(tor.Stats(), mt.Magnet(tor.Name(), tor.Hash()).String(), c.Scheme()+"://"+c.Request().Host)
|
||||
c.Response().Header().Set("Content-Type", "audio/x-mpegurl")
|
||||
c.Response().Header().Set("Connection", "close")
|
||||
name := utils.CleanFName(tor.Name()) + ".m3u"
|
||||
c.Response().Header().Set("ETag", httptoo.EncodeQuotedString(fmt.Sprintf("%s/%s", tor.Hash().HexString(), name)))
|
||||
c.Response().Header().Set("Content-Disposition", `attachment; filename="`+name+`"`)
|
||||
http.ServeContent(c.Response(), c.Request(), name, time.Now(), bytes.NewReader([]byte(m3u)))
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
files := helpers.GetPlayableFiles(tor.Stats())
|
||||
|
||||
if len(files) == 1 {
|
||||
file := helpers.FindFile(files[0].Id, tor)
|
||||
if file == nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprint("File", files[0], "not found in torrent", tor.Name()))
|
||||
}
|
||||
|
||||
return bts.Play(tor, file, preload, c)
|
||||
}
|
||||
|
||||
if qfile == "" && len(files) > 1 {
|
||||
return c.JSON(http.StatusOK, getTorPlayState(tor))
|
||||
}
|
||||
|
||||
fileInd, _ := strconv.Atoi(qfile)
|
||||
file := helpers.FindFile(fileInd, tor)
|
||||
if file == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprint("File index ", fileInd, " not found in torrent ", tor.Name()))
|
||||
}
|
||||
return bts.Play(tor, file, preload, c)
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user