diff --git a/handlers/post.go b/handlers/post.go index e92cb72..a77a2c3 100644 --- a/handlers/post.go +++ b/handlers/post.go @@ -264,7 +264,10 @@ func UpdatePost(c *gin.Context) { }) tags = append(tags, tag) } - database.DB.Model(&post).Association("Tags").Replace(tags) + if err := database.DB.Model(&post).Association("Tags").Replace(tags); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "更新文章标签失败"}) + return + } } if err := database.DB.Model(&post).Updates(updates).Error; err != nil { diff --git a/handlers/theme.go b/handlers/theme.go index 1009230..8cac482 100644 --- a/handlers/theme.go +++ b/handlers/theme.go @@ -147,7 +147,7 @@ func CreateTheme(c *gin.Context) { } // 创建主题目录 - if err := os.MkdirAll(themePath, 0755); err != nil { + if err := os.MkdirAll(themePath, 0700); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": "创建主题目录失败", }) @@ -411,7 +411,7 @@ func SaveThemeFile(c *gin.Context) { } // 保存文件 - if err := os.WriteFile(filePath, []byte(req.Content), 0644); err != nil { + if err := os.WriteFile(filePath, []byte(req.Content), 0600); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": "保存文件失败", }) diff --git a/handlers/view.go b/handlers/view.go index 84b7c4e..745b6fe 100644 --- a/handlers/view.go +++ b/handlers/view.go @@ -1,7 +1,6 @@ package handlers import ( - "html/template" "log/slog" "math" "net/http" @@ -11,7 +10,6 @@ import ( "goblog/config" "goblog/database" "goblog/models" - "goblog/utils" "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -27,21 +25,6 @@ func loadSidebarData() (categories []models.Category, tags []models.Tag, postCou return } -func templateFuncs() template.FuncMap { - return template.FuncMap{ - "add": func(a, b int) int { - return a + b - }, - "sub": func(a, b int) int { - return a - b - }, - "html": func(s string) template.HTML { - return template.HTML(s) - }, - "markdown": utils.RenderMarkdown, - } -} - // 首页 type IndexData struct { Title string @@ -79,7 +62,7 @@ func IndexView(c *gin.Context) { Where("status = ?", "published") userId, exists := c.Get("userID") - userId, exists = 1, true + if exists { db = db.Or("author_id = ?", userId) } diff --git a/middleware/auth.go b/middleware/auth.go index 14872bd..7a92bf4 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -21,7 +21,7 @@ func JWTAuth() gin.HandlerFunc { } parts := strings.SplitN(authHeader, " ", 2) - if !(len(parts) == 2 && parts[0] == "Bearer") { + if len(parts) != 2 || parts[0] != "Bearer" { c.JSON(http.StatusUnauthorized, gin.H{"error": "认证格式错误"}) c.Abort() return diff --git a/utils/templateRenderer.go b/utils/templateRenderer.go index 30fdbb6..62e19ce 100644 --- a/utils/templateRenderer.go +++ b/utils/templateRenderer.go @@ -118,10 +118,11 @@ func (r *TemplateRenderer) reload(theme string) { for _, pageName := range pages { // 加载 base.html、对应页面模板以及所有公共局部模板 - allFiles := []string{ - templateDir + "/base.html", - templateDir + "/" + pageName + ".html", - } + allFiles := make([]string, 0, 2+len(partials)) + allFiles = append(allFiles, + templateDir+"/base.html", + templateDir+"/"+pageName+".html", + ) allFiles = append(allFiles, partials...) // 解析模板文件