]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add UpvertedFiles() to Info to make single-file torrents usable like
authorMatt Joiner <anacrolix@gmail.com>
Tue, 27 Jan 2015 14:26:18 +0000 (01:26 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 27 Jan 2015 14:26:18 +0000 (01:26 +1100)
multi-file torrents.

metainfo/metainfo.go

index 8d205792837dafb8676101a4a307920642ac100c..919ee6af2cc4094504fb7f96a8ea6d0d5dedf272 100644 (file)
@@ -46,6 +46,19 @@ type Info struct {
        Files       []FileInfo `bencode:"files,omitempty"`
 }
 
+// The files field, converted up from the old single-file in the parent info
+// dict if necessary. This is a helper to avoid having to conditionally handle
+// single and multi-file torrent infos.
+func (i *Info) UpvertedFiles() []FileInfo {
+       if len(i.Files) == 0 {
+               return []FileInfo{{
+                       Length: i.Length,
+                       Path:   []string{i.Name},
+               }}
+       }
+       return i.Files
+}
+
 // The info dictionary with its hash and raw bytes exposed, as these are
 // important to Bittorrent.
 type InfoEx struct {