]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Prefer UTF-8 fields when present
authorMatt Joiner <anacrolix@gmail.com>
Thu, 17 Mar 2022 04:07:10 +0000 (15:07 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 17 Mar 2022 04:07:10 +0000 (15:07 +1100)
cmd/torrent/metainfo.go
metainfo/fileinfo.go
metainfo/info.go
t.go

index d40596b641d32c1ebf79655e48457f4904d77b33..f8f558ff332aaf76971762b9a3b28ca46a774666 100644 (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(),
index 5aea9727f5f9d0c2c8fc2193df37302909c0da00..2a5ea01d1f88d476d80a9622b4fb6d601243bbd9 100644 (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
+}
index a5011946f335d7dae8dc83e6560b46015c4294f3..1b7944738081d823f1e4836715b898b9f2fa486d 100644 (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
+}
diff --git a/t.go b/t.go
index 2bce0d7c05f6cdb5cbdeb89e380645d6eaea965e..324f761f1965bfc97298d5990c69b925740141cb 100644 (file)
--- 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