From 5a3a110e99e35ded8a2cefa0c6c16f505ebf99eb Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 28 Jan 2015 01:26:18 +1100 Subject: [PATCH] Add UpvertedFiles() to Info to make single-file torrents usable like multi-file torrents. --- metainfo/metainfo.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/metainfo/metainfo.go b/metainfo/metainfo.go index 8d205792..919ee6af 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -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 { -- 2.48.1