metainfo/fileinfo.go | 27 +++++++++++++++++++++++++++ metainfo/metainfo.go | 6 ------ diff --git a/metainfo/fileinfo.go b/metainfo/fileinfo.go new file mode 100644 index 0000000000000000000000000000000000000000..ff2d563169c4e2765e2a882a6371750bcec61b4e --- /dev/null +++ b/metainfo/fileinfo.go @@ -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") +} diff --git a/metainfo/metainfo.go b/metainfo/metainfo.go index 96a64cbc0b4499bfca26818c51602cfafb2a4170..ea09cce517f9cfc86956bddf933be56c4ed5a706 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -20,12 +20,6 @@ Encoding string `bencode:"encoding,omitempty"` 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) {