diff --git a/torrent.go b/torrent.go index 19094f2..33714cb 100644 --- a/torrent.go +++ b/torrent.go @@ -18,12 +18,6 @@ const ( TORRENT_PIECE_LENGTH = 262144 ) -func check(e error) { - if e != nil { - panic(e) - } -} - type TorrentInfo struct { PieceLength int `bencode:"piece length"` Pieces []byte `bencode:"pieces"` @@ -37,20 +31,23 @@ type Torrent struct { UrlList []string `bencode:"url-list"` } -func CreateTorrent(fileName string, filePath string) []byte { +func CreateTorrent(fileName string, filePath string) ([]byte, error) { chunk := make([]byte, TORRENT_PIECE_LENGTH) var pieces []byte length := 0 f, err := os.Open(filePath) - check(err) + if err != nil { + return []byte{}, err + } for { n, err := f.Read(chunk) if err == io.EOF { break + } else if err != nil { + return []byte{}, err } - check(err) length += n @@ -73,9 +70,11 @@ func CreateTorrent(fileName string, filePath string) []byte { } data, err := bencode.EncodeBytes(torrent) - check(err) + if err != nil { + return []byte{}, err + } - return data + return data, nil } func fileTorrentHandler(c web.C, w http.ResponseWriter, r *http.Request) { @@ -87,7 +86,11 @@ func fileTorrentHandler(c web.C, w http.ResponseWriter, r *http.Request) { return } - encoded := CreateTorrent(fileName, filePath) + encoded, err := CreateTorrent(fileName, filePath) + if err != nil { + oopsHandler(c, w, r) // 500 - creating torrent failed + return + } w.Header().Set(`Content-Disposition`, fmt.Sprintf(`attachment; filename="%s.torrent"`, fileName)) http.ServeContent(w, r, "", time.Now(), bytes.NewReader(encoded)) diff --git a/torrent_test.go b/torrent_test.go index 9b96e22..6d9afbb 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -9,9 +9,13 @@ import ( func TestCreateTorrent(t *testing.T) { fileName := "server.go" - encoded := CreateTorrent(fileName, fileName) var decoded Torrent + encoded, err := CreateTorrent(fileName, fileName) + if err != nil { + t.Fatal(err) + } + bencode.DecodeBytes(encoded, &decoded) if decoded.Encoding != "UTF-8" {