Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

start implementing upload

Cette révision appartient à :
Andrew Zhao 2017-05-06 21:26:09 -07:00
Parent 38ce6b8188
révision 637711fb9d
4 fichiers modifiés avec 53 ajouts et 14 suppressions

Voir le fichier

@ -21,7 +21,7 @@ type Feed struct {
}
type Torrents struct {
Id int `gorm:"column:torrent_id"`
Id int `gorm:"column:torrent_id;primary_key"`
Name string `gorm:"column:torrent_name"`
Category int `gorm:"column:category_id"`
Sub_Category int `gorm:"column:sub_category_id"`

Voir le fichier

@ -8,16 +8,19 @@ import (
"github.com/zeebo/bencode"
"net/http"
"net/url"
"strconv"
"strings"
)
// UploadForm serializing HTTP form for torrent upload
type UploadForm struct {
Name string
Magnet string
Infohash string
Category string
Description string
Name string
Magnet string
Infohash string
Category string
CategoryId int
SubCategoryId int
Description string
}
// TODO: these should be in another package (?)
@ -46,6 +49,9 @@ var ErrInvalidTorrentName = errors.New("torrent name is invalid")
// error indicating a torrent's description is invalid
var ErrInvalidTorrentDescription = errors.New("torrent description is invalid")
// error indicating a torrent's category is invalid
var ErrInvalidTorrentCategory = errors.New("torrent category is invalid")
/**
UploadForm.ExtractInfo takes an http request and computes all fields for this form
*/
@ -69,6 +75,24 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
return ErrInvalidTorrentDescription
}
catsSplit := strings.Split(f.Category, "_")
// need this to prevent out of index panics
if len(catsSplit) == 2 {
CatId, err := strconv.Atoi(catsSplit[0])
if err != nil {
return ErrInvalidTorrentCategory
}
SubCatId, err := strconv.Atoi(catsSplit[1])
if err != nil {
return ErrInvalidTorrentCategory
}
f.CategoryId = CatId
f.SubCategoryId = SubCatId
} else {
return ErrInvalidTorrentCategory
}
if len(f.Magnet) == 0 {
// try parsing torrent file if provided if no magnet is specified
tfile, _, err := r.FormFile(UploadFormTorrent)

Voir le fichier

@ -1,10 +1,14 @@
package router
import (
"fmt"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"github.com/gorilla/mux"
"html/template"
"net/http"
"github.com/gorilla/mux"
"strconv"
"time"
)
var uploadTemplate = template.Must(template.New("upload").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/upload.html"))
@ -22,7 +26,24 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
if err == nil {
//validate name + hash
//add to db and redirect depending on result
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)
}
}
fmt.Printf("%+v\n", uploadForm)
} else if r.Method == "GET" {
htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
err = uploadTemplate.ExecuteTemplate(w, "index.html", htv)

Voir le fichier

@ -22,27 +22,21 @@
<div class="form-group">
<label for="c">Category</label>
<select name="c" class="form-control input-sm">
<option value="3_" {{if eq .Category "3_"}}selected{{end}}>Anime</option>
<option value="3_12" {{if eq .Category "3_12"}}selected{{end}}>Anime - Anime Music Video</option>
<option value="3_5" {{if eq .Category "3_5"}}selected{{end}}>Anime - English-translated</option>
<option value="3_13" {{if eq .Category "3_13"}}selected{{end}}>Anime - Non-English-translated</option>
<option value="3_6" {{if eq .Category "3_6"}}selected{{end}}>Anime - Raw</option>
<option value="2_" {{if eq .Category "2_"}}selected{{end}}>Audio</option>
<option value="2_3" {{if eq .Category "2_3"}}selected{{end}}>Audio - Lossless</option>
<option value="2_4" {{if eq .Category "2_4"}}selected{{end}}>Audio - Lossy</option>
<option value="4_" {{if eq .Category "4_"}}selected{{end}}>Literature</option>
<option value="4_7" {{if eq .Category "4_7"}}selected{{end}}>Literature - English-translated</option>
<option value="4_8" {{if eq .Category "4_8"}}selected{{end}}>Literature - Raw</option>
<option value="4_14" {{if eq .Category "4_14"}}selected{{end}}>Literature - Non-English-translated</option>
<option value="5_" {{if eq .Category "5_"}}selected{{end}}>Live Action</option>
<option value="5_9" {{if eq .Category "5_9"}}selected{{end}}>Live Action - English-translated</option>
<option value="5_10" {{if eq .Category "5_10"}}selected{{end}}>Live Action - Idol/Promotional Video</option>
<option value="5_18" {{if eq .Category "5_18"}}selected{{end}}>Live Action - Non-English-translated</option>
<option value="5_11" {{if eq .Category "5_11"}}selected{{end}}>Live Action - Raw</option>
<option value="6_" {{if eq .Category "6_"}}selected{{end}}>Pictures</option>
<option value="6_15" {{if eq .Category "6_15"}}selected{{end}}>Pictures - Graphics</option>
<option value="6_16" {{if eq .Category "6_16"}}selected{{end}}>Pictures - Photos</option>
<option value="1_" {{if eq .Category "1_"}}selected{{end}}>Software</option>
<option value="1_1" {{if eq .Category "1_1"}}selected{{end}}>Software - Applications</option>
<option value="1_2" {{if eq .Category "1_2"}}selected{{end}}>Software - Games</option>
</select>