2018-03-16 21:40:38 +00:00
|
|
|
package upload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AnimeImage handles the anime image upload.
|
|
|
|
func AnimeImage(ctx *aero.Context) string {
|
|
|
|
user := utils.GetUser(ctx)
|
|
|
|
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()
|
|
|
|
|
2018-03-16 21:40:38 +00:00
|
|
|
return "ok"
|
|
|
|
}
|