diff options
| author | jet2tlf <jet2tlf@gmail.com> | 2024-06-03 17:43:29 +0000 |
|---|---|---|
| committer | jet2tlf <jet2tlf@gmail.com> | 2024-06-03 17:43:29 +0000 |
| commit | 6091bea631659a2e5d152726f3581ca2d1bd235c (patch) | |
| tree | 92656f5f79641d3c187ce8e82c128da4de2a252c /cmd/mybittorrent/file.go | |
| parent | 7cff8936edbdc7ce7c0ca41eab2f8d5470cad2e6 (diff) | |
| download | bittorrent-go-6091bea631659a2e5d152726f3581ca2d1bd235c.tar.gz bittorrent-go-6091bea631659a2e5d152726f3581ca2d1bd235c.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'cmd/mybittorrent/file.go')
| -rw-r--r-- | cmd/mybittorrent/file.go | 74 |
1 files changed, 1 insertions, 73 deletions
diff --git a/cmd/mybittorrent/file.go b/cmd/mybittorrent/file.go index 2302976..498ea3f 100644 --- a/cmd/mybittorrent/file.go +++ b/cmd/mybittorrent/file.go @@ -1,12 +1,6 @@ package main -import ( - "bytes" - "fmt" - "io" -) - -type File struct { +type Meta struct { Announce string `bencode:"announce"` Info FileInfo `bencode:"info"` } @@ -17,69 +11,3 @@ type FileInfo struct { PieceLength int `bencode:"piece length"` Pieces string `bencode:"pieces"` } - -func NewFile() *File { - return &File{} -} - -func (f *File) ReadFrom(r io.ReadCloser) error { - buf := new(bytes.Buffer) - - _, err := buf.ReadFrom(r) - if err != nil { - return err - } - - defer func(r io.ReadCloser) { - err := r.Close() - if err != nil { - fmt.Printf("failed to close: %v+\n", err) - } - }(r) - - if err != nil { - return err - } - - decoded, err := NewDecoder(buf.String()).Decode() - if err != nil { - return err - } - - content, ok := decoded.(map[string]any) - if !ok { - return fmt.Errorf("invalid contents") - } - - f.Announce, ok = content["announce"].(string) - if !ok { - return fmt.Errorf("invalid announce field") - } - - info, ok := content["info"].(map[string]any) - if !ok { - return fmt.Errorf("invalid info field") - } - - f.Info.Length, ok = info["length"].(int) - if !ok { - return fmt.Errorf("invalid info.length field") - } - - f.Info.Name, ok = info["name"].(string) - if !ok { - return fmt.Errorf("invalid info.name field") - } - - f.Info.PieceLength, ok = info["piece length"].(int) - if !ok { - return fmt.Errorf("invalid info.piece length field") - } - - f.Info.Pieces, ok = info["pieces"].(string) - if !ok { - return fmt.Errorf("invalid info.pieces field") - } - - return nil -} |