mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 13:36:09 +05:00
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package torr
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"server/settings"
|
|
"server/utils"
|
|
|
|
"log"
|
|
|
|
"github.com/anacrolix/missinggo/httptoo"
|
|
"github.com/anacrolix/torrent"
|
|
"github.com/labstack/echo"
|
|
)
|
|
|
|
func (bt *BTServer) View(torr *Torrent, file *torrent.File, c echo.Context) error {
|
|
go settings.SetViewed(torr.Hash().HexString(), file.Path())
|
|
reader := torr.NewReader(file, 0)
|
|
|
|
log.Println("Connect client")
|
|
c.Response().Header().Set("Connection", "close")
|
|
c.Response().Header().Set("ETag", httptoo.EncodeQuotedString(fmt.Sprintf("%s/%s", torr.Hash().HexString(), file.Path())))
|
|
|
|
http.ServeContent(c.Response(), c.Request(), file.Path(), time.Time{}, reader)
|
|
|
|
log.Println("Disconnect client")
|
|
torr.CloseReader(reader)
|
|
return c.NoContent(http.StatusOK)
|
|
}
|
|
|
|
func (bt *BTServer) Play(torr *Torrent, file *torrent.File, preload int64, c echo.Context) error {
|
|
if torr.status == TorrentAdded {
|
|
if !torr.GotInfo() {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "torrent closed befor get info")
|
|
}
|
|
}
|
|
if torr.status == TorrentGettingInfo {
|
|
if !torr.WaitInfo() {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "torrent closed befor get info")
|
|
}
|
|
}
|
|
|
|
if torr.PreloadedBytes == 0 {
|
|
torr.Preload(file, preload)
|
|
}
|
|
|
|
redirectUrl := c.Scheme() + "://" + c.Request().Host + "/torrent/view/" + torr.Hash().HexString() + "/" + utils.CleanFName(file.Path())
|
|
return c.Redirect(http.StatusFound, redirectUrl)
|
|
}
|