forked from neoql/btlet
/
metadata.go
50 lines (39 loc) · 943 Bytes
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package bt
import (
"strings"
"github.com/alxcer/btlet/bencode"
)
// RawMeta is raw metadata
type RawMeta bencode.RawMessage
// Meta is meta
type Meta struct {
Name string `bencode:"name"`
Length *uint64 `bencode:"length,omitempty"`
PieceLength uint `bencode:"piece length"`
Pieces []byte `bencode:"pieces"`
Files []struct {
Path []string `bencode:"path"`
Length uint64 `bencode:"length"`
} `bencode:"files,omitempty"`
}
// MetaOutline is outline of metadata
type MetaOutline interface {
SetName(string)
AddFile(string, uint64)
}
// FillOutline can fill MetaOutline
func (rm RawMeta) FillOutline(mo MetaOutline) error {
var meta Meta
err := bencode.Unmarshal(rm, &meta)
if err != nil {
return err
}
mo.SetName(meta.Name)
if meta.Files == nil {
mo.AddFile(meta.Name, *meta.Length)
}
for _, f := range meta.Files {
mo.AddFile(strings.Join(f.Path, "/"), f.Length)
}
return nil
}