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

82 lignes
2,4 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"
"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 {
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" {
defer r.Body.Close()
2017-05-07 13:38:46 +02:00
// validation is done in ExtractInfo()
err := uploadForm.ExtractInfo(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
2017-05-06 10:36:37 +02:00
}
user, _, err := userService.RetrieveCurrentUser(r)
if err != nil {
fmt.Printf("error %+v\n", err)
}
status := 1 // normal
2017-05-10 22:03:14 +02:00
if uploadForm.Remake { // overrides trusted
status = 2
} else if user.Status == 1 {
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)
if (sameTorrents > 0) {
//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)
return
}
} 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)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
return
2017-05-06 10:36:37 +02:00
}
}