2015-09-29 07:58:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2017-05-02 06:25:56 +02:00
|
|
|
"github.com/andreimarcu/linx-server/backends"
|
2015-09-29 07:58:14 +02:00
|
|
|
"github.com/zeebo/bencode"
|
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TORRENT_PIECE_LENGTH = 262144
|
|
|
|
)
|
|
|
|
|
|
|
|
type TorrentInfo struct {
|
|
|
|
PieceLength int `bencode:"piece length"`
|
2015-09-30 05:12:50 +02:00
|
|
|
Pieces string `bencode:"pieces"`
|
2015-09-29 07:58:14 +02:00
|
|
|
Name string `bencode:"name"`
|
|
|
|
Length int `bencode:"length"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Torrent struct {
|
|
|
|
Encoding string `bencode:"encoding"`
|
|
|
|
Info TorrentInfo `bencode:"info"`
|
|
|
|
UrlList []string `bencode:"url-list"`
|
|
|
|
}
|
|
|
|
|
2015-09-30 05:12:50 +02:00
|
|
|
func hashPiece(piece []byte) []byte {
|
|
|
|
h := sha1.New()
|
|
|
|
h.Write(piece)
|
|
|
|
return h.Sum(nil)
|
|
|
|
}
|
|
|
|
|
2016-06-07 08:37:42 +02:00
|
|
|
func createTorrent(fileName string, f io.ReadCloser, r *http.Request) ([]byte, error) {
|
2015-09-29 07:58:14 +02:00
|
|
|
chunk := make([]byte, TORRENT_PIECE_LENGTH)
|
2015-09-30 05:12:50 +02:00
|
|
|
|
|
|
|
torrent := Torrent{
|
|
|
|
Encoding: "UTF-8",
|
|
|
|
Info: TorrentInfo{
|
|
|
|
PieceLength: TORRENT_PIECE_LENGTH,
|
|
|
|
Name: fileName,
|
|
|
|
},
|
2019-01-14 23:51:02 +01:00
|
|
|
UrlList: []string{fmt.Sprintf("%s%s%s", getSiteURL(r), Config.selifPath, fileName)},
|
2015-09-30 05:12:50 +02:00
|
|
|
}
|
2015-09-29 07:58:14 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
n, err := f.Read(chunk)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
2015-09-29 17:41:42 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return []byte{}, err
|
2015-09-29 07:58:14 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 05:12:50 +02:00
|
|
|
torrent.Info.Length += n
|
|
|
|
torrent.Info.Pieces += string(hashPiece(chunk[:n]))
|
2015-09-29 07:58:14 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 05:12:50 +02:00
|
|
|
data, err := bencode.EncodeBytes(&torrent)
|
2015-09-29 17:41:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
2015-09-29 07:58:14 +02:00
|
|
|
|
2015-09-29 17:41:42 +02:00
|
|
|
return data, nil
|
2015-09-29 07:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func fileTorrentHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
fileName := c.URLParams["name"]
|
|
|
|
|
2019-01-09 05:28:01 +01:00
|
|
|
_, err := checkFile(fileName)
|
2015-10-08 04:45:34 +02:00
|
|
|
if err == NotFoundErr {
|
2015-09-29 07:58:14 +02:00
|
|
|
notFoundHandler(c, w, r)
|
|
|
|
return
|
2017-05-02 06:25:56 +02:00
|
|
|
} else if err == backends.BadMetadata {
|
2015-10-08 04:45:34 +02:00
|
|
|
oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
|
|
|
|
return
|
2015-09-29 07:58:14 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 08:37:42 +02:00
|
|
|
f, err := fileBackend.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespHTML, "Could not create torrent.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
encoded, err := createTorrent(fileName, f, r)
|
2015-09-29 17:41:42 +02:00
|
|
|
if err != nil {
|
2015-10-04 18:47:20 +02:00
|
|
|
oopsHandler(c, w, r, RespHTML, "Could not create torrent.")
|
2015-09-29 17:41:42 +02:00
|
|
|
return
|
|
|
|
}
|
2015-09-29 07:58:14 +02:00
|
|
|
|
|
|
|
w.Header().Set(`Content-Disposition`, fmt.Sprintf(`attachment; filename="%s.torrent"`, fileName))
|
|
|
|
http.ServeContent(w, r, "", time.Now(), bytes.NewReader(encoded))
|
|
|
|
}
|