a little further
This commit is contained in:
parent
eaa7cc8547
commit
3d7bb77eab
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
const MaxAvatarSize = 1024 * 1024 * 2 // 2MB
|
const MaxAvatarSize = 1024 * 1024 * 2 // 2MB
|
||||||
const AvatarPath = "avatars/"
|
const AvatarPath = "avatars/"
|
||||||
|
const AvatarPrefix = "/uploads/avatars/"
|
||||||
|
|
||||||
func GetProfileContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
|
func GetProfileContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
|
||||||
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
|
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
|
||||||
|
@ -23,7 +24,7 @@ func GetProfileContinuation(context *types.RequestContext, req *http.Request, re
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateProfileContinuation(fileAdapter files.FilesAdapter, maxAvatarSize int, avatarPath string) func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
|
func UpdateProfileContinuation(fileAdapter files.FilesAdapter, maxAvatarSize int, avatarPath string, avatarPrefix string) func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
|
||||||
return func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
|
return func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
|
||||||
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
|
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
|
||||||
formErrors := types.FormError{
|
formErrors := types.FormError{
|
||||||
|
@ -39,6 +40,9 @@ func UpdateProfileContinuation(fileAdapter files.FilesAdapter, maxAvatarSize int
|
||||||
|
|
||||||
if len(formErrors.Errors) == 0 {
|
if len(formErrors.Errors) == 0 {
|
||||||
file, _, err := req.FormFile("avatar")
|
file, _, err := req.FormFile("avatar")
|
||||||
|
if file == nil {
|
||||||
|
formErrors.Errors = append(formErrors.Errors, "avatar required")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
formErrors.Errors = append(formErrors.Errors, "error uploading avatar")
|
formErrors.Errors = append(formErrors.Errors, "error uploading avatar")
|
||||||
} else {
|
} else {
|
||||||
|
@ -52,14 +56,24 @@ func UpdateProfileContinuation(fileAdapter files.FilesAdapter, maxAvatarSize int
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(formErrors.Errors) == 0 {
|
bio := req.FormValue("bio")
|
||||||
bio := req.FormValue("bio")
|
location := req.FormValue("location")
|
||||||
location := req.FormValue("location")
|
website := req.FormValue("website")
|
||||||
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(formErrors.Errors) == 0 {
|
||||||
context.User.Bio = bio
|
context.User.Bio = bio
|
||||||
context.User.Location = location
|
context.User.Location = location
|
||||||
context.User.Website = website
|
context.User.Website = website
|
||||||
|
context.User.Avatar = avatarPrefix + context.User.ID
|
||||||
|
|
||||||
_, err = database.SaveUser(context.DBConn, context.User)
|
_, err = database.SaveUser(context.DBConn, context.User)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -126,7 +126,7 @@ func MakeServer(argv *args.Arguments, dbConn *sql.DB) *http.Server {
|
||||||
|
|
||||||
mux.HandleFunc("POST /profile", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("POST /profile", func(w http.ResponseWriter, r *http.Request) {
|
||||||
requestContext := makeRequestContext()
|
requestContext := makeRequestContext()
|
||||||
LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(profiles.UpdateProfileContinuation(uploadAdapter, profiles.MaxAvatarSize, profiles.AvatarPath), auth.GoLoginContinuation)(profiles.GetProfileContinuation, FailurePassingContinuation)(template.TemplateContinuation("profile.html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
|
LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(profiles.UpdateProfileContinuation(uploadAdapter, profiles.MaxAvatarSize, profiles.AvatarPath, profiles.AvatarPrefix), auth.GoLoginContinuation)(profiles.GetProfileContinuation, FailurePassingContinuation)(template.TemplateContinuation("profile.html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
|
||||||
})
|
})
|
||||||
|
|
||||||
mux.HandleFunc("GET /dns", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("GET /dns", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -147,7 +147,7 @@ func MigrateProfiles(dbConn *sql.DB) (*sql.DB, error) {
|
||||||
columns["bio"] = "a computer hater"
|
columns["bio"] = "a computer hater"
|
||||||
columns["location"] = "earth"
|
columns["location"] = "earth"
|
||||||
columns["website"] = "https://hatecomputers.club"
|
columns["website"] = "https://hatecomputers.club"
|
||||||
columns["avatar"] = "/files/avatars/default.png"
|
columns["avatar"] = "/static/img/default-avatar.png"
|
||||||
|
|
||||||
for column, defaultValue := range columns {
|
for column, defaultValue := range columns {
|
||||||
if userColumns[column] {
|
if userColumns[column] {
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 103 KiB |
|
@ -3,5 +3,6 @@
|
||||||
<p>current peeps:</p>
|
<p>current peeps:</p>
|
||||||
{{ range $user := .Users }}
|
{{ range $user := .Users }}
|
||||||
<p>{{ $user.Username }}</p>
|
<p>{{ $user.Username }}</p>
|
||||||
|
<img src="{{ $user.Avatar }}" alt="{{ $user.Username }}" />
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<br>
|
<br>
|
||||||
<form action="/profile" method="POST" class="form" enctype="multipart/form-data">
|
<form action="/profile" method="POST" class="form" enctype="multipart/form-data">
|
||||||
<label for="type">avatar.</label>
|
<label for="type">avatar.</label>
|
||||||
<input type="file" name="avatar">
|
<input type="file" name="avatar" required>
|
||||||
|
|
||||||
<label for="type">location.</label>
|
<label for="type">location.</label>
|
||||||
<input type="text" name="location" value="{{ .Profile.Location }}">
|
<input type="text" name="location" value="{{ .Profile.Location }}">
|
||||||
|
|
Loading…
Reference in New Issue