49 lines
1.1 KiB
Go
Raw Normal View History

package upload
import (
"net/http"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
"github.com/aerogo/aero"
)
// CharacterImage handles the character image upload.
2019-06-01 04:55:49 +00:00
func CharacterImage(ctx aero.Context) error {
2019-11-17 07:59:34 +00:00
user := arn.GetUserFromContext(ctx)
characterID := ctx.Get("id")
if user == nil || (user.Role != "editor" && user.Role != "admin") {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusUnauthorized, "Not authorized")
}
character, err := arn.GetCharacter(characterID)
if err != nil {
return ctx.Error(http.StatusNotFound, "Character not found", err)
}
// 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 character image file
err = character.SetImageBytes(data)
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Invalid image format", err)
}
// Save image information
character.Save()
// Write log entry
logEntry := arn.NewEditLogEntry(user.ID, "edit", "Character", character.ID, "Image", "", "")
logEntry.Save()
2019-06-01 04:55:49 +00:00
return nil
}