github.com/anacrolix/torrent@v1.61.0/metainfo/fileinfo.go (about) 1 package metainfo 2 3 import ( 4 "strings" 5 6 g "github.com/anacrolix/generics" 7 8 infohash_v2 "github.com/anacrolix/torrent/types/infohash-v2" 9 ) 10 11 // Information specific to a single file inside the MetaInfo structure. 12 type FileInfo struct { 13 // BEP3. With BEP 47 this can be optional, but we have no way to describe that without breaking 14 // the API. 15 Length int64 `bencode:"length"` 16 Path []string `bencode:"path"` // BEP3 17 // Unofficial extension by BiglyBT? https://github.com/BiglySoftware/BiglyBT/issues/1274. Might 18 // be a safer bet when available: https://github.com/anacrolix/torrent/pull/915. 19 PathUtf8 []string `bencode:"path.utf-8,omitempty" json:"path.utf-8,omitempty"` 20 21 ExtendedFileAttrs `json:",omitempty"` 22 23 // BEP 52. This isn't encoded in a v1 FileInfo, but is exposed here for APIs that expect to deal 24 // v1 files. 25 PiecesRoot g.Option[infohash_v2.T] `bencode:"-" json:"-"` 26 TorrentOffset int64 `bencode:"-" json:"-"` 27 } 28 29 func (fi *FileInfo) DisplayPath(info *Info) string { 30 if info.IsDir() { 31 return strings.Join(fi.BestPath(), "/") 32 } else { 33 return info.BestName() 34 } 35 } 36 37 func (fi *FileInfo) BestPath() []string { 38 if len(fi.PathUtf8) != 0 { 39 return fi.PathUtf8 40 } 41 return fi.Path 42 } 43 44 func (fi *FileInfo) BeginPieceIndex(pieceLength int64) int { 45 if pieceLength == 0 { 46 return 0 47 } 48 return int(fi.TorrentOffset / pieceLength) 49 } 50 51 func (fi *FileInfo) EndPieceIndex(pieceLength int64) int { 52 if pieceLength == 0 { 53 return 0 54 } 55 return int((fi.TorrentOffset + fi.Length + pieceLength - 1) / pieceLength) 56 }