hatecomputers.club/api/profiles/profiles.go

98 lines
3.0 KiB
Go
Raw Normal View History

2024-04-08 19:04:19 -04:00
package profiles
import (
"log"
"net/http"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/adapters/files"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/api/types"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/database"
)
const MaxAvatarSize = 1024 * 1024 * 2 // 2MB
const AvatarPath = "avatars/"
2024-04-08 19:15:31 -04:00
const AvatarPrefix = "/uploads/avatars/"
2024-04-08 19:04:19 -04:00
func GetProfileContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
if context.User == nil {
return failure(context, req, resp)
}
(*context.TemplateData)["Profile"] = context.User
return success(context, req, resp)
}
}
2024-04-08 19:15:31 -04:00
func UpdateProfileContinuation(fileAdapter files.FilesAdapter, maxAvatarSize int, avatarPath string, avatarPrefix string) func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
2024-04-08 19:04:19 -04:00
return func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
formErrors := types.FormError{
Errors: []string{},
}
err := req.ParseMultipartForm(int64(maxAvatarSize))
if err != nil {
log.Println(err)
formErrors.Errors = append(formErrors.Errors, "avatar file too large")
}
if len(formErrors.Errors) == 0 {
file, _, err := req.FormFile("avatar")
2024-04-08 19:15:31 -04:00
if file == nil {
formErrors.Errors = append(formErrors.Errors, "avatar required")
}
2024-04-08 19:04:19 -04:00
if err != nil {
formErrors.Errors = append(formErrors.Errors, "error uploading avatar")
} else {
defer file.Close()
_, err = fileAdapter.CreateFile(avatarPath+context.User.ID, file)
if err != nil {
log.Println(err)
formErrors.Errors = append(formErrors.Errors, "error saving avatar")
}
}
}
2024-04-08 19:15:31 -04:00
bio := req.FormValue("bio")
location := req.FormValue("location")
website := req.FormValue("website")
if len(bio) > 128 {
formErrors.Errors = append(formErrors.Errors, "bio too long, keep it to 128")
}
if len(location) > 32 {
formErrors.Errors = append(formErrors.Errors, "location too long, keep it to 32")
}
if len(website) > 64 {
formErrors.Errors = append(formErrors.Errors, "website too long, keep it to 64")
}
2024-04-08 19:04:19 -04:00
2024-04-08 19:15:31 -04:00
if len(formErrors.Errors) == 0 {
2024-04-08 19:04:19 -04:00
context.User.Bio = bio
context.User.Location = location
context.User.Website = website
2024-04-08 19:15:31 -04:00
context.User.Avatar = avatarPrefix + context.User.ID
2024-04-08 19:04:19 -04:00
_, err = database.SaveUser(context.DBConn, context.User)
if err != nil {
formErrors.Errors = append(formErrors.Errors, "error saving profile")
}
}
(*context.TemplateData)["Profile"] = context.User
(*context.TemplateData)["FormError"] = formErrors
if len(formErrors.Errors) > 0 {
log.Println(formErrors.Errors)
resp.WriteHeader(http.StatusBadRequest)
return failure(context, req, resp)
}
return success(context, req, resp)
}
}
}