2018-03-16 21:40:38 +00:00
|
|
|
package upload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2018-03-16 21:40:38 +00:00
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AnimeImage handles the anime image upload.
|
2019-06-01 04:55:49 +00:00
|
|
|
func AnimeImage(ctx aero.Context) error {
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2018-03-16 21:40:38 +00:00
|
|
|
animeID := 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")
|
2018-03-16 21:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
anime, err := arn.GetAnime(animeID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.StatusNotFound, "Anime 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 anime image file
|
|
|
|
err = anime.SetImageBytes(data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.StatusInternalServerError, "Invalid image format", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save image information
|
|
|
|
anime.Save()
|
|
|
|
|
2018-03-16 21:53:13 +00:00
|
|
|
// Write log entry
|
|
|
|
logEntry := arn.NewEditLogEntry(user.ID, "edit", "Anime", anime.ID, "Image", "", "")
|
|
|
|
logEntry.Save()
|
|
|
|
|
2019-06-01 04:55:49 +00:00
|
|
|
return nil
|
2018-03-16 21:40:38 +00:00
|
|
|
}
|