Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Merge branch 'bakape-less-realloc'

Cette révision appartient à :
Eliot Whalan 2017-05-09 13:17:00 +10:00
révision 20046d9ee8
8 fichiers modifiés avec 64 ajouts et 59 suppressions

Voir le fichier

@ -20,23 +20,23 @@ type Feed struct {
} }
type Torrents struct { type Torrents struct {
Id uint `gorm:"column:torrent_id;primary_key"` Id uint `gorm:"column:torrent_id;primary_key"`
Name string `gorm:"column:torrent_name"` Name string `gorm:"column:torrent_name"`
Hash string `gorm:"column:torrent_hash"` Hash string `gorm:"column:torrent_hash"`
Category int `gorm:"column:category"` Category int `gorm:"column:category"`
Sub_Category int `gorm:"column:sub_category"` Sub_Category int `gorm:"column:sub_category"`
Status int `gorm:"column:status"` Status int `gorm:"column:status"`
Date time.Time `gorm:"column:date"` Date time.Time `gorm:"column:date"`
UploaderId uint `gorm:"column:uploader"` UploaderId uint `gorm:"column:uploader"`
Downloads int `gorm:"column:downloads"` Downloads int `gorm:"column:downloads"`
Stardom int `gorm:"column:stardom"` Stardom int `gorm:"column:stardom"`
Filesize int64 `gorm:"column:filesize"` Filesize int64 `gorm:"column:filesize"`
Description string `gorm:"column:description"` Description string `gorm:"column:description"`
WebsiteLink string `gorm:"column:website_link"` WebsiteLink string `gorm:"column:website_link"`
Uploader *User `gorm:"ForeignKey:uploader"` Uploader *User `gorm:"ForeignKey:uploader"`
OldComments []OldComment `gorm:"ForeignKey:torrent_id"` OldComments []OldComment `gorm:"ForeignKey:torrent_id"`
Comments []Comment `gorm:"ForeignKey:torrent_id"` Comments []Comment `gorm:"ForeignKey:torrent_id"`
} }
/* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically */ /* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically */
@ -71,7 +71,7 @@ type TorrentsJson struct {
func (t *Torrents) ToJson() TorrentsJson { func (t *Torrents) ToJson() TorrentsJson {
magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...) magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...)
var commentsJson []CommentsJson commentsJson := make([]CommentsJson, 0, len(t.OldComments)+len(t.Comments))
for _, c := range t.OldComments { for _, c := range t.OldComments {
commentsJson = append(commentsJson, CommentsJson{Username: c.Username, Content: template.HTML(c.Content), Date: c.Date}) commentsJson = append(commentsJson, CommentsJson{Username: c.Username, Content: template.HTML(c.Content), Date: c.Date})
} }
@ -95,3 +95,12 @@ func (t *Torrents) ToJson() TorrentsJson {
} }
/* Complete the functions when necessary... */ /* Complete the functions when necessary... */
// Map Torrents to TorrentsToJSON without reallocations
func TorrentsToJSON(t []Torrents) []TorrentsJson {
json := make([]TorrentsJson, len(t))
for i := range t {
json[i] = t[i].ToJson()
}
return json
}

Voir le fichier

@ -30,14 +30,11 @@ func ApiHandler(w http.ResponseWriter, r *http.Request) {
pagenum = 1 pagenum = 1
} }
b := model.ApiResultJson{Torrents: []model.TorrentsJson{}}
torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1)) torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
for i, _ := range torrents { b := model.ApiResultJson{
res := torrents[i].ToJson() Torrents: model.TorrentsToJSON(torrents),
b.Torrents = append(b.Torrents, res)
} }
b.QueryRecordCount = maxPerPage b.QueryRecordCount = maxPerPage
b.TotalRecordCount = nbTorrents b.TotalRecordCount = nbTorrents
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")

Voir le fichier

@ -1,14 +1,15 @@
package router package router
import ( import (
"html"
"net/http"
"strconv"
"github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent" "github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util/languages" "github.com/ewhal/nyaa/util/languages"
"github.com/ewhal/nyaa/util/log" "github.com/ewhal/nyaa/util/log"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"html"
"net/http"
"strconv"
) )
func HomeHandler(w http.ResponseWriter, r *http.Request) { func HomeHandler(w http.ResponseWriter, r *http.Request) {
@ -27,13 +28,9 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
pagenum = 1 pagenum = 1
} }
b := []model.TorrentsJson{}
torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1)) torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
for i, _ := range torrents { b := model.TorrentsToJSON(torrents)
res := torrents[i].ToJson()
b = append(b, res)
}
navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"} navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}

Voir le fichier

