Retrive file via HTTP URL in `metainfo.go` (#778)

This commit is contained in:
pk 2022-09-25 19:01:51 -05:00 committed by GitHub
parent e81c6569af
commit de4c16bbe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 1 deletions

View File

@ -5,6 +5,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"strings"
"net/http"
"github.com/anacrolix/bargle" "github.com/anacrolix/bargle"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
@ -27,7 +29,18 @@ func metainfoCmd() (cmd bargle.Command) {
Value: &bargle.String{Target: &metainfoPath}, Value: &bargle.String{Target: &metainfoPath},
AfterParseFunc: func(ctx bargle.Context) error { AfterParseFunc: func(ctx bargle.Context) error {
ctx.AfterParse(func() (err error) { ctx.AfterParse(func() (err error) {
if strings.HasPrefix(metainfoPath, "http://") || strings.HasPrefix(metainfoPath, "https://") {
response, err := http.Get(metainfoPath)
if err != nil {
return nil
}
mi, err = metainfo.Load(response.Body)
if err != nil {
return nil
}
} else {
mi, err = metainfo.LoadFromFile(metainfoPath) mi, err = metainfo.LoadFromFile(metainfoPath)
}
return return
}) })
return nil return nil