func Play(btService *bittorrent.BTService) gin.HandlerFunc { return func(ctx *gin.Context) { uri := ctx.Request.URL.Query().Get("uri") if uri == "" { return } fileIndex := -1 index := ctx.Request.URL.Query().Get("index") if index != "" { fIndex, err := strconv.Atoi(index) if err == nil { fileIndex = fIndex } } torrent := bittorrent.NewTorrent(uri) magnet := torrent.Magnet() boosters := url.Values{ "tr": providers.DefaultTrackers, } magnet += "&" + boosters.Encode() player := bittorrent.NewBTPlayer(btService, magnet, config.Get().KeepFilesAfterStop == false, fileIndex) if player.Buffer() != nil { return } hostname := "localhost" if localIP, err := util.LocalIP(); err == nil { hostname = localIP.String() } rUrl, _ := url.Parse(fmt.Sprintf("http://%s:%d/files/%s", hostname, config.ListenPort, player.PlayURL())) ctx.Redirect(302, rUrl.String()) } }
func Play(btService *bittorrent.BTService) gin.HandlerFunc { return func(ctx *gin.Context) { uri := ctx.Request.URL.Query().Get("uri") index := ctx.Request.URL.Query().Get("index") resume := ctx.Request.URL.Query().Get("resume") if uri == "" && resume == "" { return } fileIndex := -1 if index != "" { fIndex, err := strconv.Atoi(index) if err == nil { fileIndex = fIndex } } resumeIndex := -1 if resume != "" { rIndex, err := strconv.Atoi(resume) if err == nil && rIndex >= 0 { resumeIndex = rIndex } } magnet := "" infoHash := "" if uri != "" { torrent := bittorrent.NewTorrent(uri) magnet = torrent.Magnet() infoHash = torrent.InfoHash boosters := url.Values{ "tr": bittorrent.DefaultTrackers, } magnet += "&" + boosters.Encode() } player := bittorrent.NewBTPlayer(btService, magnet, fileIndex, resumeIndex, infoHash) if player.Buffer() != nil { return } hostname := "localhost" if localIP, err := util.LocalIP(); err == nil { hostname = localIP.String() } rUrl, _ := url.Parse(fmt.Sprintf("http://%s:%d/files/%s", hostname, config.ListenPort, player.PlayURL())) ctx.Redirect(302, rUrl.String()) } }