From 97b3a1d7ea5d1a59836edee414032437a800f676 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sun, 2 Jul 2017 16:54:55 +0200 Subject: [PATCH] Remove common package Common is no more a thing Use of TorrentParam instead of SearchParam now Common structs for search are exported in utils/search/structs Util has been renamed utils --- common/comment.go | 8 -- common/errors.go | 17 --- common/report.go | 15 --- common/scrape.go | 14 --- common/user.go | 11 -- controllers/activity_handler.go | 4 +- controllers/api_handler.go | 12 +- controllers/database_dump_handler.go | 4 +- controllers/helpers.go | 2 +- controllers/middlewares.go | 2 +- controllers/modpanel.go | 16 +-- controllers/publicSettingsHandler.go | 8 +- controllers/router.go | 2 +- controllers/rss_handler.go | 10 +- controllers/search_handler.go | 4 +- controllers/template.go | 8 +- controllers/template_functions.go | 12 +- controllers/template_test.go | 2 +- controllers/upload_handler.go | 14 +-- controllers/user_handler.go | 12 +- controllers/view_torrent_handler.go | 12 +- main.go | 10 +- models/dumps.go | 4 +- models/gorm.go | 2 +- models/torrent.go | 17 +-- models/torrents/delete.go | 2 +- models/torrents/helpers.go | 2 +- models/torrents/torrent.go | 16 +-- models/user.go | 2 +- models/users/create.go | 2 +- models/users/helpers.go | 2 +- models/users/request.go | 4 +- network.go | 2 +- {util => utils}/captcha/captcha.go | 0 {util => utils}/categories/categories.go | 0 {util => utils}/categories/categories_test.go | 0 {util => utils}/cookies/helpers.go | 0 {util => utils}/cookies/user.go | 0 {util => utils}/crypto/crypto.go | 0 {util => utils}/email/email.go | 2 +- {util => utils}/email/verification.go | 6 +- {util => utils}/feeds/convert.go | 2 +- {util => utils}/feeds/rss.go | 0 {util => utils}/filelist/filelist.go | 0 {util => utils}/format/format.go | 0 {util => utils}/format/format_test.go | 0 {util => utils}/format/magnet.go | 0 {util => utils}/log/error.go | 0 {util => utils}/log/logger.go | 0 {util => utils}/messages/messages.go | 2 +- {util => utils}/metainfo/doc.go | 0 {util => utils}/metainfo/errors.go | 0 {util => utils}/metainfo/metainfo.go | 0 .../publicSettings/publicSettings.go | 0 .../publicSettings/publicSettings_test.go | 0 {util => utils}/sanitize/markdown.go | 0 {util => utils}/sanitize/safe.go | 0 {util => utils}/sanitize/safe_test.go | 0 {util => utils}/search/search.go | 111 +++++------------- {service => utils/search/structs}/service.go | 2 +- .../search/structs/structs.go | 67 ++++++----- .../search/structs/structs_test.go | 6 +- {common => utils/search/structs}/torrent.go | 30 +---- {util => utils}/signals/interrupt.go | 2 +- {util => utils}/signals/unix.go | 0 {util => utils}/signals/win32.go | 0 {util => utils}/timeHelper/time_helper.go | 2 +- .../torrentLanguages/torrent_languages.go | 2 +- .../torrent_languages_test.go | 0 {util => utils}/upload/upload.go | 4 +- {util => utils}/validator/number.go | 2 +- {util => utils}/validator/slices.go | 2 +- {util => utils}/validator/string.go | 2 +- {util => utils}/validator/struct.go | 2 +- {util => utils}/validator/torrent/forms.go | 0 .../validator/torrent/functions.go | 8 +- {util => utils}/validator/torrent/helpers.go | 0 {util => utils}/validator/user/forms.go | 0 {util => utils}/validator/user/forms_test.go | 4 +- {util => utils}/validator/user/functions.go | 4 +- .../validator/user/functions_test.go | 0 {util => utils}/validator/validator.go | 4 +- {util => utils}/validator/validator_test.go | 2 +- 83 files changed, 193 insertions(+), 329 deletions(-) delete mode 100644 common/comment.go delete mode 100644 common/errors.go delete mode 100644 common/report.go delete mode 100644 common/scrape.go delete mode 100644 common/user.go rename {util => utils}/captcha/captcha.go (100%) rename {util => utils}/categories/categories.go (100%) rename {util => utils}/categories/categories_test.go (100%) rename {util => utils}/cookies/helpers.go (100%) rename {util => utils}/cookies/user.go (100%) rename {util => utils}/crypto/crypto.go (100%) rename {util => utils}/email/email.go (97%) rename {util => utils}/email/verification.go (94%) rename {util => utils}/feeds/convert.go (95%) rename {util => utils}/feeds/rss.go (100%) rename {util => utils}/filelist/filelist.go (100%) rename {util => utils}/format/format.go (100%) rename {util => utils}/format/format_test.go (100%) rename {util => utils}/format/magnet.go (100%) rename {util => utils}/log/error.go (100%) rename {util => utils}/log/logger.go (100%) rename {util => utils}/messages/messages.go (99%) rename {util => utils}/metainfo/doc.go (100%) rename {util => utils}/metainfo/errors.go (100%) rename {util => utils}/metainfo/metainfo.go (100%) rename {util => utils}/publicSettings/publicSettings.go (100%) rename {util => utils}/publicSettings/publicSettings_test.go (100%) rename {util => utils}/sanitize/markdown.go (100%) rename {util => utils}/sanitize/safe.go (100%) rename {util => utils}/sanitize/safe_test.go (100%) rename {util => utils}/search/search.go (60%) rename {service => utils/search/structs}/service.go (95%) rename common/search.go => utils/search/structs/structs.go (89%) rename common/search_test.go => utils/search/structs/structs_test.go (87%) rename {common => utils/search/structs}/torrent.go (89%) rename {util => utils}/signals/interrupt.go (93%) rename {util => utils}/signals/unix.go (100%) rename {util => utils}/signals/win32.go (100%) rename {util => utils}/timeHelper/time_helper.go (97%) rename {util => utils}/torrentLanguages/torrent_languages.go (95%) rename {util => utils}/torrentLanguages/torrent_languages_test.go (100%) rename {util => utils}/upload/upload.go (98%) rename {util => utils}/validator/number.go (95%) rename {util => utils}/validator/slices.go (95%) rename {util => utils}/validator/string.go (94%) rename {util => utils}/validator/struct.go (92%) rename {util => utils}/validator/torrent/forms.go (100%) rename {util => utils}/validator/torrent/functions.go (97%) rename {util => utils}/validator/torrent/helpers.go (100%) rename {util => utils}/validator/user/forms.go (100%) rename {util => utils}/validator/user/forms_test.go (95%) rename {util => utils}/validator/user/functions.go (95%) rename {util => utils}/validator/user/functions_test.go (100%) rename {util => utils}/validator/validator.go (98%) rename {util => utils}/validator/validator_test.go (98%) diff --git a/common/comment.go b/common/comment.go deleted file mode 100644 index 645185f5..00000000 --- a/common/comment.go +++ /dev/null @@ -1,8 +0,0 @@ -package common - -type CommentParam struct { - UserID uint32 - CommentID uint32 - Limit uint32 - Offset uint32 -} diff --git a/common/errors.go b/common/errors.go deleted file mode 100644 index 764b07c1..00000000 --- a/common/errors.go +++ /dev/null @@ -1,17 +0,0 @@ -package common - -import ( - "errors" -) - -var ErrInsufficientPermission = errors.New("you are not allowed to do that") -var ErrUserExists = errors.New("user already exists") -var ErrNoSuchUser = errors.New("no such user") -var ErrBadLogin = errors.New("bad login") -var ErrUserBanned = errors.New("banned") -var ErrBadEmail = errors.New("bad email") -var ErrNoSuchEntry = errors.New("no such entry") -var ErrNoSuchComment = errors.New("no such comment") -var ErrNotFollowing = errors.New("not following that user") -var ErrInvalidToken = errors.New("invalid token") -var ErrExpiredToken = errors.New("token is expired") diff --git a/common/report.go b/common/report.go deleted file mode 100644 index ddf0647b..00000000 --- a/common/report.go +++ /dev/null @@ -1,15 +0,0 @@ -package common - -import ( - "time" -) - -type ReportParam struct { - Limit uint32 - Offset uint32 - AllTime bool - ID uint32 - TorrentID uint32 - Before time.Time - After time.Time -} diff --git a/common/scrape.go b/common/scrape.go deleted file mode 100644 index 4ffbb868..00000000 --- a/common/scrape.go +++ /dev/null @@ -1,14 +0,0 @@ -package common - -import ( - "time" -) - -type ScrapeResult struct { - Hash string - TorrentID uint32 - Seeders uint32 - Leechers uint32 - Completed uint32 - Date time.Time -} diff --git a/common/user.go b/common/user.go deleted file mode 100644 index cb1aad40..00000000 --- a/common/user.go +++ /dev/null @@ -1,11 +0,0 @@ -package common - -type UserParam struct { - Full bool // if true populate Uploads, UsersWeLiked and UsersLikingMe - Email string - Name string - APIToken string - ID uint32 - Max uint32 - Offset uint32 -} diff --git a/controllers/activity_handler.go b/controllers/activity_handler.go index 47de4c2f..8ec7b191 100644 --- a/controllers/activity_handler.go +++ b/controllers/activity_handler.go @@ -8,8 +8,8 @@ import ( "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/models/activities" - "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/log" "github.com/gin-gonic/gin" ) diff --git a/controllers/api_handler.go b/controllers/api_handler.go index 21045b13..db30e333 100644 --- a/controllers/api_handler.go +++ b/controllers/api_handler.go @@ -12,12 +12,12 @@ import ( "github.com/NyaaPantsu/nyaa/models/torrents" "github.com/NyaaPantsu/nyaa/service" "github.com/NyaaPantsu/nyaa/service/api" - "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/crypto" - "github.com/NyaaPantsu/nyaa/util/log" - msg "github.com/NyaaPantsu/nyaa/util/messages" - "github.com/NyaaPantsu/nyaa/util/search" - "github.com/NyaaPantsu/nyaa/util/upload" + "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/crypto" + "github.com/NyaaPantsu/nyaa/utils/log" + msg "github.com/NyaaPantsu/nyaa/utils/messages" + "github.com/NyaaPantsu/nyaa/utils/search" + "github.com/NyaaPantsu/nyaa/utils/upload" "github.com/gin-gonic/gin" ) diff --git a/controllers/database_dump_handler.go b/controllers/database_dump_handler.go index f843ed97..248c230d 100644 --- a/controllers/database_dump_handler.go +++ b/controllers/database_dump_handler.go @@ -7,8 +7,8 @@ import ( "time" "github.com/NyaaPantsu/nyaa/models" - "github.com/NyaaPantsu/nyaa/util/log" - "github.com/NyaaPantsu/nyaa/util/metainfo" + "github.com/NyaaPantsu/nyaa/utils/log" + "github.com/NyaaPantsu/nyaa/utils/metainfo" "github.com/gin-gonic/gin" ) diff --git a/controllers/helpers.go b/controllers/helpers.go index e767edd9..a5190dfe 100644 --- a/controllers/helpers.go +++ b/controllers/helpers.go @@ -3,7 +3,7 @@ package controllers import ( "github.com/NyaaPantsu/nyaa/common" "github.com/NyaaPantsu/nyaa/models" - "github.com/NyaaPantsu/nyaa/util/cookies" + "github.com/NyaaPantsu/nyaa/utils/cookies" "github.com/gin-gonic/gin" ) diff --git a/controllers/middlewares.go b/controllers/middlewares.go index 042aa7e6..37a6c3cb 100644 --- a/controllers/middlewares.go +++ b/controllers/middlewares.go @@ -3,7 +3,7 @@ package controllers import ( "net/http" - "github.com/NyaaPantsu/nyaa/util/cookies" + "github.com/NyaaPantsu/nyaa/utils/cookies" "github.com/gin-gonic/gin" ) diff --git a/controllers/modpanel.go b/controllers/modpanel.go index 1e0b6567..f9b563b3 100644 --- a/controllers/modpanel.go +++ b/controllers/modpanel.go @@ -13,11 +13,11 @@ import ( "github.com/NyaaPantsu/nyaa/models/comments" "github.com/NyaaPantsu/nyaa/models/reports" "github.com/NyaaPantsu/nyaa/models/torrents" - "github.com/NyaaPantsu/nyaa/util/categories" - "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/log" - msg "github.com/NyaaPantsu/nyaa/util/messages" - "github.com/NyaaPantsu/nyaa/util/search" + "github.com/NyaaPantsu/nyaa/utils/categories" + "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/log" + msg "github.com/NyaaPantsu/nyaa/utils/messages" + "github.com/NyaaPantsu/nyaa/utils/search" "github.com/gin-gonic/gin" ) @@ -388,7 +388,7 @@ func TorrentsPostListPanel(c *gin.Context) { */ func APIMassMod(c *gin.Context) { torrentManyAction(c) - messages := msg.GetMessages(c) // new util for errors and infos + messages := msg.GetMessages(c) // new utils for errors and infos c.Header("Content-Type", "application/json") var mapOk map[string]interface{} @@ -404,7 +404,7 @@ func APIMassMod(c *gin.Context) { // DeletedTorrentsModPanel : Controller for viewing deleted torrents, accept common search arguments func DeletedTorrentsModPanel(c *gin.Context) { page := c.Param("page") - messages := msg.GetMessages(c) // new util for errors and infos + messages := msg.GetMessages(c) // new utils for errors and infos deleted := c.Request.URL.Query()["deleted"] unblocked := c.Request.URL.Query()["unblocked"] blocked := c.Request.URL.Query()["blocked"] @@ -487,7 +487,7 @@ func torrentManyAction(c *gin.Context) { owner, _ := strconv.Atoi(c.PostForm("owner")) category := c.PostForm("category") withReport, _ := strconv.ParseBool(c.DefaultPostForm("withreport", "false")) - messages := msg.GetMessages(c) // new util for errors and infos + messages := msg.GetMessages(c) // new utils for errors and infos catID, subCatID := -1, -1 var err error diff --git a/controllers/publicSettingsHandler.go b/controllers/publicSettingsHandler.go index e6fbfa61..ce120b85 100644 --- a/controllers/publicSettingsHandler.go +++ b/controllers/publicSettingsHandler.go @@ -5,10 +5,10 @@ import ( "net/url" "github.com/NyaaPantsu/nyaa/config" - "github.com/NyaaPantsu/nyaa/util/cookies" - msg "github.com/NyaaPantsu/nyaa/util/messages" - "github.com/NyaaPantsu/nyaa/util/publicSettings" - "github.com/NyaaPantsu/nyaa/util/timeHelper" + "github.com/NyaaPantsu/nyaa/utils/cookies" + msg "github.com/NyaaPantsu/nyaa/utils/messages" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/timeHelper" "github.com/gin-gonic/gin" ) diff --git a/controllers/router.go b/controllers/router.go index 9cbb0629..3ee1db58 100644 --- a/controllers/router.go +++ b/controllers/router.go @@ -3,7 +3,7 @@ package controllers import ( "net/http" - "github.com/NyaaPantsu/nyaa/util/captcha" + "github.com/NyaaPantsu/nyaa/utils/captcha" "github.com/gin-gonic/gin" "github.com/justinas/nosurf" ) diff --git a/controllers/rss_handler.go b/controllers/rss_handler.go index e1495da4..0103df0d 100644 --- a/controllers/rss_handler.go +++ b/controllers/rss_handler.go @@ -12,11 +12,11 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/models" - "github.com/NyaaPantsu/nyaa/util/categories" - userService "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/feeds" - "github.com/NyaaPantsu/nyaa/util/publicSettings" - "github.com/NyaaPantsu/nyaa/util/search" + "github.com/NyaaPantsu/nyaa/utils/categories" + userService "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/feeds" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/search" "github.com/gin-gonic/gin" "github.com/gorilla/feeds" ) diff --git a/controllers/search_handler.go b/controllers/search_handler.go index fd50a941..7fd01a47 100644 --- a/controllers/search_handler.go +++ b/controllers/search_handler.go @@ -6,8 +6,8 @@ import ( "strconv" "github.com/NyaaPantsu/nyaa/models" - "github.com/NyaaPantsu/nyaa/util/log" - "github.com/NyaaPantsu/nyaa/util/search" + "github.com/NyaaPantsu/nyaa/utils/log" + "github.com/NyaaPantsu/nyaa/utils/search" "github.com/gin-gonic/gin" ) diff --git a/controllers/template.go b/controllers/template.go index 7e217039..22b1ba60 100644 --- a/controllers/template.go +++ b/controllers/template.go @@ -6,10 +6,10 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/models" - userForms "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/filelist" - "github.com/NyaaPantsu/nyaa/util/messages" - "github.com/NyaaPantsu/nyaa/util/publicSettings" + userForms "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/filelist" + "github.com/NyaaPantsu/nyaa/utils/messages" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" "github.com/gin-gonic/gin" "github.com/justinas/nosurf" diff --git a/controllers/template_functions.go b/controllers/template_functions.go index 37fc3d6b..83aa20d8 100644 --- a/controllers/template_functions.go +++ b/controllers/template_functions.go @@ -13,12 +13,12 @@ import ( "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/models/activities" "github.com/NyaaPantsu/nyaa/models/torrents" - "github.com/NyaaPantsu/nyaa/util" - "github.com/NyaaPantsu/nyaa/util/categories" - "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/filelist" - "github.com/NyaaPantsu/nyaa/util/publicSettings" - "github.com/NyaaPantsu/nyaa/util/torrentLanguages" + "github.com/NyaaPantsu/nyaa/utils" + "github.com/NyaaPantsu/nyaa/utils/categories" + "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/filelist" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/torrentLanguages" ) type captchaData struct { diff --git a/controllers/template_test.go b/controllers/template_test.go index 65c45707..399117ee 100644 --- a/controllers/template_test.go +++ b/controllers/template_test.go @@ -16,7 +16,7 @@ import ( "github.com/NyaaPantsu/nyaa/model" "github.com/NyaaPantsu/nyaa/service/api" userForm "github.com/NyaaPantsu/nyaa/service/user/form" - "github.com/NyaaPantsu/nyaa/util/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" "github.com/gin-gonic/gin" ) diff --git a/controllers/upload_handler.go b/controllers/upload_handler.go index 8156f5a5..d97c6dc0 100644 --- a/controllers/upload_handler.go +++ b/controllers/upload_handler.go @@ -9,12 +9,12 @@ import ( "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/models/torrents" "github.com/NyaaPantsu/nyaa/service/api" - "github.com/NyaaPantsu/nyaa/util/captcha" - "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/log" - msg "github.com/NyaaPantsu/nyaa/util/messages" - "github.com/NyaaPantsu/nyaa/util/publicSettings" - "github.com/NyaaPantsu/nyaa/util/upload" + "github.com/NyaaPantsu/nyaa/utils/captcha" + "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/log" + msg "github.com/NyaaPantsu/nyaa/utils/messages" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/upload" "github.com/gin-gonic/gin" ) @@ -38,7 +38,7 @@ func UploadHandler(c *gin.Context) { func UploadPostHandler(c *gin.Context) { var uploadForm apiService.TorrentRequest user := getUser(c) - messages := msg.GetMessages(c) // new util for errors and infos + messages := msg.GetMessages(c) // new utils for errors and infos if userPermission.NeedsCaptcha(user) { userCaptcha := captcha.Extract(c) diff --git a/controllers/user_handler.go b/controllers/user_handler.go index 85e3a572..a0a1fab0 100644 --- a/controllers/user_handler.go +++ b/controllers/user_handler.go @@ -8,12 +8,12 @@ import ( "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/models/notifications" - "github.com/NyaaPantsu/nyaa/util/captcha" - "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/crypto" - msg "github.com/NyaaPantsu/nyaa/util/messages" - "github.com/NyaaPantsu/nyaa/util/publicSettings" - "github.com/NyaaPantsu/nyaa/util/search" + "github.com/NyaaPantsu/nyaa/utils/captcha" + "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/crypto" + msg "github.com/NyaaPantsu/nyaa/utils/messages" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/search" "github.com/gin-gonic/gin" ) diff --git a/controllers/view_torrent_handler.go b/controllers/view_torrent_handler.go index 70b3c28c..d6af6408 100644 --- a/controllers/view_torrent_handler.go +++ b/controllers/view_torrent_handler.go @@ -18,12 +18,12 @@ import ( "github.com/NyaaPantsu/nyaa/models/reports" "github.com/NyaaPantsu/nyaa/models/torrents" "github.com/NyaaPantsu/nyaa/service/api" - "github.com/NyaaPantsu/nyaa/util" - "github.com/NyaaPantsu/nyaa/util/captcha" - "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/filelist" - msg "github.com/NyaaPantsu/nyaa/util/messages" - "github.com/NyaaPantsu/nyaa/util/publicSettings" + "github.com/NyaaPantsu/nyaa/utils" + "github.com/NyaaPantsu/nyaa/utils/captcha" + "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/filelist" + msg "github.com/NyaaPantsu/nyaa/utils/messages" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" "github.com/gin-gonic/gin" ) diff --git a/main.go b/main.go index 16260551..78d6043c 100644 --- a/main.go +++ b/main.go @@ -11,11 +11,11 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/controllers" - "github.com/NyaaPantsu/nyaa/util/cookies" - "github.com/NyaaPantsu/nyaa/util/log" - "github.com/NyaaPantsu/nyaa/util/publicSettings" - "github.com/NyaaPantsu/nyaa/util/search" - "github.com/NyaaPantsu/nyaa/util/signals" + "github.com/NyaaPantsu/nyaa/utils/cookies" + "github.com/NyaaPantsu/nyaa/utils/log" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/search" + "github.com/NyaaPantsu/nyaa/utils/signals" ) var buildversion string diff --git a/models/dumps.go b/models/dumps.go index edbf3f5a..c2eeac26 100644 --- a/models/dumps.go +++ b/models/dumps.go @@ -4,7 +4,7 @@ import ( "html/template" "time" - "github.com/NyaaPantsu/nyaa/util" + "github.com/NyaaPantsu/nyaa/utils/format" ) // DatabaseDump model @@ -28,7 +28,7 @@ type DatabaseDumpJSON struct { func (dump *DatabaseDump) ToJSON() DatabaseDumpJSON { json := DatabaseDumpJSON{ Date: dump.Date.Format(time.RFC3339), - Filesize: util.FormatFilesize(dump.Filesize), + Filesize: format.FileSize(dump.Filesize), Name: dump.Name, TorrentLink: template.URL(dump.TorrentLink), } diff --git a/models/gorm.go b/models/gorm.go index 5b44813f..5f1a7b2d 100644 --- a/models/gorm.go +++ b/models/gorm.go @@ -2,7 +2,7 @@ package models import ( "github.com/NyaaPantsu/nyaa/config" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" "github.com/azhao12345/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" // Need for postgres support _ "github.com/jinzhu/gorm/dialects/sqlite" // Need for sqlite diff --git a/models/torrent.go b/models/torrent.go index e6135e3a..94f1151d 100644 --- a/models/torrent.go +++ b/models/torrent.go @@ -17,8 +17,9 @@ import ( "net/url" "github.com/NyaaPantsu/nyaa/config" - "github.com/NyaaPantsu/nyaa/util" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/format" + "github.com/NyaaPantsu/nyaa/utils/log" + "github.com/NyaaPantsu/nyaa/utils/sanitize" "github.com/bradfitz/slice" ) @@ -256,14 +257,14 @@ func (t *Torrent) ToJSON() TorrentJSON { } else { trackers = t.GetTrackersArray() } - magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, trackers...) + magnet := format.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, trackers...) commentsJSON := make([]CommentJSON, 0, len(t.OldComments)+len(t.Comments)) for _, c := range t.OldComments { commentsJSON = append(commentsJSON, CommentJSON{Username: c.Username, UserID: -1, Content: template.HTML(c.Content), Date: c.Date.UTC()}) } for _, c := range t.Comments { if c.User != nil { - commentsJSON = append(commentsJSON, CommentJSON{Username: c.User.Username, UserID: int(c.User.ID), Content: util.MarkdownToHTML(c.Content), Date: c.CreatedAt.UTC(), UserAvatar: c.User.MD5}) + commentsJSON = append(commentsJSON, CommentJSON{Username: c.User.Username, UserID: int(c.User.ID), Content: sanitize.MarkdownToHTML(c.Content), Date: c.CreatedAt.UTC(), UserAvatar: c.User.MD5}) } else { commentsJSON = append(commentsJSON, CommentJSON{}) } @@ -317,17 +318,17 @@ func (t *Torrent) ToJSON() TorrentJSON { Hash: t.Hash, Date: t.Date.Format(time.RFC3339), Filesize: t.Filesize, - Description: util.MarkdownToHTML(t.Description), + Description: sanitize.MarkdownToHTML(t.Description), Comments: commentsJSON, SubCategory: strconv.Itoa(t.SubCategory), Category: strconv.Itoa(t.Category), Downloads: t.Downloads, UploaderID: uploaderID, - UploaderName: util.SafeText(uploader), - WebsiteLink: util.Safe(t.WebsiteLink), + UploaderName: sanitize.SafeText(uploader), + WebsiteLink: sanitize.Safe(t.WebsiteLink), Language: t.Language, Magnet: template.URL(magnet), - TorrentLink: util.Safe(torrentlink), + TorrentLink: sanitize.Safe(torrentlink), Leechers: scrape.Leechers, Seeders: scrape.Seeders, Completed: scrape.Completed, diff --git a/models/torrents/delete.go b/models/torrents/delete.go index 6cefef4d..e0c88472 100644 --- a/models/torrents/delete.go +++ b/models/torrents/delete.go @@ -2,7 +2,7 @@ package torrents import ( "errors" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" "net/http" "github.com/NyaaPantsu/nyaa/models" diff --git a/models/torrents/helpers.go b/models/torrents/helpers.go index bae28b9c..7b711ef5 100644 --- a/models/torrents/helpers.go +++ b/models/torrents/helpers.go @@ -9,7 +9,7 @@ import ( "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/models/activities" "github.com/NyaaPantsu/nyaa/models/notifications" - "github.com/NyaaPantsu/nyaa/util/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" ) // ExistOrDelete : Check if a torrent exist with the same hash and if it can be replaced, it is replaced diff --git a/models/torrents/torrent.go b/models/torrents/torrent.go index d438670a..13950680 100644 --- a/models/torrents/torrent.go +++ b/models/torrents/torrent.go @@ -8,7 +8,7 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/models" - "github.com/NyaaPantsu/nyaa/service" + "github.com/NyaaPantsu/nyaa/utils/search/structs" ) /* Function to interact with Models @@ -79,24 +79,24 @@ func FindRawByHash(hash string) (torrent models.Torrent, err error) { } // FindOrderByNoCount : Get torrents based on search without counting and user -func FindOrderByNoCount(parameters *serviceBase.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, err error) { +func FindOrderByNoCount(parameters *structs.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, err error) { torrents, _, err = findOrderBy(parameters, orderBy, limit, offset, false, false, false) return } // FindOrderBy : Get torrents based on search without user -func FindOrderBy(parameters *serviceBase.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, count int, err error) { +func FindOrderBy(parameters *structs.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, count int, err error) { torrents, count, err = findOrderBy(parameters, orderBy, limit, offset, true, false, false) return } // FindWithUserOrderBy : Get torrents based on search with user -func FindWithUserOrderBy(parameters *serviceBase.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, count int, err error) { +func FindWithUserOrderBy(parameters *structs.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, count int, err error) { torrents, count, err = findOrderBy(parameters, orderBy, limit, offset, true, true, false) return } -func findOrderBy(parameters *serviceBase.WhereParams, orderBy string, limit int, offset int, countAll bool, withUser bool, deleted bool) ( +func findOrderBy(parameters *structs.WhereParams, orderBy string, limit int, offset int, countAll bool, withUser bool, deleted bool) ( torrents []models.Torrent, count int, err error, ) { var conditionArray []string @@ -150,12 +150,12 @@ func findOrderBy(parameters *serviceBase.WhereParams, orderBy string, limit int, // database. The list will be of length 'limit' and in default order. // GetTorrents returns the first records found. Later records may be retrieved // by providing a positive 'offset' -func Find(parameters serviceBase.WhereParams, limit int, offset int) ([]models.Torrent, int, error) { +func Find(parameters structs.WhereParams, limit int, offset int) ([]models.Torrent, int, error) { return FindOrderBy(¶meters, "", limit, offset) } // FindDB : Get Torrents with where parameters but no limit and order by default (get all the torrents corresponding in the db) -func FindDB(parameters serviceBase.WhereParams) ([]models.Torrent, int, error) { +func FindDB(parameters structs.WhereParams) ([]models.Torrent, int, error) { return FindOrderBy(¶meters, "", 0, 0) } @@ -192,7 +192,7 @@ func ToggleBlock(id uint) (models.Torrent, int, error) { } // FindDeleted : Gets deleted torrents based on search params -func FindDeleted(parameters *serviceBase.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, count int, err error) { +func FindDeleted(parameters *structs.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, count int, err error) { torrents, count, err = findOrderBy(parameters, orderBy, limit, offset, true, true, true) return } diff --git a/models/user.go b/models/user.go index 890a9172..35b99f5d 100644 --- a/models/user.go +++ b/models/user.go @@ -3,7 +3,7 @@ package models import ( "encoding/json" "fmt" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" "time" "github.com/NyaaPantsu/nyaa/config" diff --git a/models/users/create.go b/models/users/create.go index 126aac67..258c509f 100644 --- a/models/users/create.go +++ b/models/users/create.go @@ -7,7 +7,7 @@ import ( "time" "github.com/NyaaPantsu/nyaa/models" - "github.com/NyaaPantsu/nyaa/util/crypto" + "github.com/NyaaPantsu/nyaa/utils/crypto" "github.com/dorajistyle/goyangi/util/modelHelper" "github.com/gin-gonic/gin" "golang.org/x/crypto/bcrypt" diff --git a/models/users/helpers.go b/models/users/helpers.go index 7e758347..f34e84cd 100644 --- a/models/users/helpers.go +++ b/models/users/helpers.go @@ -8,7 +8,7 @@ import ( "strconv" "github.com/NyaaPantsu/nyaa/models" - "github.com/NyaaPantsu/nyaa/util/validator/user" + "github.com/NyaaPantsu/nyaa/utils/validator/user" "golang.org/x/crypto/bcrypt" ) diff --git a/models/users/request.go b/models/users/request.go index a4525502..552708d4 100644 --- a/models/users/request.go +++ b/models/users/request.go @@ -4,10 +4,10 @@ import ( "errors" "net/http" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" "github.com/NyaaPantsu/nyaa/models" - "github.com/NyaaPantsu/nyaa/util/validator" + "github.com/NyaaPantsu/nyaa/utils/validator" "github.com/gin-gonic/gin" "github.com/gorilla/context" "golang.org/x/crypto/bcrypt" diff --git a/network.go b/network.go index e3fa8bc9..0fe26b6a 100644 --- a/network.go +++ b/network.go @@ -5,7 +5,7 @@ import ( "net" "github.com/NyaaPantsu/nyaa/config" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" "github.com/majestrate/i2p-tools/lib/i2p" ) diff --git a/util/captcha/captcha.go b/utils/captcha/captcha.go similarity index 100% rename from util/captcha/captcha.go rename to utils/captcha/captcha.go diff --git a/util/categories/categories.go b/utils/categories/categories.go similarity index 100% rename from util/categories/categories.go rename to utils/categories/categories.go diff --git a/util/categories/categories_test.go b/utils/categories/categories_test.go similarity index 100% rename from util/categories/categories_test.go rename to utils/categories/categories_test.go diff --git a/util/cookies/helpers.go b/utils/cookies/helpers.go similarity index 100% rename from util/cookies/helpers.go rename to utils/cookies/helpers.go diff --git a/util/cookies/user.go b/utils/cookies/user.go similarity index 100% rename from util/cookies/user.go rename to utils/cookies/user.go diff --git a/util/crypto/crypto.go b/utils/crypto/crypto.go similarity index 100% rename from util/crypto/crypto.go rename to utils/crypto/crypto.go diff --git a/util/email/email.go b/utils/email/email.go similarity index 97% rename from util/email/email.go rename to utils/email/email.go index 27275cc2..1c574041 100644 --- a/util/email/email.go +++ b/utils/email/email.go @@ -4,7 +4,7 @@ import ( "path/filepath" "github.com/NyaaPantsu/nyaa/config" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" gomail "gopkg.in/gomail.v2" ) diff --git a/util/email/verification.go b/utils/email/verification.go similarity index 94% rename from util/email/verification.go rename to utils/email/verification.go index 40b18530..874eca1c 100644 --- a/util/email/verification.go +++ b/utils/email/verification.go @@ -10,9 +10,9 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/db" "github.com/NyaaPantsu/nyaa/model" - "github.com/NyaaPantsu/nyaa/util" - "github.com/NyaaPantsu/nyaa/util/publicSettings" - "github.com/NyaaPantsu/nyaa/util/timeHelper" + "github.com/NyaaPantsu/nyaa/utils" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/timeHelper" "github.com/gin-gonic/gin" "github.com/gorilla/securecookie" "github.com/NyaaPantsu/nyaa/models/users" diff --git a/util/feeds/convert.go b/utils/feeds/convert.go similarity index 95% rename from util/feeds/convert.go rename to utils/feeds/convert.go index b755bac7..54ac57d3 100644 --- a/util/feeds/convert.go +++ b/utils/feeds/convert.go @@ -4,7 +4,7 @@ import ( "strconv" "strings" - "github.com/NyaaPantsu/nyaa/util/categories" + "github.com/NyaaPantsu/nyaa/utils/categories" ) // ConvertToCat : Convert a torznab cat to our cat diff --git a/util/feeds/rss.go b/utils/feeds/rss.go similarity index 100% rename from util/feeds/rss.go rename to utils/feeds/rss.go diff --git a/util/filelist/filelist.go b/utils/filelist/filelist.go similarity index 100% rename from util/filelist/filelist.go rename to utils/filelist/filelist.go diff --git a/util/format/format.go b/utils/format/format.go similarity index 100% rename from util/format/format.go rename to utils/format/format.go diff --git a/util/format/format_test.go b/utils/format/format_test.go similarity index 100% rename from util/format/format_test.go rename to utils/format/format_test.go diff --git a/util/format/magnet.go b/utils/format/magnet.go similarity index 100% rename from util/format/magnet.go rename to utils/format/magnet.go diff --git a/util/log/error.go b/utils/log/error.go similarity index 100% rename from util/log/error.go rename to utils/log/error.go diff --git a/util/log/logger.go b/utils/log/logger.go similarity index 100% rename from util/log/logger.go rename to utils/log/logger.go diff --git a/util/messages/messages.go b/utils/messages/messages.go similarity index 99% rename from util/messages/messages.go rename to utils/messages/messages.go index 4ec8b91e..41a7fb86 100644 --- a/util/messages/messages.go +++ b/utils/messages/messages.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" - "github.com/NyaaPantsu/nyaa/util/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" "github.com/gin-gonic/gin" "github.com/gorilla/context" "github.com/nicksnyder/go-i18n/i18n" diff --git a/util/metainfo/doc.go b/utils/metainfo/doc.go similarity index 100% rename from util/metainfo/doc.go rename to utils/metainfo/doc.go diff --git a/util/metainfo/errors.go b/utils/metainfo/errors.go similarity index 100% rename from util/metainfo/errors.go rename to utils/metainfo/errors.go diff --git a/util/metainfo/metainfo.go b/utils/metainfo/metainfo.go similarity index 100% rename from util/metainfo/metainfo.go rename to utils/metainfo/metainfo.go diff --git a/util/publicSettings/publicSettings.go b/utils/publicSettings/publicSettings.go similarity index 100% rename from util/publicSettings/publicSettings.go rename to utils/publicSettings/publicSettings.go diff --git a/util/publicSettings/publicSettings_test.go b/utils/publicSettings/publicSettings_test.go similarity index 100% rename from util/publicSettings/publicSettings_test.go rename to utils/publicSettings/publicSettings_test.go diff --git a/util/sanitize/markdown.go b/utils/sanitize/markdown.go similarity index 100% rename from util/sanitize/markdown.go rename to utils/sanitize/markdown.go diff --git a/util/sanitize/safe.go b/utils/sanitize/safe.go similarity index 100% rename from util/sanitize/safe.go rename to utils/sanitize/safe.go diff --git a/util/sanitize/safe_test.go b/utils/sanitize/safe_test.go similarity index 100% rename from util/sanitize/safe_test.go rename to utils/sanitize/safe_test.go diff --git a/util/search/search.go b/utils/search/search.go similarity index 60% rename from util/search/search.go rename to utils/search/search.go index 066ed73f..3ff4bb5e 100644 --- a/util/search/search.go +++ b/utils/search/search.go @@ -3,16 +3,14 @@ package search import ( "strconv" "strings" - "time" "unicode" "unicode/utf8" - "github.com/NyaaPantsu/nyaa/common" "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/models/torrents" - "github.com/NyaaPantsu/nyaa/service" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" + "github.com/NyaaPantsu/nyaa/utils/search/structs" "github.com/gin-gonic/gin" ) @@ -23,7 +21,7 @@ var useTSQuery bool func Configure(conf *config.SearchConfig) (err error) { useTSQuery = false // Postgres needs ILIKE for case-insensitivity - if db.ORM.Dialect().GetName() == "postgres" { + if models.ORM.Dialect().GetName() == "postgres" { searchOperator = "ILIKE ?" //useTSQuery = true // !!DISABLED!! because this makes search a lot stricter @@ -44,31 +42,31 @@ func stringIsASCII(input string) bool { } // SearchByQuery : search torrents according to request without user -func SearchByQuery(c *gin.Context, pagenum int) (search common.SearchParam, tor []models.Torrent, count int, err error) { +func SearchByQuery(c *gin.Context, pagenum int) (search structs.TorrentParam, tor []models.Torrent, count int, err error) { search, tor, count, err = searchByQuery(c, pagenum, true, false, false, false) return } // SearchByQueryWithUser : search torrents according to request with user -func SearchByQueryWithUser(c *gin.Context, pagenum int) (search common.SearchParam, tor []models.Torrent, count int, err error) { +func SearchByQueryWithUser(c *gin.Context, pagenum int) (search structs.TorrentParam, tor []models.Torrent, count int, err error) { search, tor, count, err = searchByQuery(c, pagenum, true, true, false, false) return } // SearchByQueryNoCount : search torrents according to request without user and count -func SearchByQueryNoCount(c *gin.Context, pagenum int) (search common.SearchParam, tor []models.Torrent, err error) { +func SearchByQueryNoCount(c *gin.Context, pagenum int) (search structs.TorrentParam, tor []models.Torrent, err error) { search, tor, _, err = searchByQuery(c, pagenum, false, false, false, false) return } // SearchByQueryDeleted : search deleted torrents according to request with user and count -func SearchByQueryDeleted(c *gin.Context, pagenum int) (search common.SearchParam, tor []models.Torrent, count int, err error) { +func SearchByQueryDeleted(c *gin.Context, pagenum int) (search structs.TorrentParam, tor []models.Torrent, count int, err error) { search, tor, count, err = searchByQuery(c, pagenum, true, true, true, false) return } // SearchByQueryNoHidden : search torrents and filter those hidden -func SearchByQueryNoHidden(c *gin.Context, pagenum int) (search common.SearchParam, tor []models.Torrent, count int, err error) { +func SearchByQueryNoHidden(c *gin.Context, pagenum int) (search structs.TorrentParam, tor []models.Torrent, count int, err error) { search, tor, count, err = searchByQuery(c, pagenum, true, false, false, true) return } @@ -79,35 +77,16 @@ func SearchByQueryNoHidden(c *gin.Context, pagenum int) (search common.SearchPar // elasticsearch always provide a count to how many hits // deleted is unused because es doesn't index deleted torrents func searchByQuery(c *gin.Context, pagenum int, countAll bool, withUser bool, deleted bool, hidden bool) ( - search common.SearchParam, tor []models.Torrent, count int, err error, + search structs.TorrentParam, tor []models.Torrent, count int, err error, ) { - if db.ElasticSearchClient != nil { - var torrentParam common.TorrentParam + if models.ElasticSearchClient != nil { + var torrentParam structs.TorrentParam torrentParam.FromRequest(c) torrentParam.Offset = uint32(pagenum) torrentParam.Hidden = hidden - totalHits, torrents, err := torrentParam.Find(db.ElasticSearchClient) - searchParam := common.SearchParam{ - TorrentID: uint(torrentParam.TorrentID), - FromID: uint(torrentParam.FromID), - FromDate: torrentParam.FromDate, - ToDate: torrentParam.ToDate, - Order: torrentParam.Order, - Hidden: torrentParam.Hidden, - Status: torrentParam.Status, - Sort: torrentParam.Sort, - Category: torrentParam.Category, - Page: int(torrentParam.Offset), - UserID: uint(torrentParam.UserID), - Max: uint(torrentParam.Max), - NotNull: torrentParam.NotNull, - Language: torrentParam.Language, - MinSize: torrentParam.MinSize, - MaxSize: torrentParam.MaxSize, - Query: torrentParam.NameLike, - } + totalHits, torrents, err := torrentParam.Find(models.ElasticSearchClient) // Convert back to non-json torrents - return searchParam, torrents, int(totalHits), err + return torrentParam, torrents, int(totalHits), err } log.Errorf("Unable to create elasticsearch client: %s", err) log.Errorf("Falling back to postgresql query") @@ -115,64 +94,34 @@ func searchByQuery(c *gin.Context, pagenum int, countAll bool, withUser bool, de } func searchByQueryPostgres(c *gin.Context, pagenum int, countAll bool, withUser bool, deleted bool, hidden bool) ( - search common.SearchParam, tor []models.Torrent, count int, err error, + search structs.TorrentParam, tor []models.Torrent, count int, err error, ) { - max, err := strconv.ParseUint(c.DefaultQuery("limit", "50"), 10, 32) - if err != nil { - max = 50 // default Value maxPerPage - } else if max > 300 { - max = 300 - } - search.Max = uint(max) + search.FromRequest(c) - search.Page = pagenum + search.Offset = uint32(pagenum) search.Hidden = hidden - search.Query = c.Query("q") - search.Language = c.Query("lang") - userID, _ := strconv.Atoi(c.Query("userID")) - search.UserID = uint(userID) - fromID, _ := strconv.Atoi(c.Query("fromID")) - search.FromID = uint(fromID) - - maxage, err := strconv.Atoi(c.Query("maxage")) - if err != nil { - if c.Query("toDate") != "" { - search.FromDate.Parse(c.Query("toDate"), c.Query("dateType")) - search.ToDate.Parse(c.Query("fromDate"), c.Query("dateType")) - } else { - search.FromDate.Parse(c.Query("fromDate"), c.Query("dateType")) - } - } else { - search.FromDate = common.DateFilter(time.Now().AddDate(0, 0, -maxage).Format("2006-01-02")) - } - search.Category = common.ParseCategories(c.Query("c")) - search.Status.Parse(c.Query("s")) - search.Sort.Parse(c.Query("sort")) - search.MinSize.Parse(c.Query("minSize"), c.Query("sizeType")) - search.MaxSize.Parse(c.Query("maxSize"), c.Query("sizeType")) orderBy := search.Sort.ToDBField() - if search.Sort == common.Date { + if search.Sort == structs.Date { search.NotNull = search.Sort.ToDBField() + " IS NOT NULL" } orderBy += " " - switch s := c.Query("order"); s { - case "true": - search.Order = true + switch search.Order { + case true: orderBy += "asc" - if db.ORM.Dialect().GetName() == "postgres" { + if models.ORM.Dialect().GetName() == "postgres" { orderBy += " NULLS FIRST" } - default: + case false: orderBy += "desc" - if db.ORM.Dialect().GetName() == "postgres" { + if models.ORM.Dialect().GetName() == "postgres" { orderBy += " NULLS LAST" } } - parameters := serviceBase.WhereParams{ + parameters := structs.WhereParams{ Params: make([]interface{}, 0, 64), } conditions := make([]string, 0, 64) @@ -207,7 +156,7 @@ func searchByQueryPostgres(c *gin.Context, pagenum int, countAll bool, withUser parameters.Params = append(parameters.Params, search.ToDate) } if search.Status != 0 { - if search.Status == common.FilterRemakes { + if search.Status == structs.FilterRemakes { conditions = append(conditions, "status <> ?") } else { conditions = append(conditions, "status >= ?") @@ -230,8 +179,8 @@ func searchByQueryPostgres(c *gin.Context, pagenum int, countAll bool, withUser parameters.Params = append(parameters.Params, uint64(search.MaxSize)) } - searchQuerySplit := strings.Fields(search.Query) - for _, word := range searchQuerySplit { + querySplit := strings.Fields(search.NameLike) + for _, word := range querySplit { firstRune, _ := utf8.DecodeRuneInString(word) if len(word) == 1 && unicode.IsPunct(firstRune) { // some queries have a single punctuation character @@ -257,13 +206,13 @@ func searchByQueryPostgres(c *gin.Context, pagenum int, countAll bool, withUser log.Infof("SQL query is :: %s\n", parameters.Conditions) if deleted { - tor, count, err = torrentService.GetDeletedTorrents(¶meters, orderBy, int(search.Max), int(search.Max)*(search.Page-1)) + tor, count, err = torrents.FindDeleted(¶meters, orderBy, int(search.Max), int(search.Max*(search.Offset-1))) } else if countAll && !withUser { - tor, count, err = torrentService.GetTorrentsOrderBy(¶meters, orderBy, int(search.Max), int(search.Max)*(search.Page-1)) + tor, count, err = torrents.FindOrderBy(¶meters, orderBy, int(search.Max), int(search.Max*(search.Offset-1))) } else if withUser { - tor, count, err = torrentService.GetTorrentsWithUserOrderBy(¶meters, orderBy, int(search.Max), int(search.Max)*(search.Page-1)) + tor, count, err = torrents.FindWithUserOrderBy(¶meters, orderBy, int(search.Max), int(search.Max*(search.Offset-1))) } else { - tor, err = torrentService.GetTorrentsOrderByNoCount(¶meters, orderBy, int(search.Max), int(search.Max)*(search.Page-1)) + tor, err = torrents.FindOrderByNoCount(¶meters, orderBy, int(search.Max), int(search.Max*(search.Offset-1))) } return } diff --git a/service/service.go b/utils/search/structs/service.go similarity index 95% rename from service/service.go rename to utils/search/structs/service.go index a28e9cd8..c191e85c 100644 --- a/service/service.go +++ b/utils/search/structs/service.go @@ -1,4 +1,4 @@ -package serviceBase +package structs // WhereParams struct for search type WhereParams struct { diff --git a/common/search.go b/utils/search/structs/structs.go similarity index 89% rename from common/search.go rename to utils/search/structs/structs.go index d63b6413..ae8a096e 100644 --- a/common/search.go +++ b/utils/search/structs/structs.go @@ -1,4 +1,4 @@ -package common +package structs import ( "math" @@ -10,11 +10,9 @@ import ( "strings" "github.com/NyaaPantsu/nyaa/config" - catUtil "github.com/NyaaPantsu/nyaa/util/categories" + catUtil "github.com/NyaaPantsu/nyaa/utils/categories" ) -type Status uint8 - const ( ShowAll Status = 0 FilterRemakes = 2 @@ -22,6 +20,39 @@ const ( APlus = 4 ) +type Status uint8 +type SortMode uint8 +type Category struct { + Main, Sub uint8 +} +type SizeBytes uint64 +type DateFilter string +type Categories []*Category + +// TorrentParam defines all parameters that can be provided when searching for a torrent +type TorrentParam struct { + All bool // True means ignore everything but Max and Offset + Full bool // True means load all members + Order bool // True means ascending + Hidden bool // True means filter hidden torrents + Status Status + Sort SortMode + Category Categories + Max uint32 + Offset uint32 + UserID uint32 + TorrentID uint32 + FromID uint32 + FromDate DateFilter + ToDate DateFilter + NotNull string // csv + Null string // csv + NameLike string // csv + Language string + MinSize SizeBytes + MaxSize SizeBytes +} + func (st *Status) ToString() string { switch *st { case FilterRemakes: @@ -47,7 +78,6 @@ func (st *Status) Parse(s string) { } } -type SortMode uint8 const ( ID SortMode = iota @@ -129,9 +159,6 @@ func (s *SortMode) ToDBField() string { return config.Conf.Torrents.Order } -type Category struct { - Main, Sub uint8 -} func (c Category) String() (s string) { if c.Main != 0 { @@ -187,7 +214,6 @@ func ParseCategories(s string) []*Category { return Categories{} } -type SizeBytes uint64 func (sz *SizeBytes) Parse(s string, sizeType string) bool { if s == "" { @@ -214,7 +240,6 @@ func (sz *SizeBytes) Parse(s string, sizeType string) bool { return true } -type DateFilter string func (d *DateFilter) Parse(s string, dateType string) bool { if s == "" { @@ -237,25 +262,3 @@ func (d *DateFilter) Parse(s string, dateType string) bool { return true } -type Categories []*Category - -// deprecated for TorrentParam -type SearchParam struct { - TorrentID uint - FromID uint // Search for torrentID > FromID - Order bool // True means acsending - Hidden bool // True means filter hidden torrents - Status Status - Sort SortMode - Category Categories - FromDate DateFilter - ToDate DateFilter - Page int - UserID uint - Max uint - NotNull string - Language string - MinSize SizeBytes - MaxSize SizeBytes - Query string -} diff --git a/common/search_test.go b/utils/search/structs/structs_test.go similarity index 87% rename from common/search_test.go rename to utils/search/structs/structs_test.go index 7f744282..7826ec4c 100644 --- a/common/search_test.go +++ b/utils/search/structs/structs_test.go @@ -1,4 +1,4 @@ -package common +package structs import ( "path" @@ -11,8 +11,8 @@ import ( // run before config/parse.go:init() var _ = func() (_ struct{}) { - config.ConfigPath = path.Join("..", config.ConfigPath) - config.DefaultConfigPath = path.Join("..", config.DefaultConfigPath) + config.ConfigPath = path.Join("..", "..", "..", config.ConfigPath) + config.DefaultConfigPath = path.Join("..", "..", "..", config.DefaultConfigPath) config.Parse() return }() diff --git a/common/torrent.go b/utils/search/structs/torrent.go similarity index 89% rename from common/torrent.go rename to utils/search/structs/torrent.go index 1f3970a3..e8c865fd 100644 --- a/common/torrent.go +++ b/utils/search/structs/torrent.go @@ -1,4 +1,4 @@ -package common +package structs import ( "context" @@ -11,41 +11,17 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/models" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" "github.com/gin-gonic/gin" ) -// TorrentParam defines all parameters that can be provided when searching for a torrent -type TorrentParam struct { - All bool // True means ignore everything but Max and Offset - Full bool // True means load all members - Order bool // True means ascending - Hidden bool // True means filter hidden torrents - Status Status - Sort SortMode - Category Categories - Max uint32 - Offset uint32 - UserID uint32 - TorrentID uint32 - FromID uint32 - FromDate DateFilter - ToDate DateFilter - NotNull string // csv - Null string // csv - NameLike string // csv - Language string - MinSize SizeBytes - MaxSize SizeBytes -} - // FromRequest : parse a request in torrent param // TODO Should probably return an error ? func (p *TorrentParam) FromRequest(c *gin.Context) { var err error nameLike := strings.TrimSpace(c.Query("q")) - max, err := strconv.ParseUint(c.Query("limit"), 10, 32) + max, err := strconv.ParseUint(c.DefaultQuery("limit", strconv.Itoa(config.Conf.Navigation.TorrentsPerPage)), 10, 32) if err != nil { max = uint64(config.Conf.Navigation.TorrentsPerPage) } else if max > uint64(config.Conf.Navigation.MaxTorrentsPerPage) { diff --git a/util/signals/interrupt.go b/utils/signals/interrupt.go similarity index 93% rename from util/signals/interrupt.go rename to utils/signals/interrupt.go index 45f14def..eca8fbb1 100644 --- a/util/signals/interrupt.go +++ b/utils/signals/interrupt.go @@ -3,7 +3,7 @@ package signals import ( "sync" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" ) // registered interrupt callbacks. diff --git a/util/signals/unix.go b/utils/signals/unix.go similarity index 100% rename from util/signals/unix.go rename to utils/signals/unix.go diff --git a/util/signals/win32.go b/utils/signals/win32.go similarity index 100% rename from util/signals/win32.go rename to utils/signals/win32.go diff --git a/util/timeHelper/time_helper.go b/utils/timeHelper/time_helper.go similarity index 97% rename from util/timeHelper/time_helper.go rename to utils/timeHelper/time_helper.go index ddefe987..10df3b5e 100644 --- a/util/timeHelper/time_helper.go +++ b/utils/timeHelper/time_helper.go @@ -3,7 +3,7 @@ package timeHelper import ( "time" - "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/utils/log" ) // FewDaysLater : Give time now + some days diff --git a/util/torrentLanguages/torrent_languages.go b/utils/torrentLanguages/torrent_languages.go similarity index 95% rename from util/torrentLanguages/torrent_languages.go rename to utils/torrentLanguages/torrent_languages.go index 8201bede..5dc9af4c 100644 --- a/util/torrentLanguages/torrent_languages.go +++ b/utils/torrentLanguages/torrent_languages.go @@ -4,7 +4,7 @@ import ( "strings" "github.com/NyaaPantsu/nyaa/config" - "github.com/NyaaPantsu/nyaa/util/publicSettings" + "github.com/NyaaPantsu/nyaa/utils/publicSettings" ) var torrentLanguages []string diff --git a/util/torrentLanguages/torrent_languages_test.go b/utils/torrentLanguages/torrent_languages_test.go similarity index 100% rename from util/torrentLanguages/torrent_languages_test.go rename to utils/torrentLanguages/torrent_languages_test.go diff --git a/util/upload/upload.go b/utils/upload/upload.go similarity index 98% rename from util/upload/upload.go rename to utils/upload/upload.go index f4e33ffb..22ad4836 100644 --- a/util/upload/upload.go +++ b/utils/upload/upload.go @@ -12,8 +12,8 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/service" - "github.com/NyaaPantsu/nyaa/util/sanitize" - "github.com/NyaaPantsu/nyaa/util/validator/torrent" + "github.com/NyaaPantsu/nyaa/utils/sanitize" + "github.com/NyaaPantsu/nyaa/utils/validator/torrent" "github.com/gin-gonic/gin" ) diff --git a/util/validator/number.go b/utils/validator/number.go similarity index 95% rename from util/validator/number.go rename to utils/validator/number.go index c679f80e..817afdc5 100644 --- a/util/validator/number.go +++ b/utils/validator/number.go @@ -1,7 +1,7 @@ package validator import ( - "github.com/NyaaPantsu/nyaa/util/messages" + "github.com/NyaaPantsu/nyaa/utils/messages" "github.com/go-playground/validator" ) diff --git a/util/validator/slices.go b/utils/validator/slices.go similarity index 95% rename from util/validator/slices.go rename to utils/validator/slices.go index 8bee4dbd..d02e600a 100644 --- a/util/validator/slices.go +++ b/utils/validator/slices.go @@ -1,7 +1,7 @@ package validator import ( - "github.com/NyaaPantsu/nyaa/util/messages" + "github.com/NyaaPantsu/nyaa/utils/messages" "github.com/go-playground/validator" ) diff --git a/util/validator/string.go b/utils/validator/string.go similarity index 94% rename from util/validator/string.go rename to utils/validator/string.go index 9e7a4262..adcb0b6f 100644 --- a/util/validator/string.go +++ b/utils/validator/string.go @@ -1,7 +1,7 @@ package validator import ( - "github.com/NyaaPantsu/nyaa/util/messages" + "github.com/NyaaPantsu/nyaa/utils/messages" "github.com/go-playground/validator" ) diff --git a/util/validator/struct.go b/utils/validator/struct.go similarity index 92% rename from util/validator/struct.go rename to utils/validator/struct.go index ce3375b9..652f1f5c 100644 --- a/util/validator/struct.go +++ b/utils/validator/struct.go @@ -1,7 +1,7 @@ package validator import ( - "github.com/NyaaPantsu/nyaa/util/messages" + "github.com/NyaaPantsu/nyaa/utils/messages" "github.com/go-playground/validator" ) diff --git a/util/validator/torrent/forms.go b/utils/validator/torrent/forms.go similarity index 100% rename from util/validator/torrent/forms.go rename to utils/validator/torrent/forms.go diff --git a/util/validator/torrent/functions.go b/utils/validator/torrent/functions.go similarity index 97% rename from util/validator/torrent/functions.go rename to utils/validator/torrent/functions.go index 2bb7aba1..a0cb1802 100644 --- a/util/validator/torrent/functions.go +++ b/utils/validator/torrent/functions.go @@ -5,10 +5,10 @@ import ( "encoding/hex" "errors" "github.com/NyaaPantsu/nyaa/config" - "github.com/NyaaPantsu/nyaa/util/categories" - "github.com/NyaaPantsu/nyaa/util/format" - "github.com/NyaaPantsu/nyaa/util/metainfo" - "github.com/NyaaPantsu/nyaa/util/torrentLanguages" + "github.com/NyaaPantsu/nyaa/utils/categories" + "github.com/NyaaPantsu/nyaa/utils/format" + "github.com/NyaaPantsu/nyaa/utils/metainfo" + "github.com/NyaaPantsu/nyaa/utils/torrentLanguages" "github.com/gin-gonic/gin" "github.com/zeebo/bencode" "io" diff --git a/util/validator/torrent/helpers.go b/utils/validator/torrent/helpers.go similarity index 100% rename from util/validator/torrent/helpers.go rename to utils/validator/torrent/helpers.go diff --git a/util/validator/user/forms.go b/utils/validator/user/forms.go similarity index 100% rename from util/validator/user/forms.go rename to utils/validator/user/forms.go diff --git a/util/validator/user/forms_test.go b/utils/validator/user/forms_test.go similarity index 95% rename from util/validator/user/forms_test.go rename to utils/validator/user/forms_test.go index 714172db..e26cb0a8 100644 --- a/util/validator/user/forms_test.go +++ b/utils/validator/user/forms_test.go @@ -1,8 +1,8 @@ package userValidator import ( - msg "github.com/NyaaPantsu/nyaa/util/messages" - "github.com/NyaaPantsu/nyaa/util/validator" + msg "github.com/NyaaPantsu/nyaa/utils/messages" + "github.com/NyaaPantsu/nyaa/utils/validator" "github.com/gin-gonic/gin" "net/http" "testing" diff --git a/util/validator/user/functions.go b/utils/validator/user/functions.go similarity index 95% rename from util/validator/user/functions.go rename to utils/validator/user/functions.go index 707c5bec..c8cae92c 100644 --- a/util/validator/user/functions.go +++ b/utils/validator/user/functions.go @@ -3,8 +3,8 @@ package userValidator import ( "regexp" - "github.com/NyaaPantsu/nyaa/util/log" - "github.com/NyaaPantsu/nyaa/util/validator" + "github.com/NyaaPantsu/nyaa/utils/log" + "github.com/NyaaPantsu/nyaa/utils/validator" ) // Regex by: Philippe Verdy (in a comment somewhere on a website) - Valid every email RFC valid diff --git a/util/validator/user/functions_test.go b/utils/validator/user/functions_test.go similarity index 100% rename from util/validator/user/functions_test.go rename to utils/validator/user/functions_test.go diff --git a/util/validator/validator.go b/utils/validator/validator.go similarity index 98% rename from util/validator/validator.go rename to utils/validator/validator.go index 225750dc..27a2972e 100644 --- a/util/validator/validator.go +++ b/utils/validator/validator.go @@ -6,8 +6,8 @@ import ( "time" "unicode" - "github.com/NyaaPantsu/nyaa/util/log" - msg "github.com/NyaaPantsu/nyaa/util/messages" + "github.com/NyaaPantsu/nyaa/utils/log" + msg "github.com/NyaaPantsu/nyaa/utils/messages" "github.com/go-playground/validator" "strconv" ) diff --git a/util/validator/validator_test.go b/utils/validator/validator_test.go similarity index 98% rename from util/validator/validator_test.go rename to utils/validator/validator_test.go index 182d5640..9e4a3507 100644 --- a/util/validator/validator_test.go +++ b/utils/validator/validator_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/NyaaPantsu/nyaa/config" - msg "github.com/NyaaPantsu/nyaa/util/messages" + msg "github.com/NyaaPantsu/nyaa/utils/messages" "github.com/gin-gonic/gin" )