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

58 lignes
1,6 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"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"github.com/gorilla/mux"
2017-05-06 10:36:37 +02:00
"html/template"
"net/http"
2017-05-07 06:26:09 +02:00
"strconv"
"time"
2017-05-06 10:36:37 +02:00
)
var uploadTemplate = template.Must(template.New("upload").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/upload.html"))
2017-05-07 01:20:13 +02:00
func init() {
template.Must(uploadTemplate.ParseGlob("templates/_*.html")) // common
}
2017-05-06 10:36:37 +02:00
func UploadHandler(w http.ResponseWriter, r *http.Request) {
var err error
2017-05-06 10:36:37 +02:00
var uploadForm UploadForm
if r.Method == "POST" {
defer r.Body.Close()
err = uploadForm.ExtractInfo(r)
if err == nil {
//validate name + hash
//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,
Date: time.Now().Unix(),
Description: []byte{},
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))
if err == nil {
http.Redirect(w, r, url.String(), 302)
}
2017-05-06 10:36:37 +02:00
}
2017-05-07 06:26:09 +02:00
fmt.Printf("%+v\n", uploadForm)
} else if r.Method == "GET" {
2017-05-07 01:20:13 +02:00
htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
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)
}
}