Achieve consistency by using a single Image type
This commit is contained in:
49
pages/upload/character-image.go
Normal file
49
pages/upload/character-image.go
Normal file
@ -0,0 +1,49 @@
|
||||
package upload
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/animenotifier/notify.moe/arn"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
// CharacterImage handles the character image upload.
|
||||
func CharacterImage(ctx aero.Context) error {
|
||||
user := utils.GetUser(ctx)
|
||||
characterID := ctx.Get("id")
|
||||
|
||||
if user == nil || (user.Role != "editor" && user.Role != "admin") {
|
||||
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()
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user