2017-01-04 14:15:11 +08:00
|
|
|
package metainfo
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
// Information specific to a single file inside the MetaInfo structure.
|
|
|
|
type FileInfo struct {
|
2020-05-01 03:23:20 +08:00
|
|
|
Length int64 `bencode:"length"` // BEP3
|
|
|
|
Path []string `bencode:"path"` // BEP3
|
2022-03-17 12:07:10 +08:00
|
|
|
PathUtf8 []string `bencode:"path.utf-8,omitempty"`
|
2017-01-04 14:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fi *FileInfo) DisplayPath(info *Info) string {
|
|
|
|
if info.IsDir() {
|
2022-03-17 12:07:10 +08:00
|
|
|
return strings.Join(fi.BestPath(), "/")
|
2017-01-04 14:15:11 +08:00
|
|
|
} else {
|
2022-03-17 12:07:10 +08:00
|
|
|
return info.BestName()
|
2017-01-04 14:15:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me FileInfo) Offset(info *Info) (ret int64) {
|
2017-01-04 15:09:17 +08:00
|
|
|
for _, fi := range info.UpvertedFiles() {
|
2017-01-04 14:15:11 +08:00
|
|
|
if me.DisplayPath(info) == fi.DisplayPath(info) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret += fi.Length
|
|
|
|
}
|
|
|
|
panic("not found")
|
|
|
|
}
|
2022-03-17 12:07:10 +08:00
|
|
|
|
|
|
|
func (fi FileInfo) BestPath() []string {
|
|
|
|
if len(fi.PathUtf8) != 0 {
|
|
|
|
return fi.PathUtf8
|
|
|
|
}
|
|
|
|
return fi.Path
|
|
|
|
}
|