metainfo URL list can be a string or list
This commit is contained in:
parent
582d749f69
commit
cc17c8a54c
|
@ -17,7 +17,7 @@ type MetaInfo struct {
|
|||
Comment string `bencode:"comment,omitempty"`
|
||||
CreatedBy string `bencode:"created by,omitempty"`
|
||||
Encoding string `bencode:"encoding,omitempty"`
|
||||
URLList []string `bencode:"url-list,omitempty"`
|
||||
UrlList UrlList `bencode:"url-list,omitempty"`
|
||||
}
|
||||
|
||||
// Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
|
||||
|
|
|
@ -116,3 +116,15 @@ func TestUnmarshal(t *testing.T) {
|
|||
testUnmarshal(t, `d4:infoabce`, nil)
|
||||
testUnmarshal(t, `d4:infodee`, &MetaInfo{InfoBytes: []byte("de")})
|
||||
}
|
||||
|
||||
func TestMetainfoWithListURLList(t *testing.T) {
|
||||
mi, err := LoadFromFile("testdata/SKODAOCTAVIA336x280_archive.torrent")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, mi.UrlList, 3)
|
||||
}
|
||||
|
||||
func TestMetainfoWithStringURLList(t *testing.T) {
|
||||
mi, err := LoadFromFile("testdata/flat-url-list.torrent")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, mi.UrlList, 1)
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,27 @@
|
|||
package metainfo
|
||||
|
||||
import (
|
||||
"github.com/anacrolix/torrent/bencode"
|
||||
)
|
||||
|
||||
type UrlList []string
|
||||
|
||||
var (
|
||||
_ bencode.Unmarshaler = (*UrlList)(nil)
|
||||
)
|
||||
|
||||
func (me *UrlList) UnmarshalBencode(b []byte) error {
|
||||
if len(b) == 0 {
|
||||
return nil
|
||||
}
|
||||
if b[0] == 'l' {
|
||||
var l []string
|
||||
err := bencode.Unmarshal(b, &l)
|
||||
*me = l
|
||||
return err
|
||||
}
|
||||
var s string
|
||||
err := bencode.Unmarshal(b, &s)
|
||||
*me = []string{s}
|
||||
return err
|
||||
}
|
Loading…
Reference in New Issue