metainfo URL list can be a string or list

This commit is contained in:
Matt Joiner 2017-06-16 17:07:30 +10:00
parent 582d749f69
commit cc17c8a54c
5 changed files with 40 additions and 1 deletions

View File

@ -17,7 +17,7 @@ type MetaInfo struct {
Comment string `bencode:"comment,omitempty"` Comment string `bencode:"comment,omitempty"`
CreatedBy string `bencode:"created by,omitempty"` CreatedBy string `bencode:"created by,omitempty"`
Encoding string `bencode:"encoding,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 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of

View File

@ -116,3 +116,15 @@ func TestUnmarshal(t *testing.T) {
testUnmarshal(t, `d4:infoabce`, nil) testUnmarshal(t, `d4:infoabce`, nil)
testUnmarshal(t, `d4:infodee`, &MetaInfo{InfoBytes: []byte("de")}) 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.

BIN
metainfo/testdata/flat-url-list.torrent vendored Normal file

Binary file not shown.

27
metainfo/urllist.go Normal file
View File

@ -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
}