Merge pull request #131 from azhao12345/upload-impl
Implement upload for new torrents
Cette révision appartient à :
révision
33491e8e89
4 fichiers modifiés avec 58 ajouts et 16 suppressions
|
@ -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"`
|
||||
|
@ -30,7 +30,7 @@ type Torrents struct {
|
|||
Date int64 `gorm:"column:date"`
|
||||
Downloads int `gorm:"column:downloads"`
|
||||
Filesize int64 `gorm:"column:filesize"`
|
||||
Description []byte `gorm:"column:description"`
|
||||
Description string `gorm:"column:description"`
|
||||
Comments []byte `gorm:"column:comments"`
|
||||
}
|
||||
|
||||
|
|
|
@ -5,19 +5,23 @@ import (
|
|||
"errors"
|
||||
"github.com/ewhal/nyaa/util"
|
||||
"github.com/ewhal/nyaa/util/metainfo"
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"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 +50,11 @@ 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")
|
||||
|
||||
var p = bluemonday.UGCPolicy()
|
||||
|
||||
/**
|
||||
UploadForm.ExtractInfo takes an http request and computes all fields for this form
|
||||
*/
|
||||
|
@ -58,7 +67,7 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
|
|||
|
||||
// trim whitespaces
|
||||
f.Name = util.TrimWhitespaces(f.Name)
|
||||
f.Description = util.TrimWhitespaces(f.Description)
|
||||
f.Description = p.Sanitize(util.TrimWhitespaces(f.Description))
|
||||
f.Magnet = util.TrimWhitespaces(f.Magnet)
|
||||
|
||||
if len(f.Name) == 0 {
|
||||
|
@ -69,6 +78,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)
|
||||
|
|
|
@ -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: uploadForm.Description,
|
||||
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)
|
||||
|
|
|
@ -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>
|
||||
|
|
Référencer dans un nouveau ticket