diff --git a/model/torrent.go b/model/torrent.go index 21377669..eaa47292 100644 --- a/model/torrent.go +++ b/model/torrent.go @@ -20,23 +20,23 @@ type Feed struct { } type Torrents struct { - Id uint `gorm:"column:torrent_id;primary_key"` - Name string `gorm:"column:torrent_name"` - Hash string `gorm:"column:torrent_hash"` - Category int `gorm:"column:category"` - Sub_Category int `gorm:"column:sub_category"` - Status int `gorm:"column:status"` - Date time.Time `gorm:"column:date"` - UploaderId uint `gorm:"column:uploader"` - Downloads int `gorm:"column:downloads"` - Stardom int `gorm:"column:stardom"` - Filesize int64 `gorm:"column:filesize"` - Description string `gorm:"column:description"` - WebsiteLink string `gorm:"column:website_link"` + Id uint `gorm:"column:torrent_id;primary_key"` + Name string `gorm:"column:torrent_name"` + Hash string `gorm:"column:torrent_hash"` + Category int `gorm:"column:category"` + Sub_Category int `gorm:"column:sub_category"` + Status int `gorm:"column:status"` + Date time.Time `gorm:"column:date"` + UploaderId uint `gorm:"column:uploader"` + Downloads int `gorm:"column:downloads"` + Stardom int `gorm:"column:stardom"` + Filesize int64 `gorm:"column:filesize"` + Description string `gorm:"column:description"` + WebsiteLink string `gorm:"column:website_link"` - Uploader *User `gorm:"ForeignKey:uploader"` - OldComments []OldComment `gorm:"ForeignKey:torrent_id"` - Comments []Comment `gorm:"ForeignKey:torrent_id"` + Uploader *User `gorm:"ForeignKey:uploader"` + OldComments []OldComment `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 */ @@ -71,7 +71,7 @@ type TorrentsJson struct { func (t *Torrents) ToJson() TorrentsJson { 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 { 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... */ + +// 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 +} diff --git a/router/apiHandler.go b/router/apiHandler.go index d7139a01..7e95ad1a 100644 --- a/router/apiHandler.go +++ b/router/apiHandler.go @@ -30,14 +30,11 @@ func ApiHandler(w http.ResponseWriter, r *http.Request) { pagenum = 1 } - b := model.ApiResultJson{Torrents: []model.TorrentsJson{}} torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1)) - for i, _ := range torrents { - res := torrents[i].ToJson() - b.Torrents = append(b.Torrents, res) + b := model.ApiResultJson{ + Torrents: model.TorrentsToJSON(torrents), } - b.QueryRecordCount = maxPerPage b.TotalRecordCount = nbTorrents w.Header().Set("Content-Type", "application/json") diff --git a/router/homeHandler.go b/router/homeHandler.go index 0cdcb13a..a5c8f7bc 100644 --- a/router/homeHandler.go +++ b/router/homeHandler.go @@ -1,14 +1,15 @@ package router import ( + "html" + "net/http" + "strconv" + "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/service/torrent" "github.com/ewhal/nyaa/util/languages" "github.com/ewhal/nyaa/util/log" "github.com/gorilla/mux" - "html" - "net/http" - "strconv" ) func HomeHandler(w http.ResponseWriter, r *http.Request) { @@ -27,13 +28,9 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) { pagenum = 1 } - b := []model.TorrentsJson{} torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1)) - for i, _ := range torrents { - res := torrents[i].ToJson() - b = append(b, res) - } + b := model.TorrentsToJSON(torrents) navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"} diff --git a/router/searchHandler.go b/router/searchHandler.go index 957a8b76..9d4fdbe9 100644 --- a/router/searchHandler.go +++ b/router/searchHandler.go @@ -1,13 +1,14 @@ package router import ( - "github.com/ewhal/nyaa/model" - "github.com/ewhal/nyaa/util/search" - "github.com/ewhal/nyaa/util/languages" - "github.com/gorilla/mux" "html" "net/http" "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) { @@ -20,14 +21,9 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) { pagenum = 1 } - b := []model.TorrentsJson{} - search_param, torrents, nbTorrents := search.SearchByQuery(r, pagenum) - for i, _ := range torrents { - res := torrents[i].ToJson() - b = append(b, res) - } + b := model.TorrentsToJSON(torrents) navigationTorrents := Navigation{nbTorrents, search_param.Max, pagenum, "search_page"} searchForm := SearchForm{ diff --git a/service/torrent/torrent.go b/service/torrent/torrent.go index d880478c..fb32f9b3 100644 --- a/service/torrent/torrent.go +++ b/service/torrent/torrent.go @@ -2,12 +2,13 @@ package torrentService import ( "errors" + "strconv" + "strings" + "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/util" - "strconv" - "strings" ) type WhereParams struct { @@ -23,7 +24,7 @@ type WhereParams struct { // don't need raw SQL once we get MySQL func GetFeeds() []model.Feed { - var result []model.Feed + result := make([]model.Feed, 0, 50) rows, err := db.ORM.DB(). Query( "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 { - whereParams := WhereParams{} - whereParams.Conditions = conditions - for i, _ := range params { - whereParams.Params = append(whereParams.Params, params[i]) + whereParams := WhereParams{ + Conditions: conditions, + Params: make([]interface{}, len(params)), + } + for i := range params { + whereParams.Params[i] = params[i] } - return whereParams } diff --git a/service/user/user.go b/service/user/user.go index 072ec894..78f00564 100644 --- a/service/user/user.go +++ b/service/user/user.go @@ -2,12 +2,11 @@ package userService import ( "errors" + "fmt" "net/http" "strconv" "time" - "golang.org/x/crypto/bcrypt" - "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/model" @@ -16,7 +15,7 @@ import ( "github.com/ewhal/nyaa/util/log" "github.com/ewhal/nyaa/util/modelHelper" "github.com/ewhal/nyaa/util/timeHelper" - "fmt" + "golang.org/x/crypto/bcrypt" ) 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 status int var err error - + modelHelper.BindValueForm(®istrationForm, r) 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) } if CheckEmail(registrationForm.Email) { diff --git a/util/metainfo/metainfo.go b/util/metainfo/metainfo.go index c1ba8072..c3a0ffe7 100644 --- a/util/metainfo/metainfo.go +++ b/util/metainfo/metainfo.go @@ -4,10 +4,11 @@ package metainfo import ( "crypto/sha1" - "github.com/zeebo/bencode" "io" "os" "path/filepath" + + "github.com/zeebo/bencode" ) type FilePath []string @@ -91,6 +92,7 @@ func (tf *TorrentFile) TotalSize() uint64 { } func (tf *TorrentFile) GetAllAnnounceURLS() (l []string) { + l = make([]string, 0, 64) if len(tf.Announce) > 0 { l = append(l, tf.Announce) } diff --git a/util/search/search.go b/util/search/search.go index 53712371..1001a96d 100644 --- a/util/search/search.go +++ b/util/search/search.go @@ -1,15 +1,16 @@ package search import ( - "github.com/ewhal/nyaa/model" - "github.com/ewhal/nyaa/service/torrent" - "github.com/ewhal/nyaa/util/log" "html" "net/http" "strconv" "strings" "unicode" "unicode/utf8" + + "github.com/ewhal/nyaa/model" + "github.com/ewhal/nyaa/service/torrent" + "github.com/ewhal/nyaa/util/log" ) 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 - parameters := torrentService.WhereParams{} - conditions := []string{} + parameters := torrentService.WhereParams{ + Params: make([]interface{}, 0, 64), + } + conditions := make([]string, 0, 64) if searchCatId != "" { conditions = append(conditions, "category = ?") parameters.Params = append(parameters.Params, searchCatId)