use oopsHandler instead of panicking on error
This commit is contained in:
parent
b81477c1d3
commit
df09b005de
27
torrent.go
27
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))
|
||||
|
|
|
@ -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" {
|
||||
|
|
Loading…
Reference in New Issue