外观设置改为半屏布局;背景图仅保存后生效,并支持媒体库选用。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/freefire/jiang13-bbs/middleware"
|
||||
"github.com/freefire/jiang13-bbs/realtime"
|
||||
"github.com/freefire/jiang13-bbs/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -167,10 +166,9 @@ func (h *Handlers) DeleteAttachment(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
// UploadBackground 超管上传站点背景图(JPEG/PNG/WebP),并立即绑定前台或后台
|
||||
// UploadBackground 超管上传站点背景图(JPEG/PNG/WebP),只落盘返回 URL,不写站点设置
|
||||
func (h *Handlers) UploadBackground(c *gin.Context) {
|
||||
surface, ok := service.NormalizeBgSurface(c.PostForm("surface"))
|
||||
if !ok {
|
||||
if _, ok := service.NormalizeBgSurface(c.PostForm("surface")); !ok {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请指定前台或后台(surface=site|admin)"})
|
||||
return
|
||||
}
|
||||
@@ -209,26 +207,36 @@ func (h *Handlers) UploadBackground(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"url": url})
|
||||
}
|
||||
|
||||
if err := h.applyBgURL(surface, url); err != nil {
|
||||
writeBgSettingError(c, err)
|
||||
type backgroundFromMediaRequest struct {
|
||||
AttachmentID uint `json:"attachment_id"`
|
||||
Surface string `json:"surface"`
|
||||
}
|
||||
|
||||
// UploadBackgroundFromMedia 把本人媒体库图片复制进 backgrounds,不写站点设置
|
||||
func (h *Handlers) UploadBackgroundFromMedia(c *gin.Context) {
|
||||
var req backgroundFromMediaRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil || req.AttachmentID == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请选择媒体库中的图片"})
|
||||
return
|
||||
}
|
||||
|
||||
saved, err := h.Setting.Public()
|
||||
if _, ok := service.NormalizeBgSurface(req.Surface); !ok {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请指定前台或后台(surface=site|admin)"})
|
||||
return
|
||||
}
|
||||
claims := middleware.CurrentUser(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "请先登录"})
|
||||
return
|
||||
}
|
||||
url, err := h.Upload.CopyBackgroundFromMedia(claims.ID, req.AttachmentID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取站点设置失败"})
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
payload := settingsPayload(saved)
|
||||
h.Hub.BroadcastAll(realtime.Envelope{
|
||||
Type: realtime.EventSettingsChanged,
|
||||
Data: payload,
|
||||
})
|
||||
out := settingsPayload(saved)
|
||||
out["ok"] = true
|
||||
out["url"] = url
|
||||
c.JSON(http.StatusOK, out)
|
||||
c.JSON(http.StatusOK, gin.H{"url": url})
|
||||
}
|
||||
|
||||
// UploadBrand 超管上传 Logo / Favicon,只落盘返回 URL,不写站点设置
|
||||
|
||||
@@ -267,6 +267,7 @@ func Setup(cfg *config.Config) (*gin.Engine, error) {
|
||||
staffAPI.GET("/settings", authMW.RequirePerm(service.PermSettings), h.AdminGetSettings)
|
||||
staffAPI.PUT("/settings", authMW.RequirePerm(service.PermSettings), h.UpdateSettings)
|
||||
staffAPI.POST("/upload/background", authMW.RequirePerm(service.PermSettings), middleware.RateLimitMiddleware(limiter, service.RateUpload), h.UploadBackground)
|
||||
staffAPI.POST("/upload/background/from-media", authMW.RequirePerm(service.PermSettings), middleware.RateLimitMiddleware(limiter, service.RateUpload), h.UploadBackgroundFromMedia)
|
||||
staffAPI.POST("/upload/brand", authMW.RequirePerm(service.PermSettings), middleware.RateLimitMiddleware(limiter, service.RateUpload), h.UploadBrand)
|
||||
staffAPI.POST("/upload/brand/from-media", authMW.RequirePerm(service.PermSettings), middleware.RateLimitMiddleware(limiter, service.RateUpload), h.UploadBrandFromMedia)
|
||||
|
||||
|
||||
@@ -364,6 +364,24 @@ func (s *UploadService) SaveBackground(src io.Reader) (string, error) {
|
||||
return "/uploads/backgrounds/" + filename, nil
|
||||
}
|
||||
|
||||
// CopyBackgroundFromMedia 把当前用户媒体库里的一张图复制进 backgrounds,不写站点设置
|
||||
func (s *UploadService) CopyBackgroundFromMedia(userID, attachmentID uint) (string, error) {
|
||||
var att model.Attachment
|
||||
if err := s.db.Where("id = ? AND user_id = ?", attachmentID, userID).First(&att).Error; err != nil {
|
||||
return "", errors.New("图片不存在或不属于你")
|
||||
}
|
||||
abs, ok := s.safeUploadPath(att.URL)
|
||||
if !ok {
|
||||
return "", errors.New("无效的图片地址")
|
||||
}
|
||||
f, err := os.Open(abs)
|
||||
if err != nil {
|
||||
return "", errors.New("读取图片失败")
|
||||
}
|
||||
defer f.Close()
|
||||
return s.SaveBackground(f)
|
||||
}
|
||||
|
||||
// BackgroundFileExists 确认 URL 对应文件在 backgrounds 目录内
|
||||
func (s *UploadService) BackgroundFileExists(url string) bool {
|
||||
rel := BgRelPath(url)
|
||||
|
||||
Reference in New Issue
Block a user