2017-05-19 04:55:59 +02:00
package router
import (
2017-05-21 00:02:57 +02:00
"fmt"
2017-05-19 04:55:59 +02:00
"net/http"
"strconv"
"time"
2017-05-27 20:33:40 +02:00
elastic "gopkg.in/olivere/elastic.v5"
2017-05-21 20:20:40 +02:00
"github.com/NyaaPantsu/nyaa/config"
2017-05-19 04:55:59 +02:00
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/service/captcha"
2017-05-20 20:53:05 +02:00
"github.com/NyaaPantsu/nyaa/service/notifier"
2017-05-25 02:19:05 +02:00
"github.com/NyaaPantsu/nyaa/service/torrent"
2017-05-20 01:10:16 +02:00
"github.com/NyaaPantsu/nyaa/service/upload"
2017-05-20 20:53:05 +02:00
"github.com/NyaaPantsu/nyaa/service/user"
2017-05-19 04:55:59 +02:00
"github.com/NyaaPantsu/nyaa/service/user/permission"
2017-05-26 01:48:14 +02:00
"github.com/NyaaPantsu/nyaa/util/log"
2017-05-20 17:01:13 +02:00
msg "github.com/NyaaPantsu/nyaa/util/messages"
2017-05-27 20:33:40 +02:00
"github.com/NyaaPantsu/nyaa/util/publicSettings"
2017-05-19 04:55:59 +02:00
)
2017-05-25 21:54:58 +02:00
// UploadHandler : Main Controller for uploading a torrent
2017-05-19 04:55:59 +02:00
func UploadHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-25 21:54:58 +02:00
user := getUser ( r )
2017-05-20 01:10:16 +02:00
if ! uploadService . IsUploadEnabled ( * user ) {
http . Error ( w , "Error uploads are disabled" , http . StatusBadRequest )
return
2017-05-19 04:55:59 +02:00
}
if r . Method == "POST" {
UploadPostHandler ( w , r )
}
2017-05-20 17:01:13 +02:00
UploadGetHandler ( w , r )
2017-05-19 04:55:59 +02:00
}
2017-05-25 21:54:58 +02:00
// UploadPostHandler : Controller for uploading a torrent, after POST request, redirect or makes error in messages
2017-05-19 04:55:59 +02:00
func UploadPostHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-25 21:54:58 +02:00
var uploadForm uploadForm
2017-05-19 04:55:59 +02:00
defer r . Body . Close ( )
2017-05-25 21:54:58 +02:00
user := getUser ( r )
2017-05-20 17:01:13 +02:00
messages := msg . GetMessages ( r ) // new util for errors and infos
2017-05-19 04:55:59 +02:00
if userPermission . NeedsCaptcha ( user ) {
userCaptcha := captcha . Extract ( r )
if ! captcha . Authenticate ( userCaptcha ) {
2017-05-20 17:01:13 +02:00
messages . AddError ( "errors" , captcha . ErrInvalidCaptcha . Error ( ) )
2017-05-19 04:55:59 +02:00
}
}
// validation is done in ExtractInfo()
err := uploadForm . ExtractInfo ( r )
if err != nil {
2017-05-20 17:01:13 +02:00
messages . AddError ( "errors" , err . Error ( ) )
2017-05-19 04:55:59 +02:00
}
2017-05-20 01:10:16 +02:00
status := model . TorrentStatusNormal
2017-05-19 04:55:59 +02:00
if uploadForm . Remake { // overrides trusted
2017-05-20 01:10:16 +02:00
status = model . TorrentStatusRemake
} else if user . IsTrusted ( ) {
status = model . TorrentStatusTrusted
2017-05-19 04:55:59 +02:00
}
2017-05-25 02:19:05 +02:00
torrentIndb := model . Torrent { }
db . ORM . Unscoped ( ) . Model ( & model . Torrent { } ) . Where ( "torrent_hash = ?" , uploadForm . Infohash ) . First ( & torrentIndb )
if torrentIndb . ID > 0 {
2017-05-25 21:54:58 +02:00
if userPermission . CurrentUserIdentical ( user , torrentIndb . UploaderID ) && torrentIndb . IsDeleted ( ) && ! torrentIndb . IsBlocked ( ) { // if torrent is not locked and is deleted and the user is the actual owner
2017-05-25 02:19:05 +02:00
torrentService . DefinitelyDeleteTorrent ( strconv . Itoa ( int ( torrentIndb . ID ) ) )
} else {
messages . AddError ( "errors" , "Torrent already in database !" )
}
2017-05-20 17:01:13 +02:00
}
if ! messages . HasErrors ( ) {
2017-05-19 04:55:59 +02:00
// add to db and redirect
torrent := model . Torrent {
Name : uploadForm . Name ,
Category : uploadForm . CategoryID ,
SubCategory : uploadForm . SubCategoryID ,
Status : status ,
2017-05-27 20:33:40 +02:00
Hidden : uploadForm . Hidden ,
2017-05-19 04:55:59 +02:00
Hash : uploadForm . Infohash ,
Date : time . Now ( ) ,
Filesize : uploadForm . Filesize ,
Description : uploadForm . Description ,
2017-05-20 17:02:34 +02:00
WebsiteLink : uploadForm . WebsiteLink ,
2017-05-19 04:55:59 +02:00
UploaderID : user . ID }
2017-05-27 00:45:18 +02:00
torrent . ParseTrackers ( uploadForm . Trackers )
2017-05-20 16:26:22 +02:00
db . ORM . Create ( & torrent )
2017-05-19 04:55:59 +02:00
2017-05-26 01:48:14 +02:00
client , err := elastic . NewClient ( )
if err == nil {
err = torrent . AddToESIndex ( client )
if err == nil {
log . Infof ( "Successfully added torrent to ES index." )
} else {
log . Errorf ( "Unable to add torrent to ES index: %s" , err )
}
} else {
log . Errorf ( "Unable to create elasticsearch client: %s" , err )
}
2017-05-21 00:02:57 +02:00
url , err := Router . Get ( "view_torrent" ) . URL ( "id" , strconv . FormatUint ( uint64 ( torrent . ID ) , 10 ) )
2017-05-24 09:11:13 +02:00
if user . ID > 0 && config . DefaultUserSettings [ "new_torrent" ] { // If we are a member and notifications for new torrents are enabled
2017-05-27 00:45:18 +02:00
userService . GetFollowers ( user ) // We populate the liked field for users
if len ( user . Followers ) > 0 { // If we are followed by at least someone
for _ , follower := range user . Followers {
2017-05-21 18:13:28 +02:00
follower . ParseSettings ( ) // We need to call it before checking settings
2017-05-24 09:11:13 +02:00
if follower . Settings . Get ( "new_torrent" ) {
2017-05-27 19:08:47 +02:00
T , _ , _ := publicSettings . TfuncAndLanguageWithFallback ( follower . Language , follower . Language ) // We need to send the notification to every user in their language
2017-05-20 20:53:05 +02:00
2017-05-22 00:22:42 +02:00
notifierService . NotifyUser ( & follower , torrent . Identifier ( ) , fmt . Sprintf ( T ( "new_torrent_uploaded" ) , torrent . Name , user . Username ) , url . String ( ) , follower . Settings . Get ( "new_torrent_email" ) )
2017-05-21 18:13:28 +02:00
}
2017-05-20 20:53:05 +02:00
}
}
}
2017-05-19 04:55:59 +02:00
// add filelist to files db, if we have one
if len ( uploadForm . FileList ) > 0 {
for _ , uploadedFile := range uploadForm . FileList {
file := model . File { TorrentID : torrent . ID , Filesize : uploadedFile . Filesize }
err := file . SetPath ( uploadedFile . Path )
if err != nil {
2017-05-20 17:01:13 +02:00
messages . AddError ( "errors" , err . Error ( ) )
2017-05-19 04:55:59 +02:00
}
db . ORM . Create ( & file )
}
}
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2017-05-20 17:01:13 +02:00
http . Redirect ( w , r , url . String ( ) + "?success" , 302 )
2017-05-19 04:55:59 +02:00
}
}
2017-05-25 21:54:58 +02:00
// UploadGetHandler : Controller for uploading a torrent, after GET request or Failed Post request
2017-05-19 04:55:59 +02:00
func UploadGetHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-20 17:01:13 +02:00
messages := msg . GetMessages ( r ) // new util for errors and infos
2017-05-19 04:55:59 +02:00
2017-05-25 21:54:58 +02:00
var uploadForm uploadForm
2017-05-20 17:13:43 +02:00
_ = uploadForm . ExtractInfo ( r )
2017-05-25 21:54:58 +02:00
user := getUser ( r )
2017-05-19 04:55:59 +02:00
if userPermission . NeedsCaptcha ( user ) {
uploadForm . CaptchaID = captcha . GetID ( )
} else {
uploadForm . CaptchaID = ""
}
2017-05-25 21:54:58 +02:00
utv := formTemplateVariables {
commonTemplateVariables : newCommonVariables ( r ) ,
Form : uploadForm ,
FormErrors : messages . GetAllErrors ( ) ,
2017-05-19 04:55:59 +02:00
}
err := uploadTemplate . ExecuteTemplate ( w , "index.html" , utv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
}