Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/router/uploadHandler.go

70 lignes
2 Kio
Go
Brut Vue normale Historique

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"
"github.com/ewhal/nyaa/config"
"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-09 19:23:21 +02:00
"github.com/ewhal/nyaa/util"
"github.com/ewhal/nyaa/util/languages"
"github.com/gorilla/mux"
2017-05-06 10:36:37 +02:00
)
func UploadHandler(w http.ResponseWriter, r *http.Request) {
if config.UploadsDisabled == 1 {
http.Error(w, "Error uploads are disabled", http.StatusInternalServerError)
return
}
var err error
2017-05-06 10:36:37 +02:00
var uploadForm UploadForm
if r.Method == "POST" {
defer r.Body.Close()
2017-05-07 13:38:46 +02:00
// validation is done in ExtractInfo()
err = uploadForm.ExtractInfo(r)
if err == nil {
2017-05-08 23:49:06 +02:00
user, _, err := userService.RetrieveCurrentUser(r)
if err != nil {
fmt.Printf("error %+v\n", err)
}
//add to db and redirect depending on result
2017-05-07 06:26:09 +02:00
torrent := model.Torrents{
Name: uploadForm.Name,
Category: uploadForm.CategoryId,
Sub_Category: uploadForm.SubCategoryId,
Status: 1,
Hash: uploadForm.Infohash,
2017-05-08 19:26:29 +02:00
Date: time.Now(),
2017-05-07 13:48:28 +02:00
Filesize: uploadForm.Filesize, // FIXME: should set to NULL instead of 0
2017-05-08 23:49:06 +02:00
Description: uploadForm.Description,
UploaderId: user.Id}
2017-05-09 19:23:21 +02:00
err = db.ORM.Create(&torrent).Error
if err != nil {
util.SendError(w, err, 500)
return
}
2017-05-07 06:26:09 +02:00
fmt.Printf("%+v\n", torrent)
2017-05-08 19:26:29 +02:00
url, err := Router.Get("view_torrent").URL("id", strconv.FormatUint(uint64(torrent.Id), 10))
2017-05-07 06:26:09 +02:00
if err == nil {
http.Redirect(w, r, url.String(), 302)
}
2017-05-06 10:36:37 +02:00
}
} else if r.Method == "GET" {
uploadForm.CaptchaID = captcha.GetID()
htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)}
languages.SetTranslationFromRequest(uploadTemplate, r, "en-us")
err = uploadTemplate.ExecuteTemplate(w, "index.html", htv)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
return
2017-05-06 10:36:37 +02:00
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}