torrent/file.go => torrent/metainfo.go | 57 ++++++++++++++--------------------------------------- torrent/file_test.go => torrent/metainfo_test.go | 13 ++++++------- diff --git a/torrent/file.go b/torrent/metainfo.go rename from torrent/file.go rename to torrent/metainfo.go index 13c73d17fc788de68df8a7bfd0e7519a67f4bc9c..51f481fcb789adcafb97800c338449a96ef42dae 100644 --- a/torrent/file.go +++ b/torrent/metainfo.go @@ -9,50 +9,28 @@ "os" "time" ) -//---------------------------------------------------------------------------- - -// SingleFile represents the specific data of the single file torrent -// metainfo. That includes length of the file and its recommended name. -type SingleFile struct { - Name string - Length int64 -} - -//---------------------------------------------------------------------------- - -// MultiFile represents the specific data of the multiple files torrent -// metainfo. That includes the name of the directory which will contain all the -// files and the files information. -type MultiFile struct { - Name string - Files []FileInfo -} - -// Information of a single file in multiple files torrent metainfo. +// Information specific to a single file inside the MetaInfo structure.. type FileInfo struct { Length int64 Path []string } -//---------------------------------------------------------------------------- - // MetaInfo is the type you should use when reading torrent files. See Load and -// LoadFromFile functions. All the fields are intended to be read-only. The -// "Info" field has SingleFile or MultiFile type, use the type switch or type -// assertion to determine the exact type. +// LoadFromFile functions. All the fields are intended to be read-only. If +// 'len(Files) == 1', then the FileInfo.Path is nil in that entry. type MetaInfo struct { - Info interface{} - InfoHash []byte - PieceLength int64 - Pieces []byte - Private bool - + Name string + Files []FileInfo + InfoHash []byte + PieceLength int64 + Pieces []byte + Private bool AnnounceList [][]string CreationDate time.Time Comment string CreatedBy string Encoding string - URLList []string + WebSeedURLs []string } // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of @@ -67,6 +45,7 @@ return nil, err } // post-parse processing + mi.Name = data.Info.Name if len(data.Info.Files) > 0 { files := make([]FileInfo, len(data.Info.Files)) for i, fi := range data.Info.Files { @@ -75,15 +54,9 @@ Length: fi.Length, Path: fi.Path, } } - mi.Info = MultiFile{ - Name: data.Info.Name, - Files: files, - } + mi.Files = files } else { - mi.Info = SingleFile{ - Name: data.Info.Name, - Length: data.Info.Length, - } + mi.Files = []FileInfo{{Length: data.Info.Length, Path: nil}} } mi.InfoHash = data.Info.Hash mi.PieceLength = data.Info.PieceLength @@ -102,7 +75,7 @@ mi.Encoding = data.Encoding if data.URLList != nil { switch v := data.URLList.(type) { case string: - mi.URLList = []string{v} + mi.WebSeedURLs = []string{v} case []interface{}: var ok bool ss := make([]string, len(v)) @@ -112,7 +85,7 @@ if !ok { return nil, errors.New("bad url-list data type") } } - mi.URLList = ss + mi.WebSeedURLs = ss default: return nil, errors.New("bad url-list data type") } diff --git a/torrent/file_test.go b/torrent/metainfo_test.go rename from torrent/file_test.go rename to torrent/metainfo_test.go index 6e895b81ee13e914841e969c2bda17183be34c1d..440a029f53874aa41f7874687918f2525ef1ce0f 100644 --- a/torrent/file_test.go +++ b/torrent/metainfo_test.go @@ -9,12 +9,11 @@ if err != nil { t.Fatal(err) } - switch info := mi.Info.(type) { - case SingleFile: - t.Logf("Single file: %s (length: %d)\n", info.Name, info.Length) - case MultiFile: - t.Logf("Multiple files: %s\n", info.Name) - for _, f := range info.Files { + if len(mi.Files) == 1 { + t.Logf("Single file: %s (length: %d)\n", mi.Name, mi.Files[0].Length) + } else { + t.Logf("Multiple files: %s\n", mi.Name) + for _, f := range mi.Files { t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length) } } @@ -24,7 +23,7 @@ for _, tracker := range group { t.Logf("Tracker: %s\n", tracker) } } - for _, url := range mi.URLList { + for _, url := range mi.WebSeedURLs { t.Logf("URL: %s\n", url) }