fileinfo.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package metainfo
  2. import (
  3. "strings"
  4. g "github.com/anacrolix/generics"
  5. )
  6. // Information specific to a single file inside the MetaInfo structure.
  7. type FileInfo struct {
  8. // BEP3. With BEP 47 this can be optional, but we have no way to describe that without breaking
  9. // the API.
  10. Length int64 `bencode:"length"`
  11. Path []string `bencode:"path"` // BEP3
  12. // Unofficial extension by BiglyBT? https://github.com/BiglySoftware/BiglyBT/issues/1274. Might
  13. // be a safer bet when available: https://github.com/anacrolix/torrent/pull/915.
  14. PathUtf8 []string `bencode:"path.utf-8,omitempty"`
  15. ExtendedFileAttrs
  16. // BEP 52. This isn't encoded in a v1 FileInfo, but is exposed here for APIs that expect to deal
  17. // v1 files.
  18. PiecesRoot g.Option[[32]byte] `bencode:"-"`
  19. TorrentOffset int64 `bencode:"-"`
  20. }
  21. func (fi *FileInfo) DisplayPath(info *Info) string {
  22. if info.IsDir() {
  23. return strings.Join(fi.BestPath(), "/")
  24. } else {
  25. return info.BestName()
  26. }
  27. }
  28. func (fi *FileInfo) BestPath() []string {
  29. if len(fi.PathUtf8) != 0 {
  30. return fi.PathUtf8
  31. }
  32. return fi.Path
  33. }