37 lines
738 B
Go

package upload
import (
"net/http"
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/arn"
)
// UserImage handles the avatar upload.
func UserImage(ctx aero.Context) error {
user := arn.GetUserFromContext(ctx)
if user == nil {
return ctx.Error(http.StatusUnauthorized, "Not logged in")
}
// Retrieve file from post body
data, err := ctx.Request().Body().Bytes()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Reading request body failed", err)
}
// Set avatar file
err = user.SetImageBytes(data)
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Invalid image format", err)
}
// Save avatar information
user.Save()
return ctx.Text(user.AvatarLink("small"))
}