2020-04-19 05:01:09 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/thedevsaddam/govalidator"
|
|
|
|
|
|
|
|
"github.com/elyby/chrly/model"
|
|
|
|
)
|
|
|
|
|
2023-12-12 06:05:08 +05:30
|
|
|
// noinspection GoSnakeCaseUsage
|
2020-04-19 05:01:09 +05:30
|
|
|
const UUID_ANY = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
|
|
|
|
|
|
|
|
var regexUuidAny = regexp.MustCompile(UUID_ANY)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Add ability to validate any possible uuid form
|
|
|
|
govalidator.AddCustomRule("uuid_any", func(field string, rule string, message string, value interface{}) error {
|
|
|
|
str := value.(string)
|
|
|
|
if !regexUuidAny.MatchString(str) {
|
|
|
|
if message == "" {
|
|
|
|
message = fmt.Sprintf("The %s field must contain valid UUID", field)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Api struct {
|
|
|
|
SkinsRepo SkinsRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Api) Handler() *mux.Router {
|
|
|
|
router := mux.NewRouter().StrictSlash(true)
|
|
|
|
router.HandleFunc("/skins", ctx.postSkinHandler).Methods(http.MethodPost)
|
|
|
|
router.HandleFunc("/skins/id:{id:[0-9]+}", ctx.deleteSkinByUserIdHandler).Methods(http.MethodDelete)
|
|
|
|
router.HandleFunc("/skins/{username}", ctx.deleteSkinByUsernameHandler).Methods(http.MethodDelete)
|
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Api) postSkinHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
validationErrors := validatePostSkinRequest(req)
|
|
|
|
if validationErrors != nil {
|
|
|
|
apiBadRequest(resp, validationErrors)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
identityId, _ := strconv.Atoi(req.Form.Get("identityId"))
|
|
|
|
username := req.Form.Get("username")
|
|
|
|
|
|
|
|
record, err := ctx.findIdentityOrCleanup(identityId, username)
|
|
|
|
if err != nil {
|
2021-02-26 07:15:45 +05:30
|
|
|
panic(err)
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2020-04-20 17:46:15 +05:30
|
|
|
if record == nil {
|
|
|
|
record = &model.Skin{
|
|
|
|
UserId: identityId,
|
|
|
|
Username: username,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
skinId, _ := strconv.Atoi(req.Form.Get("skinId"))
|
|
|
|
is18, _ := strconv.ParseBool(req.Form.Get("is1_8"))
|
|
|
|
isSlim, _ := strconv.ParseBool(req.Form.Get("isSlim"))
|
|
|
|
|
|
|
|
record.Uuid = req.Form.Get("uuid")
|
|
|
|
record.SkinId = skinId
|
|
|
|
record.Is1_8 = is18
|
|
|
|
record.IsSlim = isSlim
|
|
|
|
record.Url = req.Form.Get("url")
|
|
|
|
record.MojangTextures = req.Form.Get("mojangTextures")
|
|
|
|
record.MojangSignature = req.Form.Get("mojangSignature")
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
err = ctx.SkinsRepo.SaveSkin(record)
|
2020-04-19 05:01:09 +05:30
|
|
|
if err != nil {
|
2021-02-26 07:15:45 +05:30
|
|
|
panic(err)
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
resp.WriteHeader(http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Api) deleteSkinByUserIdHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
id, _ := strconv.Atoi(mux.Vars(req)["id"])
|
2020-04-21 00:48:27 +05:30
|
|
|
skin, err := ctx.SkinsRepo.FindSkinByUserId(id)
|
2020-04-19 05:01:09 +05:30
|
|
|
ctx.deleteSkin(skin, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Api) deleteSkinByUsernameHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
username := mux.Vars(req)["username"]
|
2020-04-21 00:48:27 +05:30
|
|
|
skin, err := ctx.SkinsRepo.FindSkinByUsername(username)
|
2020-04-19 05:01:09 +05:30
|
|
|
ctx.deleteSkin(skin, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Api) deleteSkin(skin *model.Skin, err error, resp http.ResponseWriter) {
|
|
|
|
if err != nil {
|
2021-02-26 07:15:45 +05:30
|
|
|
panic(err)
|
2020-04-20 17:46:15 +05:30
|
|
|
}
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2020-04-20 17:46:15 +05:30
|
|
|
if skin == nil {
|
|
|
|
apiNotFound(resp, "Cannot find record for the requested identifier")
|
2020-04-19 05:01:09 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
err = ctx.SkinsRepo.RemoveSkinByUserId(skin.UserId)
|
2020-04-19 05:01:09 +05:30
|
|
|
if err != nil {
|
2021-02-26 07:15:45 +05:30
|
|
|
panic(err)
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
resp.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Api) findIdentityOrCleanup(identityId int, username string) (*model.Skin, error) {
|
2020-04-21 00:48:27 +05:30
|
|
|
record, err := ctx.SkinsRepo.FindSkinByUserId(identityId)
|
2020-04-19 05:01:09 +05:30
|
|
|
if err != nil {
|
2020-04-20 17:46:15 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2020-04-20 17:46:15 +05:30
|
|
|
if record != nil {
|
|
|
|
// The username may have changed in the external database,
|
|
|
|
// so we need to remove the old association
|
|
|
|
if record.Username != username {
|
2020-04-21 00:48:27 +05:30
|
|
|
_ = ctx.SkinsRepo.RemoveSkinByUserId(identityId)
|
2020-04-20 17:46:15 +05:30
|
|
|
record.Username = username
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
2020-04-20 17:46:15 +05:30
|
|
|
|
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the requested id was not found, then username was reassigned to another user
|
|
|
|
// who has not uploaded his data to Chrly yet
|
2020-04-21 00:48:27 +05:30
|
|
|
record, err = ctx.SkinsRepo.FindSkinByUsername(username)
|
2020-04-20 17:46:15 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the target username does exist, clear it as it will be reassigned to the new user
|
|
|
|
if record != nil {
|
2020-04-21 00:48:27 +05:30
|
|
|
_ = ctx.SkinsRepo.RemoveSkinByUsername(username)
|
2020-04-20 17:46:15 +05:30
|
|
|
record.UserId = identityId
|
|
|
|
|
|
|
|
return record, nil
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2020-04-20 17:46:15 +05:30
|
|
|
return nil, nil
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func validatePostSkinRequest(request *http.Request) map[string][]string {
|
2023-12-22 06:26:02 +05:30
|
|
|
_ = request.ParseForm()
|
2020-04-19 05:01:09 +05:30
|
|
|
|
|
|
|
validationRules := govalidator.MapData{
|
2023-12-22 06:26:02 +05:30
|
|
|
"identityId": {"required", "numeric", "min:1"},
|
|
|
|
"username": {"required"},
|
|
|
|
"uuid": {"required", "uuid_any"},
|
|
|
|
"skinId": {"required", "numeric"},
|
|
|
|
"url": {},
|
|
|
|
"is1_8": {"bool"},
|
|
|
|
"isSlim": {"bool"},
|
|
|
|
"mojangTextures": {},
|
|
|
|
"mojangSignature": {},
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
url := request.Form.Get("url")
|
2023-12-22 06:26:02 +05:30
|
|
|
if url == "" {
|
|
|
|
validationRules["skinId"] = append(validationRules["skinId"], "numeric_between:0,0")
|
|
|
|
} else {
|
|
|
|
validationRules["url"] = append(validationRules["url"], "url")
|
|
|
|
validationRules["skinId"] = append(validationRules["skinId"], "numeric_between:1,")
|
2020-04-19 05:01:09 +05:30
|
|
|
validationRules["is1_8"] = append(validationRules["is1_8"], "required")
|
|
|
|
validationRules["isSlim"] = append(validationRules["isSlim"], "required")
|
|
|
|
}
|
|
|
|
|
|
|
|
mojangTextures := request.Form.Get("mojangTextures")
|
|
|
|
if mojangTextures != "" {
|
2023-12-22 06:26:02 +05:30
|
|
|
validationRules["mojangSignature"] = append(validationRules["mojangSignature"], "required")
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
validator := govalidator.New(govalidator.Options{
|
|
|
|
Request: request,
|
|
|
|
Rules: validationRules,
|
|
|
|
RequiredDefault: false,
|
|
|
|
})
|
|
|
|
validationResults := validator.Validate()
|
|
|
|
|
|
|
|
if len(validationResults) != 0 {
|
|
|
|
return validationResults
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|