101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package themes_handler
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"autolibrary.top/webserver/backend/models/themes_model"
|
|
"autolibrary.top/webserver/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func parseThemeID(c *gin.Context) (uint, error) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
if err != nil || id == 0 {
|
|
return 0, strconv.ErrSyntax
|
|
}
|
|
return uint(id), nil
|
|
}
|
|
|
|
func (h *Handler) PutThemeByID(c *gin.Context) {
|
|
theme, ok := h.ownTheme(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var update struct {
|
|
Name string `json:"name"`
|
|
Brief string `json:"brief"`
|
|
Description string `json:"description"`
|
|
NeedTheme string `json:"need_theme"`
|
|
Tags []string `json:"tags"`
|
|
Version string `json:"version"`
|
|
CompatVersion string `json:"compat_version"`
|
|
PreviewURL string `json:"preview_url"`
|
|
}
|
|
if err := c.ShouldBindJSON(&update); err != nil {
|
|
fail(c, 400, "Invalid theme metadata")
|
|
return
|
|
}
|
|
if update.Name != "" && strings.TrimSpace(update.Name) != theme.Name {
|
|
var count int64
|
|
if err := h.DB.Model(&themes_model.Theme{}).Where("user_id = ? AND name = ? AND id != ?", theme.UserID, strings.TrimSpace(update.Name), theme.ID).Count(&count).Error; err != nil {
|
|
fail(c, 500, "Unable to check theme name")
|
|
return
|
|
}
|
|
if count > 0 {
|
|
fail(c, 409, "You already have a theme with this name")
|
|
return
|
|
}
|
|
theme.Name = strings.TrimSpace(update.Name)
|
|
}
|
|
if update.NeedTheme != "" && update.NeedTheme != "dark" && update.NeedTheme != "light" && update.NeedTheme != "both" {
|
|
fail(c, 400, "Invalid theme mode")
|
|
return
|
|
}
|
|
if utf8.RuneCountInString(update.Brief) > 50 || utf8.RuneCountInString(update.Description) > 100 {
|
|
fail(c, 400, "Theme description is too long")
|
|
return
|
|
}
|
|
if strings.TrimSpace(update.Brief) != "" {
|
|
theme.Brief = strings.TrimSpace(update.Brief)
|
|
}
|
|
theme.Description = update.Description
|
|
if update.NeedTheme != "" {
|
|
theme.NeedTheme = update.NeedTheme
|
|
}
|
|
if update.Tags != nil {
|
|
theme.Tags = update.Tags
|
|
}
|
|
if update.Version != "" {
|
|
theme.Version = update.Version
|
|
}
|
|
theme.CompatVersion = update.CompatVersion
|
|
// Preview changes go through the authenticated image upload endpoint. Preserve
|
|
// the legacy field only when it refers to the theme's existing preview.
|
|
if update.PreviewURL != "" && update.PreviewURL != theme.PreviewURL {
|
|
fail(c, 400, "Use the preview upload endpoint to replace an image")
|
|
return
|
|
}
|
|
theme.Status = themes_model.ThemePending
|
|
theme.UpdatedAt = utils.NowInShanghai()
|
|
if err := h.Themes.Save(theme, nil); err != nil {
|
|
fail(c, 500, "Unable to save theme and archive")
|
|
return
|
|
}
|
|
success(c, 200, "Theme updated; review required", theme)
|
|
}
|
|
|
|
func (h *Handler) DeleteThemeByID(c *gin.Context) {
|
|
theme, ok := h.ownTheme(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := h.Themes.Delete(theme); err != nil {
|
|
fail(c, 500, "Unable to delete theme")
|
|
return
|
|
}
|
|
success(c, 200, "Theme deleted", nil)
|
|
}
|