@ -1,13 +1,14 @@
package router package router
import ( import (
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/util/search"
"github.com/ewhal/nyaa/util/languages"
"github.com/gorilla/mux"
"html" "html"
"net/http" "net/http"
"strconv" "strconv"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/util/languages"
"github.com/ewhal/nyaa/util/search"
"github.com/gorilla/mux"
) )
func SearchHandler(w http.ResponseWriter, r *http.Request) { func SearchHandler(w http.ResponseWriter, r *http.Request) {
@ -20,14 +21,9 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) {
pagenum = 1 pagenum = 1
} }
b := []model.TorrentsJson{}
search_param, torrents, nbTorrents := search.SearchByQuery(r, pagenum) search_param, torrents, nbTorrents := search.SearchByQuery(r, pagenum)
for i, _ := range torrents { b := model.TorrentsToJSON(torrents)
res := torrents[i].ToJson()
b = append(b, res)
}
navigationTorrents := Navigation{nbTorrents, search_param.Max, pagenum, "search_page"} navigationTorrents := Navigation{nbTorrents, search_param.Max, pagenum, "search_page"}
searchForm := SearchForm{ searchForm := SearchForm{

Voir le fichier

@ -2,12 +2,13 @@ package torrentService
import ( import (
"errors" "errors"
"strconv"
"strings"
"github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/util" "github.com/ewhal/nyaa/util"
"strconv"
"strings"
) )
type WhereParams struct { type WhereParams struct {
@ -23,7 +24,7 @@ type WhereParams struct {
// don't need raw SQL once we get MySQL // don't need raw SQL once we get MySQL
func GetFeeds() []model.Feed { func GetFeeds() []model.Feed {
var result []model.Feed result := make([]model.Feed, 0, 50)
rows, err := db.ORM.DB(). rows, err := db.ORM.DB().
Query( Query(
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " + "SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " +
@ -135,11 +136,12 @@ func GetAllTorrentsDB() ([]model.Torrents, int) {
} }
func CreateWhereParams(conditions string, params ...string) WhereParams { func CreateWhereParams(conditions string, params ...string) WhereParams {
whereParams := WhereParams{} whereParams := WhereParams{
whereParams.Conditions = conditions Conditions: conditions,
for i, _ := range params { Params: make([]interface{}, len(params)),
whereParams.Params = append(whereParams.Params, params[i]) }
for i := range params {
whereParams.Params[i] = params[i]
} }
return whereParams return whereParams
} }

Voir le fichier

@ -2,12 +2,11 @@ package userService
import ( import (
"errors" "errors"
"fmt"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
"golang.org/x/crypto/bcrypt"
"github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/model"
@ -16,7 +15,7 @@ import (
"github.com/ewhal/nyaa/util/log" "github.com/ewhal/nyaa/util/log"
"github.com/ewhal/nyaa/util/modelHelper" "github.com/ewhal/nyaa/util/modelHelper"
"github.com/ewhal/nyaa/util/timeHelper" "github.com/ewhal/nyaa/util/timeHelper"
"fmt" "golang.org/x/crypto/bcrypt"
) )
var userFields []string = []string{"name", "email", "createdAt", "updatedAt"} var userFields []string = []string{"name", "email", "createdAt", "updatedAt"}
@ -82,10 +81,10 @@ func CreateUser(w http.ResponseWriter, r *http.Request) (int, error) {
var registrationForm formStruct.RegistrationForm var registrationForm formStruct.RegistrationForm
var status int var status int
var err error var err error
modelHelper.BindValueForm(&registrationForm, r) modelHelper.BindValueForm(&registrationForm, r)
usernameCandidate := SuggestUsername(registrationForm.Username) usernameCandidate := SuggestUsername(registrationForm.Username)
if (usernameCandidate != registrationForm.Username) { if usernameCandidate != registrationForm.Username {
return http.StatusInternalServerError, fmt.Errorf("Username already taken, you can choose: %s", usernameCandidate) return http.StatusInternalServerError, fmt.Errorf("Username already taken, you can choose: %s", usernameCandidate)
} }
if CheckEmail(registrationForm.Email) { if CheckEmail(registrationForm.Email) {

Voir le fichier

@ -4,10 +4,11 @@ package metainfo
import ( import (
"crypto/sha1" "crypto/sha1"
"github.com/zeebo/bencode"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"github.com/zeebo/bencode"
) )
type FilePath []string type FilePath []string
@ -91,6 +92,7 @@ func (tf *TorrentFile) TotalSize() uint64 {
} }
func (tf *TorrentFile) GetAllAnnounceURLS() (l []string) { func (tf *TorrentFile) GetAllAnnounceURLS() (l []string) {
l = make([]string, 0, 64)
if len(tf.Announce) > 0 { if len(tf.Announce) > 0 {
l = append(l, tf.Announce) l = append(l, tf.Announce)
} }

Voir le fichier

@ -1,15 +1,16 @@
package search package search
import ( import (
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util/log"
"html" "html"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util/log"
) )
type SearchParam struct { type SearchParam struct {
@ -76,8 +77,10 @@ func SearchByQuery(r *http.Request, pagenum int) (SearchParam, []model.Torrents,
order_by := search_param.Sort + " " + search_param.Order order_by := search_param.Sort + " " + search_param.Order
parameters := torrentService.WhereParams{} parameters := torrentService.WhereParams{
conditions := []string{} Params: make([]interface{}, 0, 64),
}
conditions := make([]string, 0, 64)
if searchCatId != "" { if searchCatId != "" {
conditions = append(conditions, "category = ?") conditions = append(conditions, "category = ?")
parameters.Params = append(parameters.Params, searchCatId) parameters.Params = append(parameters.Params, searchCatId)