2015-09-29 07:58:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2017-05-02 06:25:56 +02:00
|
|
|
"github.com/andreimarcu/linx-server/backends"
|
2019-01-25 08:33:11 +01:00
|
|
|
"github.com/andreimarcu/linx-server/expiry"
|
|
|
|
"github.com/andreimarcu/linx-server/torrent"
|
2015-09-29 07:58:14 +02:00
|
|
|
"github.com/zeebo/bencode"
|
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
2019-01-25 08:33:11 +01:00
|
|
|
func createTorrent(fileName string, f io.Reader, r *http.Request) ([]byte, error) {
|
|
|
|
url := getSiteURL(r) + Config.selifPath + fileName
|
|
|
|
chunk := make([]byte, torrent.TORRENT_PIECE_LENGTH)
|
2015-09-30 05:12:50 +02:00
|
|
|
|
2019-01-25 08:33:11 +01:00
|
|
|
t := torrent.Torrent{
|
2015-09-30 05:12:50 +02:00
|
|
|
Encoding: "UTF-8",
|
2019-01-25 08:33:11 +01:00
|
|
|
Info: torrent.TorrentInfo{
|
|
|
|
PieceLength: torrent.TORRENT_PIECE_LENGTH,
|
2015-09-30 05:12:50 +02:00
|
|
|
Name: fileName,
|
|
|
|
},
|
2019-01-25 08:33:11 +01:00
|
|
|
UrlList: []string{url},
|
2015-09-30 05:12:50 +02:00
|
|
|
}
|
2015-09-29 07:58:14 +02:00
|
|
|
|
|
|
|
for {
|
2019-01-25 08:33:11 +01:00
|
|
|
n, err := io.ReadFull(f, chunk)
|
2015-09-29 07:58:14 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
2019-01-25 08:33:11 +01:00
|
|
|
} else if err != nil && err != io.ErrUnexpectedEOF {
|
2015-09-29 17:41:42 +02:00
|
|
|
return []byte{}, err
|
2015-09-29 07:58:14 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 08:33:11 +01:00
|
|
|
t.Info.Length += n
|
|
|
|
t.Info.Pieces += string(torrent.HashPiece(chunk[:n]))
|
2015-09-29 07:58:14 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 08:33:11 +01:00
|
|
|
data, err := bencode.EncodeBytes(&t)
|
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-25 08:33:11 +01:00
|
|
|
metadata, f, err := storageBackend.Get(fileName)
|
|
|
|
if err == backends.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
|
2019-01-25 08:33:11 +01:00
|
|
|
} else if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespAUTO, err.Error())
|
|
|
|
return
|
2015-09-29 07:58:14 +02:00
|
|
|
}
|
2019-01-25 08:33:11 +01:00
|
|
|
defer f.Close()
|
2015-09-29 07:58:14 +02:00
|
|
|
|
2019-01-25 08:33:11 +01:00
|
|
|
if expiry.IsTsExpired(metadata.Expiry) {
|
|
|
|
storageBackend.Delete(fileName)
|
|
|
|
notFoundHandler(c, w, r)
|
2016-06-07 08:37:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|