révision
aad9e28938
5 fichiers modifiés avec 49 ajouts et 6 suppressions
6
config/upload.go
Fichier normal
6
config/upload.go
Fichier normal
|
@ -0,0 +1,6 @@
|
|||
package config
|
||||
|
||||
const (
|
||||
// TorrentFileStorage = "/var/tmp/torrent_outgoing"
|
||||
TorrentFileStorage = ""
|
||||
)
|
3
main.go
3
main.go
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import(
|
|||
"time"
|
||||
"net/http"
|
||||
"github.com/gorilla/feeds"
|
||||
"github.com/ewhal/nyaa/config"
|
||||
"github.com/ewhal/nyaa/util/search"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -18,7 +19,7 @@ func RssHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
feed := &feeds.Feed{
|
||||
Title: "Nyaa Pantsu",
|
||||
Link: &feeds.Link{Href: "https://nyaa.pantsu.cat/"},
|
||||
Link: &feeds.Link{Href: "https://" + config.WebAddress + "/"},
|
||||
Created: created_as_time,
|
||||
}
|
||||
feed.Items = []*feeds.Item{}
|
||||
|
@ -44,4 +45,4 @@ func RssHandler(w http.ResponseWriter, r *http.Request) {
|
|||
} else {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,12 +3,18 @@ package router
|
|||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"regexp"
|
||||
|
||||
"github.com/ewhal/nyaa/config"
|
||||
"github.com/ewhal/nyaa/service/captcha"
|
||||
"github.com/ewhal/nyaa/util"
|
||||
"github.com/ewhal/nyaa/util/metainfo"
|
||||
|
@ -20,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 (?)
|
||||
|
@ -55,8 +62,7 @@ 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://nyaa.pantsu.cat/faq#trackers")
|
||||
var ErrTrackerProblem = errors.New("torrent does not have any (working) trackers: https://" + config.WebAddress + "/faq#trackers")
|
||||
|
||||
// error indicating a torrent's name is invalid
|
||||
var ErrInvalidTorrentName = errors.New("torrent name is invalid")
|
||||
|
@ -108,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
|
||||
|
@ -155,6 +163,7 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
|
|||
}
|
||||
|
||||
f.Filesize = 0
|
||||
f.Filepath = ""
|
||||
}
|
||||
|
||||
|
||||
|
@ -167,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",
|
||||
|
|
|
@ -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))
|
||||
|
|
Référencer dans un nouveau ticket