Related refactors to ctx.FormX functions (#16567)

* use FormTrim if posible

* speedup goGet

* only convert if nessesary
This commit is contained in:
6543 2021-08-11 17:08:52 +02:00 committed by GitHub
parent 2d25b7d44b
commit f1a810e090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 36 additions and 52 deletions

View File

@ -171,7 +171,7 @@ func ReadRepoNotifications(ctx *context.APIContext) {
// "$ref": "#/responses/empty" // "$ref": "#/responses/empty"
lastRead := int64(0) lastRead := int64(0)
qLastRead := strings.Trim(ctx.FormString("last_read_at"), " ") qLastRead := ctx.FormTrim("last_read_at")
if len(qLastRead) > 0 { if len(qLastRead) > 0 {
tmpLastRead, err := time.Parse(time.RFC3339, qLastRead) tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
if err != nil { if err != nil {

View File

@ -6,7 +6,6 @@ package notify
import ( import (
"net/http" "net/http"
"strings"
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -122,7 +121,7 @@ func ReadNotifications(ctx *context.APIContext) {
// "$ref": "#/responses/empty" // "$ref": "#/responses/empty"
lastRead := int64(0) lastRead := int64(0)
qLastRead := strings.Trim(ctx.FormString("last_read_at"), " ") qLastRead := ctx.FormTrim("last_read_at")
if len(qLastRead) > 0 { if len(qLastRead) > 0 {
tmpLastRead, err := time.Parse(time.RFC3339, qLastRead) tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
if err != nil { if err != nil {

View File

@ -8,7 +8,6 @@ package org
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -658,7 +657,7 @@ func SearchTeam(ctx *context.APIContext) {
opts := &models.SearchTeamOptions{ opts := &models.SearchTeamOptions{
UserID: ctx.User.ID, UserID: ctx.User.ID,
Keyword: strings.TrimSpace(ctx.FormString("q")), Keyword: ctx.FormTrim("q"),
OrgID: ctx.Org.Organization.ID, OrgID: ctx.Org.Organization.ID,
IncludeDesc: ctx.FormString("include_desc") == "" || ctx.FormBool("include_desc"), IncludeDesc: ctx.FormString("include_desc") == "" || ctx.FormBool("include_desc"),
ListOptions: listOptions, ListOptions: listOptions,

View File

@ -140,7 +140,7 @@ func SearchIssues(ctx *context.APIContext) {
var issues []*models.Issue var issues []*models.Issue
var filteredCount int64 var filteredCount int64
keyword := strings.Trim(ctx.FormString("q"), " ") keyword := ctx.FormTrim("q")
if strings.IndexByte(keyword, 0) >= 0 { if strings.IndexByte(keyword, 0) >= 0 {
keyword = "" keyword = ""
} }
@ -162,13 +162,13 @@ func SearchIssues(ctx *context.APIContext) {
isPull = util.OptionalBoolNone isPull = util.OptionalBoolNone
} }
labels := strings.TrimSpace(ctx.FormString("labels")) labels := ctx.FormTrim("labels")
var includedLabelNames []string var includedLabelNames []string
if len(labels) > 0 { if len(labels) > 0 {
includedLabelNames = strings.Split(labels, ",") includedLabelNames = strings.Split(labels, ",")
} }
milestones := strings.TrimSpace(ctx.FormString("milestones")) milestones := ctx.FormTrim("milestones")
var includedMilestones []string var includedMilestones []string
if len(milestones) > 0 { if len(milestones) > 0 {
includedMilestones = strings.Split(milestones, ",") includedMilestones = strings.Split(milestones, ",")
@ -331,7 +331,7 @@ func ListIssues(ctx *context.APIContext) {
var issues []*models.Issue var issues []*models.Issue
var filteredCount int64 var filteredCount int64
keyword := strings.Trim(ctx.FormString("q"), " ") keyword := ctx.FormTrim("q")
if strings.IndexByte(keyword, 0) >= 0 { if strings.IndexByte(keyword, 0) >= 0 {
keyword = "" keyword = ""
} }

View File

@ -7,7 +7,6 @@ package repo
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -90,7 +89,7 @@ func ListTrackedTimes(ctx *context.APIContext) {
IssueID: issue.ID, IssueID: issue.ID,
} }
qUser := strings.Trim(ctx.FormString("user"), " ") qUser := ctx.FormTrim("user")
if qUser != "" { if qUser != "" {
user, err := models.GetUserByName(qUser) user, err := models.GetUserByName(qUser)
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
@ -500,7 +499,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
} }
// Filters // Filters
qUser := strings.Trim(ctx.FormString("user"), " ") qUser := ctx.FormTrim("user")
if qUser != "" { if qUser != "" {
user, err := models.GetUserByName(qUser) user, err := models.GetUserByName(qUser)
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {

View File

@ -135,7 +135,7 @@ func Search(ctx *context.APIContext) {
opts := &models.SearchRepoOptions{ opts := &models.SearchRepoOptions{
ListOptions: utils.GetListOptions(ctx), ListOptions: utils.GetListOptions(ctx),
Actor: ctx.User, Actor: ctx.User,
Keyword: strings.Trim(ctx.FormString("q"), " "), Keyword: ctx.FormTrim("q"),
OwnerID: ctx.FormInt64("uid"), OwnerID: ctx.FormInt64("uid"),
PriorityOwnerID: ctx.FormInt64("priority_owner_id"), PriorityOwnerID: ctx.FormInt64("priority_owner_id"),
TeamID: ctx.FormInt64("team_id"), TeamID: ctx.FormInt64("team_id"),

View File

@ -8,7 +8,6 @@ package user
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -58,7 +57,7 @@ func Search(ctx *context.APIContext) {
opts := &models.SearchUserOptions{ opts := &models.SearchUserOptions{
Actor: ctx.User, Actor: ctx.User,
Keyword: strings.Trim(ctx.FormString("q"), " "), Keyword: ctx.FormTrim("q"),
UID: ctx.FormInt64("uid"), UID: ctx.FormInt64("uid"),
Type: models.UserTypeIndividual, Type: models.UserTypeIndividual,
ListOptions: listOptions, ListOptions: listOptions,

View File

@ -7,7 +7,6 @@ package admin
import ( import (
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -111,7 +110,7 @@ func UnadoptedRepos(ctx *context.Context) {
func AdoptOrDeleteRepository(ctx *context.Context) { func AdoptOrDeleteRepository(ctx *context.Context) {
dir := ctx.FormString("id") dir := ctx.FormString("id")
action := ctx.FormString("action") action := ctx.FormString("action")
page := ctx.FormInt("page") page := ctx.FormString("page")
q := ctx.FormString("q") q := ctx.FormString("q")
dirSplit := strings.SplitN(dir, "/", 2) dirSplit := strings.SplitN(dir, "/", 2)
@ -162,5 +161,5 @@ func AdoptOrDeleteRepository(ctx *context.Context) {
} }
ctx.Flash.Success(ctx.Tr("repo.delete_preexisting_success", dir)) ctx.Flash.Success(ctx.Tr("repo.delete_preexisting_success", dir))
} }
ctx.Redirect(setting.AppSubURL + "/admin/repos/unadopted?search=true&q=" + url.QueryEscape(q) + "&page=" + strconv.Itoa(page)) ctx.Redirect(setting.AppSubURL + "/admin/repos/unadopted?search=true&q=" + url.QueryEscape(q) + "&page=" + page)
} }

View File

@ -6,7 +6,6 @@ package explore
import ( import (
"net/http" "net/http"
"strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
@ -33,14 +32,14 @@ func Code(ctx *context.Context) {
ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreCode"] = true ctx.Data["PageIsExploreCode"] = true
language := strings.TrimSpace(ctx.FormString("l")) language := ctx.FormTrim("l")
keyword := strings.TrimSpace(ctx.FormString("q")) keyword := ctx.FormTrim("q")
page := ctx.FormInt("page") page := ctx.FormInt("page")
if page <= 0 { if page <= 0 {
page = 1 page = 1
} }
queryType := strings.TrimSpace(ctx.FormString("t")) queryType := ctx.FormTrim("t")
isMatch := queryType == "match" isMatch := queryType == "match"
var ( var (

View File

@ -6,7 +6,6 @@ package explore
import ( import (
"net/http" "net/http"
"strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
@ -73,7 +72,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
orderBy = models.SearchOrderByRecentUpdated orderBy = models.SearchOrderByRecentUpdated
} }
keyword := strings.Trim(ctx.FormString("q"), " ") keyword := ctx.FormTrim("q")
topicOnly := ctx.FormBool("topic") topicOnly := ctx.FormBool("topic")
ctx.Data["TopicOnly"] = topicOnly ctx.Data["TopicOnly"] = topicOnly

View File

@ -7,7 +7,6 @@ package explore
import ( import (
"bytes" "bytes"
"net/http" "net/http"
"strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
@ -63,7 +62,7 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN
orderBy = models.SearchOrderByAlphabetically orderBy = models.SearchOrderByAlphabetically
} }
opts.Keyword = strings.Trim(ctx.FormString("q"), " ") opts.Keyword = ctx.FormTrim("q")
opts.OrderBy = orderBy opts.OrderBy = orderBy
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) { if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
users, count, err = models.SearchUsers(opts) users, count, err = models.SearchUsers(opts)

View File

@ -18,7 +18,7 @@ import (
) )
func goGet(ctx *context.Context) { func goGet(ctx *context.Context) {
if ctx.Req.Method != "GET" || ctx.FormString("go-get") != "1" || len(ctx.Req.URL.Query()) > 1 { if ctx.Req.Method != "GET" || len(ctx.Req.URL.RawQuery) < 8 || ctx.FormString("go-get") != "1" {
return return
} }

View File

@ -6,7 +6,6 @@ package org
import ( import (
"net/http" "net/http"
"strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
@ -78,7 +77,7 @@ func Home(ctx *context.Context) {
orderBy = models.SearchOrderByRecentUpdated orderBy = models.SearchOrderByRecentUpdated
} }
keyword := strings.Trim(ctx.FormString("q"), " ") keyword := ctx.FormTrim("q")
ctx.Data["Keyword"] = keyword ctx.Data["Keyword"] = keyword
page := ctx.FormInt("page") page := ctx.FormInt("page")

View File

@ -177,7 +177,7 @@ func SearchCommits(ctx *context.Context) {
ctx.Data["PageIsCommits"] = true ctx.Data["PageIsCommits"] = true
ctx.Data["PageIsViewCode"] = true ctx.Data["PageIsViewCode"] = true
query := strings.Trim(ctx.FormString("q"), " ") query := ctx.FormTrim("q")
if len(query) == 0 { if len(query) == 0 {
ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL()) ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
return return

View File

@ -1722,14 +1722,12 @@ func UpdateIssueContent(ctx *context.Context) {
return return
} }
content := ctx.FormString("content") if err := issue_service.ChangeContent(issue, ctx.User, ctx.Req.FormValue("content")); err != nil {
if err := issue_service.ChangeContent(issue, ctx.User, content); err != nil {
ctx.ServerError("ChangeContent", err) ctx.ServerError("ChangeContent", err)
return return
} }
files := ctx.FormStrings("files[]") if err := updateAttachments(issue, ctx.FormStrings("files[]")); err != nil {
if err := updateAttachments(issue, files); err != nil {
ctx.ServerError("UpdateAttachments", err) ctx.ServerError("UpdateAttachments", err)
return return
} }
@ -2157,8 +2155,7 @@ func UpdateCommentContent(ctx *context.Context) {
return return
} }
files := ctx.FormStrings("files[]") if err := updateAttachments(comment, ctx.FormStrings("files[]")); err != nil {
if err := updateAttachments(comment, files); err != nil {
ctx.ServerError("UpdateAttachments", err) ctx.ServerError("UpdateAttachments", err)
return return
} }

View File

@ -6,7 +6,6 @@ package repo
import ( import (
"net/http" "net/http"
"strings"
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -47,7 +46,7 @@ func Milestones(ctx *context.Context) {
sortType := ctx.FormString("sort") sortType := ctx.FormString("sort")
keyword := strings.Trim(ctx.FormString("q"), " ") keyword := ctx.FormTrim("q")
page := ctx.FormInt("page") page := ctx.FormInt("page")
if page <= 1 { if page <= 1 {

View File

@ -6,7 +6,6 @@ package repo
import ( import (
"net/http" "net/http"
"strings"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -22,13 +21,13 @@ func Search(ctx *context.Context) {
ctx.Redirect(ctx.Repo.RepoLink, 302) ctx.Redirect(ctx.Repo.RepoLink, 302)
return return
} }
language := strings.TrimSpace(ctx.FormString("l")) language := ctx.FormTrim("l")
keyword := strings.TrimSpace(ctx.FormString("q")) keyword := ctx.FormTrim("q")
page := ctx.FormInt("page") page := ctx.FormInt("page")
if page <= 0 { if page <= 0 {
page = 1 page = 1
} }
queryType := strings.TrimSpace(ctx.FormString("t")) queryType := ctx.FormTrim("t")
isMatch := queryType == "match" isMatch := queryType == "match"
total, searchResults, searchResultLanguages, err := code_indexer.PerformSearch([]int64{ctx.Repo.Repository.ID}, total, searchResults, searchResultLanguages, err := code_indexer.PerformSearch([]int64{ctx.Repo.Repository.ID},

View File

@ -23,7 +23,7 @@ func TopicsPost(ctx *context.Context) {
} }
var topics = make([]string, 0) var topics = make([]string, 0)
var topicsStr = strings.TrimSpace(ctx.FormString("topics")) var topicsStr = ctx.FormTrim("topics")
if len(topicsStr) > 0 { if len(topicsStr) > 0 {
topics = strings.Split(topicsStr, ",") topics = strings.Split(topicsStr, ",")
} }

View File

@ -1491,8 +1491,7 @@ func ForgotPasswd(ctx *context.Context) {
return return
} }
email := ctx.FormString("email") ctx.Data["Email"] = ctx.FormString("email")
ctx.Data["Email"] = email
ctx.Data["IsResetRequest"] = true ctx.Data["IsResetRequest"] = true
ctx.HTML(http.StatusOK, tplForgotPassword) ctx.HTML(http.StatusOK, tplForgotPassword)

View File

@ -204,7 +204,7 @@ func Milestones(ctx *context.Context) {
isShowClosed = ctx.FormString("state") == "closed" isShowClosed = ctx.FormString("state") == "closed"
sortType = ctx.FormString("sort") sortType = ctx.FormString("sort")
page = ctx.FormInt("page") page = ctx.FormInt("page")
keyword = strings.Trim(ctx.FormString("q"), " ") keyword = ctx.FormTrim("q")
) )
if page <= 1 { if page <= 1 {

View File

@ -8,7 +8,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -59,7 +58,7 @@ func Notifications(c *context.Context) {
func getNotifications(c *context.Context) { func getNotifications(c *context.Context) {
var ( var (
keyword = strings.Trim(c.FormString("q"), " ") keyword = c.FormTrim("q")
status models.NotificationStatus status models.NotificationStatus
page = c.FormInt("page") page = c.FormInt("page")
perPage = c.FormInt("perPage") perPage = c.FormInt("perPage")
@ -144,9 +143,9 @@ func getNotifications(c *context.Context) {
// NotificationStatusPost is a route for changing the status of a notification // NotificationStatusPost is a route for changing the status of a notification
func NotificationStatusPost(c *context.Context) { func NotificationStatusPost(c *context.Context) {
var ( var (
notificationID, _ = strconv.ParseInt(c.Req.PostFormValue("notification_id"), 10, 64) notificationID = c.FormInt64("notification_id")
statusStr = c.Req.PostFormValue("status") statusStr = c.FormString("status")
status models.NotificationStatus status models.NotificationStatus
) )
switch statusStr { switch statusStr {

View File

@ -187,7 +187,7 @@ func Profile(ctx *context.Context) {
orderBy = models.SearchOrderByRecentUpdated orderBy = models.SearchOrderByRecentUpdated
} }
keyword := strings.Trim(ctx.FormString("q"), " ") keyword := ctx.FormTrim("q")
ctx.Data["Keyword"] = keyword ctx.Data["Keyword"] = keyword
switch tab { switch tab {
case "followers": case "followers":