Prefer UTF-8 fields when present

This commit is contained in:
Matt Joiner 2022-03-17 15:07:10 +11:00
parent a1072bfee9
commit ff3b74ad88
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
4 changed files with 20 additions and 15 deletions

View File

@ -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(),

View File

@ -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
}

View File

@ -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
}

14
t.go
View File

@ -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