2017-05-06 10:36:37 +02:00
package router
import (
2017-05-07 06:26:09 +02:00
"fmt"
2017-05-06 10:36:37 +02:00
"net/http"
2017-05-07 06:26:09 +02:00
"strconv"
"time"
2017-05-06 19:01:15 +02:00
2017-05-08 00:16:20 +02:00
"github.com/ewhal/nyaa/config"
2017-05-07 11:08:44 +02:00
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
2017-05-07 10:25:09 +02:00
"github.com/ewhal/nyaa/service/captcha"
2017-05-08 23:49:06 +02:00
"github.com/ewhal/nyaa/service/user"
2017-05-08 22:43:33 +02:00
"github.com/ewhal/nyaa/util/languages"
2017-05-06 19:01:15 +02:00
"github.com/gorilla/mux"
2017-05-06 10:36:37 +02:00
)
func UploadHandler ( w http . ResponseWriter , r * http . Request ) {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if config . UploadsDisabled {
2017-05-08 00:16:20 +02:00
http . Error ( w , "Error uploads are disabled" , http . StatusInternalServerError )
return
}
2017-05-06 10:36:37 +02:00
var uploadForm UploadForm
if r . Method == "POST" {
2017-05-06 13:43:24 +02:00
defer r . Body . Close ( )
2017-05-07 13:38:46 +02:00
// validation is done in ExtractInfo()
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
err := uploadForm . ExtractInfo ( r )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
2017-05-06 10:36:37 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
user , _ , err := userService . RetrieveCurrentUser ( r )
if err != nil {
fmt . Printf ( "error %+v\n" , err )
}
2017-05-10 21:41:47 +02:00
status := 1 // normal
2017-05-10 22:03:14 +02:00
if uploadForm . Remake { // overrides trusted
status = 2
} else if user . Status == 1 {
2017-05-10 21:41:47 +02:00
status = 3 // mark as trusted if user is trusted
}
2017-05-11 04:07:39 +02:00
var sameTorrents int
db . ORM . Model ( & model . Torrent { } ) . Where ( "torrent_hash = ?" , uploadForm . Infohash ) . Count ( & sameTorrents )
2017-05-11 04:16:37 +02:00
if ( sameTorrents == 0 ) {
2017-05-11 04:07:39 +02:00
//add to db and redirect depending on result
torrent := model . Torrent {
Name : uploadForm . Name ,
Category : uploadForm . CategoryID ,
SubCategory : uploadForm . SubCategoryID ,
Status : status ,
Hash : uploadForm . Infohash ,
Date : time . Now ( ) ,
Filesize : uploadForm . Filesize ,
Description : uploadForm . Description ,
UploaderID : user . ID }
db . ORM . Create ( & torrent )
fmt . Printf ( "%+v\n" , torrent )
url , err := Router . Get ( "view_torrent" ) . URL ( "id" , strconv . FormatUint ( uint64 ( torrent . ID ) , 10 ) )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
http . Redirect ( w , r , url . String ( ) , 302 )
} else {
http . Error ( w , fmt . Errorf ( "Torrent already in database!" ) . Error ( ) , http . StatusInternalServerError )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
return
}
2017-05-06 13:43:24 +02:00
} else if r . Method == "GET" {
2017-05-07 14:55:46 +02:00
uploadForm . CaptchaID = captcha . GetID ( )
2017-05-08 01:46:30 +02:00
htv := UploadTemplateVariables { uploadForm , NewSearchForm ( ) , Navigation { } , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-08 22:43:33 +02:00
languages . SetTranslationFromRequest ( uploadTemplate , r , "en-us" )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
err := uploadTemplate . ExecuteTemplate ( w , "index.html" , htv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2017-05-06 13:43:24 +02:00
} else {
w . WriteHeader ( http . StatusMethodNotAllowed )
return
2017-05-06 10:36:37 +02:00
}
}