2018-03-02 21:42:34 +01:00
|
|
|
package upload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
|
|
|
)
|
|
|
|
|
2018-03-07 14:00:14 +01:00
|
|
|
// Avatar handles the avatar upload.
|
2018-03-02 21:42:34 +01:00
|
|
|
func Avatar(ctx *aero.Context) string {
|
|
|
|
user := utils.GetUser(ctx)
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
return ctx.Error(http.StatusUnauthorized, "Not logged in", nil)
|
|
|
|
}
|
|
|
|
|
2018-03-03 16:03:18 +01:00
|
|
|
// Retrieve file from post body
|
2018-03-02 21:42:34 +01:00
|
|
|
data, err := ctx.Request().Body().Bytes()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.StatusInternalServerError, "Reading request body failed", err)
|
|
|
|
}
|
|
|
|
|
2018-03-03 16:03:18 +01:00
|
|
|
// Set avatar file
|
|
|
|
err = user.SetAvatarBytes(data)
|
2018-03-02 21:42:34 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2018-03-03 16:03:18 +01:00
|
|
|
return ctx.Error(http.StatusInternalServerError, "Invalid image format", err)
|
2018-03-02 21:42:34 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 16:03:18 +01:00
|
|
|
// Save avatar information
|
|
|
|
user.Save()
|
|
|
|
|
2018-03-05 17:49:24 +01:00
|
|
|
return user.AvatarLink("small")
|
2018-03-02 21:42:34 +01:00
|
|
|
}
|