diff --git a/cmd/torrent/metainfo.go b/cmd/torrent/metainfo.go index d40596b6..f8f558ff 100644 --- a/cmd/torrent/metainfo.go +++ b/cmd/torrent/metainfo.go @@ -77,6 +77,7 @@ func pprintMetainfo(metainfo *metainfo.MetaInfo, flags pprintMetainfoFlags) erro } 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 5aea9727..2a5ea01d 100644 --- a/metainfo/fileinfo.go +++ b/metainfo/fileinfo.go @@ -6,14 +6,14 @@ import "strings" 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 @@ func (me FileInfo) Offset(info *Info) (ret int64) { } 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 a5011946..1b794473 100644 --- a/metainfo/info.go +++ b/metainfo/info.go @@ -153,3 +153,10 @@ func (info *Info) UpvertedFiles() []FileInfo { 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 2bce0d7c..324f761f 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