cmd/torrent/metainfo.go | 1 + metainfo/fileinfo.go | 13 ++++++++++--- metainfo/info.go | 7 +++++++ t.go | 14 ++------------ diff --git a/cmd/torrent/metainfo.go b/cmd/torrent/metainfo.go index d40596b641d32c1ebf79655e48457f4904d77b33..f8f558ff332aaf76971762b9a3b28ca46a774666 100644 --- a/cmd/torrent/metainfo.go +++ b/cmd/torrent/metainfo.go @@ -77,6 +77,7 @@ return nil } d := map[string]interface{}{ "Name": info.Name, + "Name.Utf8": info.NameUtf8, "NumPieces": info.NumPieces(), "PieceLength": info.PieceLength, "InfoHash": metainfo.HashInfoBytes().HexString(), diff --git a/metainfo/fileinfo.go b/metainfo/fileinfo.go index 5aea9727f5f9d0c2c8fc2193df37302909c0da00..2a5ea01d1f88d476d80a9622b4fb6d601243bbd9 100644 --- a/metainfo/fileinfo.go +++ b/metainfo/fileinfo.go @@ -6,14 +6,14 @@ // Information specific to a single file inside the MetaInfo structure. type FileInfo struct { Length int64 `bencode:"length"` // BEP3 Path []string `bencode:"path"` // BEP3 - PathUTF8 []string `bencode:"path.utf-8,omitempty"` + PathUtf8 []string `bencode:"path.utf-8,omitempty"` } func (fi *FileInfo) DisplayPath(info *Info) string { if info.IsDir() { - return strings.Join(fi.Path, "/") + return strings.Join(fi.BestPath(), "/") } else { - return info.Name + return info.BestName() } } @@ -26,3 +26,10 @@ ret += fi.Length } panic("not found") } + +func (fi FileInfo) BestPath() []string { + if len(fi.PathUtf8) != 0 { + return fi.PathUtf8 + } + return fi.Path +} diff --git a/metainfo/info.go b/metainfo/info.go index a5011946f335d7dae8dc83e6560b46015c4294f3..1b7944738081d823f1e4836715b898b9f2fa486d 100644 --- a/metainfo/info.go +++ b/metainfo/info.go @@ -153,3 +153,10 @@ func (info *Info) Piece(index int) Piece { return Piece{info, pieceIndex(index)} } + +func (info Info) BestName() string { + if info.NameUtf8 != "" { + return info.NameUtf8 + } + return info.Name +} diff --git a/t.go b/t.go index 2bce0d7c05f6cdb5cbdeb89e380645d6eaea965e..324f761f1965bfc97298d5990c69b925740141cb 100644 --- a/t.go +++ b/t.go @@ -211,23 +211,13 @@ func (t *Torrent) initFiles() { var offset int64 t.files = new([]*File) for _, fi := range t.info.UpvertedFiles() { - var path []string - if len(fi.PathUTF8) != 0 { - path = fi.PathUTF8 - } else { - path = fi.Path - } - dp := t.info.Name - if len(fi.Path) != 0 { - dp = strings.Join(fi.Path, "/") - } *t.files = append(*t.files, &File{ t, - strings.Join(append([]string{t.info.Name}, path...), "/"), + strings.Join(append([]string{t.info.BestName()}, fi.BestPath()...), "/"), offset, fi.Length, fi, - dp, + fi.DisplayPath(t.info), PiecePriorityNone, }) offset += fi.Length