|
@ -18,12 +18,6 @@ const ( |
|
|
TORRENT_PIECE_LENGTH = 262144 |
|
|
TORRENT_PIECE_LENGTH = 262144 |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func check(e error) { |
|
|
|
|
|
if e != nil { |
|
|
|
|
|
panic(e) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type TorrentInfo struct { |
|
|
type TorrentInfo struct { |
|
|
PieceLength int `bencode:"piece length"` |
|
|
PieceLength int `bencode:"piece length"` |
|
|
Pieces []byte `bencode:"pieces"` |
|
|
Pieces []byte `bencode:"pieces"` |
|
@ -37,20 +31,23 @@ type Torrent struct { |
|
|
UrlList []string `bencode:"url-list"` |
|
|
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) |
|
|
chunk := make([]byte, TORRENT_PIECE_LENGTH) |
|
|
var pieces []byte |
|
|
var pieces []byte |
|
|
length := 0 |
|
|
length := 0 |
|
|
|
|
|
|
|
|
f, err := os.Open(filePath) |
|
|
f, err := os.Open(filePath) |
|
|
check(err) |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return []byte{}, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for { |
|
|
for { |
|
|
n, err := f.Read(chunk) |
|
|
n, err := f.Read(chunk) |
|
|
if err == io.EOF { |
|
|
if err == io.EOF { |
|
|
break |
|
|
break |
|
|
|
|
|
} else if err != nil { |
|
|
|
|
|
return []byte{}, err |
|
|
} |
|
|
} |
|
|
check(err) |
|
|
|
|
|
|
|
|
|
|
|
length += n |
|
|
length += n |
|
|
|
|
|
|
|
@ -73,9 +70,11 @@ func CreateTorrent(fileName string, filePath string) []byte { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
data, err := bencode.EncodeBytes(torrent) |
|
|
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) { |
|
|
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 |
|
|
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)) |
|
|
w.Header().Set(`Content-Disposition`, fmt.Sprintf(`attachment; filename="%s.torrent"`, fileName)) |
|
|
http.ServeContent(w, r, "", time.Now(), bytes.NewReader(encoded)) |
|
|
http.ServeContent(w, r, "", time.Now(), bytes.NewReader(encoded)) |
|
|