]> Sergey Matveev's repositories - btrtrc.git/commitdiff
metainfo URL list can be a string or list
authorMatt Joiner <anacrolix@gmail.com>
Fri, 16 Jun 2017 07:07:30 +0000 (17:07 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 16 Jun 2017 07:07:30 +0000 (17:07 +1000)
metainfo/metainfo.go
metainfo/metainfo_test.go
metainfo/testdata/SKODAOCTAVIA336x280_archive.torrent [new file with mode: 0644]
metainfo/testdata/flat-url-list.torrent [new file with mode: 0644]
metainfo/urllist.go [new file with mode: 0644]

index 947bd7136b0e9d61e8ab9522853ff485fd92d633..55624e99c5db448f3ac2ed2e42df48b3f60e61bb 100644 (file)
@@ -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
index 037f8825f5a4421be15fa77d705b54c97721716b..2f1ec7450cd033f645b5a38e3752ec4df46bb54d 100644 (file)
@@ -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)
+}
diff --git a/metainfo/testdata/SKODAOCTAVIA336x280_archive.torrent b/metainfo/testdata/SKODAOCTAVIA336x280_archive.torrent
new file mode 100644 (file)
index 0000000..40ec11f
Binary files /dev/null and b/metainfo/testdata/SKODAOCTAVIA336x280_archive.torrent differ
diff --git a/metainfo/testdata/flat-url-list.torrent b/metainfo/testdata/flat-url-list.torrent
new file mode 100644 (file)
index 0000000..216ab38
Binary files /dev/null and b/metainfo/testdata/flat-url-list.torrent differ
diff --git a/metainfo/urllist.go b/metainfo/urllist.go
new file mode 100644 (file)
index 0000000..8171f19
--- /dev/null
@@ -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
+}