2022-11-09 10:15:37 -08:00
|
|
|
package manage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gochan-org/gochan/pkg/gcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getStringField(field string, staff string, request *http.Request, logCallerOffset ...int) (string, error) {
|
|
|
|
action := request.FormValue("action")
|
|
|
|
callerOffset := 1
|
|
|
|
if len(logCallerOffset) > 0 {
|
|
|
|
callerOffset += logCallerOffset[0]
|
|
|
|
}
|
|
|
|
if len(request.Form[field]) == 0 {
|
|
|
|
gcutil.LogError(nil).
|
|
|
|
Str("IP", gcutil.GetRealIP(request)).
|
|
|
|
Str("staff", staff).
|
|
|
|
Str("action", action).
|
|
|
|
Str("field", field).
|
|
|
|
Caller(callerOffset).Msg("Missing required field")
|
|
|
|
return "", &ErrStaffAction{
|
|
|
|
ErrorField: field,
|
|
|
|
Action: action,
|
|
|
|
Message: fmt.Sprintf("Missing required field %q", field),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return request.FormValue(field), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getIntField gets the requested value from the form and tries to convert it to int. If it fails, it logs the error
|
|
|
|
// and wraps it in ErrStaffAction
|
|
|
|
func getIntField(field string, staff string, request *http.Request, logCallerOffset ...int) (int, error) {
|
|
|
|
action := request.FormValue("action")
|
|
|
|
callerOffset := 1
|
|
|
|
if len(logCallerOffset) > 0 {
|
|
|
|
callerOffset += logCallerOffset[0]
|
|
|
|
}
|
2022-12-05 16:18:19 -08:00
|
|
|
errEv := gcutil.LogError(nil).
|
|
|
|
Str("IP", gcutil.GetRealIP(request)).
|
|
|
|
Str("staff", staff).
|
|
|
|
Str("action", action).
|
|
|
|
Str("field", field)
|
|
|
|
defer errEv.Discard()
|
2022-11-09 10:15:37 -08:00
|
|
|
|
|
|
|
if len(request.Form[field]) == 0 {
|
2022-12-05 16:18:19 -08:00
|
|
|
errEv.Caller(callerOffset).Msg("Missing required field")
|
2022-11-09 10:15:37 -08:00
|
|
|
return 0, &ErrStaffAction{
|
|
|
|
ErrorField: field,
|
|
|
|
Action: action,
|
|
|
|
Message: fmt.Sprintf("Missing required field %q", field),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strVal := request.FormValue(field)
|
|
|
|
|
|
|
|
intVal, err := strconv.Atoi(strVal)
|
|
|
|
if err != nil {
|
2022-12-05 16:18:19 -08:00
|
|
|
errEv.Err(err).Caller(callerOffset).Msg("Unable to convert field to int")
|
2022-11-09 10:15:37 -08:00
|
|
|
return 0, &ErrStaffAction{
|
|
|
|
ErrorField: field,
|
|
|
|
Action: action,
|
2022-12-05 16:18:19 -08:00
|
|
|
Message: fmt.Sprintf("Unable to convert form field %q to int", field),
|
2022-11-09 10:15:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return intVal, nil
|
|
|
|
}
|