37 lines
731 B
Go
Raw Normal View History

2018-03-02 20:42:34 +00:00
package upload
import (
"net/http"
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/utils"
)
// UserImage handles the avatar upload.
func UserImage(ctx aero.Context) error {
2018-03-02 20:42:34 +00:00
user := utils.GetUser(ctx)
if user == nil {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusUnauthorized, "Not logged in")
2018-03-02 20:42:34 +00:00
}
2018-03-03 15:03:18 +00:00
// Retrieve file from post body
2018-03-02 20:42:34 +00:00
data, err := ctx.Request().Body().Bytes()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Reading request body failed", err)
}
2018-03-03 15:03:18 +00:00
// Set avatar file
err = user.SetImageBytes(data)
2018-03-02 20:42:34 +00:00
if err != nil {
2018-03-03 15:03:18 +00:00
return ctx.Error(http.StatusInternalServerError, "Invalid image format", err)
2018-03-02 20:42:34 +00:00
}
2018-03-03 15:03:18 +00:00
// Save avatar information
user.Save()
2019-06-01 04:55:49 +00:00
return ctx.Text(user.AvatarLink("small"))
2018-03-02 20:42:34 +00:00
}