Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0

Allow storage of uploaded .torrent files (disabled by default)

Cette révision appartient à :
sfan5 2017-05-07 15:55:34 +02:00
Parent a201d0d058
révision 9b6b25e7df
4 fichiers modifiés avec 44 ajouts et 3 suppressions

6
config/upload.go Fichier normal
Voir le fichier

@ -0,0 +1,6 @@
package config
const (
// TorrentFileStorage = "/var/tmp/torrent_outgoing"
TorrentFileStorage = ""
)

Voir le fichier

@ -56,6 +56,9 @@ func main() {
db.ORM, _ = db.GormInit(conf)
initI18N()
go signals.Handle()
if len(config.TorrentFileStorage) > 0 {
os.MkdirAll(config.TorrentFileStorage, 0755)
}
RunServer(conf)
}
}

Voir le fichier

@ -3,8 +3,13 @@ package router
import (
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"regexp"
@ -21,14 +26,15 @@ import (
type UploadForm struct {
Name string
Magnet string
Infohash string
Category string
Description string
captcha.Captcha
Infohash string
CategoryId int
SubCategoryId int
Filesize int64
Filepath string
}
// TODO: these should be in another package (?)
@ -56,7 +62,6 @@ var ErrTorrentPlusMagnet = errors.New("upload either a torrent file or magnet li
var ErrPrivateTorrent = errors.New("torrent is private")
// error indicating a problem with its trackers
// FIXME: hardcoded link
var ErrTrackerProblem = errors.New("torrent does not have any (working) trackers: https://" + config.WebAddress + "/faq#trackers")
// error indicating a torrent's name is invalid
@ -109,7 +114,9 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
tfile, _, err := r.FormFile(UploadFormTorrent)
if err == nil {
var torrent metainfo.TorrentFile
// decode torrent
tfile.Seek(0, io.SeekStart)
err = bencode.NewDecoder(tfile).Decode(&torrent)
if err != nil {
return metainfo.ErrInvalidTorrentFile
@ -156,6 +163,7 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
}
f.Filesize = 0
f.Filepath = ""
}
@ -168,9 +176,30 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
// return ErrInvalidTorrentDescription
//}
// after data has been checked & extracted, write it to disk
if len(config.TorrentFileStorage) > 0 {
err := WriteTorrentToDisk(tfile, f.Infohash + ".torrent", &f.Filepath)
if err != nil {
return err
}
} else {
f.Filepath = ""
}
return nil
}
func WriteTorrentToDisk(file multipart.File, name string, fullpath *string) error {
file.Seek(0, io.SeekStart)
b, err := ioutil.ReadAll(file)
if err != nil {
return err
}
*fullpath = fmt.Sprintf("%s%c%s", config.TorrentFileStorage, os.PathSeparator, name)
return ioutil.WriteFile(*fullpath, b, 0644)
}
var dead_trackers = []string{ // substring matches!
"://open.nyaatorrents.info:6544",
"://tracker.openbittorrent.com:80",

Voir le fichier

@ -3,6 +3,7 @@ package router
import (
"fmt"
"net/http"
"os"
"strconv"
"time"
@ -23,6 +24,9 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
if !captcha.Authenticate(uploadForm.Captcha) {
// TODO: Prettier passing of mistyoed captcha errors
http.Error(w, captcha.ErrInvalidCaptcha.Error(), 403)
if len(uploadForm.Filepath) > 0 {
os.Remove(uploadForm.Filepath)
}
return
}
@ -37,7 +41,6 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
Filesize: uploadForm.Filesize, // FIXME: should set to NULL instead of 0
Description: uploadForm.Description,
Comments: []byte{}}
//fmt.Printf("%+v\n", torrent)
db.ORM.Create(&torrent)
fmt.Printf("%+v\n", torrent)
url, err := Router.Get("view_torrent").URL("id", strconv.Itoa(torrent.Id))