49 lines
1.1 KiB
Go
Raw Normal View History

2018-11-22 01:27:53 +00:00
package upload
import (
"net/http"
"github.com/aerogo/aero"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-11-22 01:27:53 +00:00
"github.com/animenotifier/notify.moe/utils"
)
// GroupImage handles the group image upload.
2019-06-01 04:55:49 +00:00
func GroupImage(ctx aero.Context) error {
2018-11-22 01:27:53 +00:00
user := utils.GetUser(ctx)
groupID := ctx.Get("id")
if user == nil || (user.Role != "editor" && user.Role != "admin") {
return ctx.Error(http.StatusUnauthorized, "Not authorized")
}
group, err := arn.GetGroup(groupID)
if err != nil {
return ctx.Error(http.StatusNotFound, "Group 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 group image file
err = group.SetImageBytes(data)
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Invalid image format", err)
}
// Save image information
group.Save()
// Write log entry
logEntry := arn.NewEditLogEntry(user.ID, "edit", "Group", group.ID, "Image", "", "")
logEntry.Save()
2019-06-01 04:55:49 +00:00
return nil
2018-11-22 01:27:53 +00:00
}