metainfo: Add helper methods to FileInfo
This commit is contained in:
parent
984a69c73a
commit
51bae0a3c8
|
@ -0,0 +1,27 @@
|
|||
package metainfo
|
||||
|
||||
import "strings"
|
||||
|
||||
// Information specific to a single file inside the MetaInfo structure.
|
||||
type FileInfo struct {
|
||||
Length int64 `bencode:"length"`
|
||||
Path []string `bencode:"path"`
|
||||
}
|
||||
|
||||
func (fi *FileInfo) DisplayPath(info *Info) string {
|
||||
if info.IsDir() {
|
||||
return strings.Join(fi.Path, "/")
|
||||
} else {
|
||||
return info.Name
|
||||
}
|
||||
}
|
||||
|
||||
func (me FileInfo) Offset(info *Info) (ret int64) {
|
||||
for _, fi := range info.Files {
|
||||
if me.DisplayPath(info) == fi.DisplayPath(info) {
|
||||
return
|
||||
}
|
||||
ret += fi.Length
|
||||
}
|
||||
panic("not found")
|
||||
}
|
|
@ -20,12 +20,6 @@ type MetaInfo struct {
|
|||
URLList interface{} `bencode:"url-list,omitempty"`
|
||||
}
|
||||
|
||||
// Information specific to a single file inside the MetaInfo structure.
|
||||
type FileInfo struct {
|
||||
Length int64 `bencode:"length"`
|
||||
Path []string `bencode:"path"`
|
||||
}
|
||||
|
||||
// Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
|
||||
// failure.
|
||||
func Load(r io.Reader) (*MetaInfo, error) {
|
||||
|
|
Loading…
Reference in New Issue