fix: golangci-lint 零告警通过 (57→0) + gofumpt/goimports 全量格式化
## CI 修复 (P0/P1) P0 — 编译阻塞: - interfaces.go: postUseCase ISP 接口移除不用的 Create/Update/Delete P1 — 必须修复: - ST1000: 为 13 个包添加包注释 (common/config/model/service/...) - ST1005: redis_store.go 全部错误消息改为小写开头 - errcheck (19处): defer Close()→闭包忽略, notifier.Create→_=, r.Run→检查error - errorlint (9处): switch-on-error→errors.Is 链, ==→errors.Is - ST1020/ST1022: 导出符号注释以符号名开头 P2 — 安全评审: - gosec (12处): G203/G301/G304/G306 添加 nolint 注释并附理由 P3 — 清理: - unused: 移除 hasUnicode/energyStore/current/energyAdminUseCase - gofumpt + goimports 全量格式化 (35+ 文件)
This commit is contained in:
@ -22,8 +22,8 @@ import (
|
||||
|
||||
// 头像处理常量
|
||||
const (
|
||||
avatarMaxFileSize = 5 * 1024 * 1024 // 5MB
|
||||
avatarMinWidth = 128 // 最小宽高,防止马赛克图被拉升
|
||||
avatarMaxFileSize = 5 * 1024 * 1024 // 5MB
|
||||
avatarMinWidth = 128 // 最小宽高,防止马赛克图被拉升
|
||||
avatarMinHeight = 128
|
||||
avatarMaxWidth = 3840
|
||||
avatarMaxHeight = 3840
|
||||
@ -97,7 +97,7 @@ func (s *AvatarService) ProcessImage(userID uint, file io.Reader, contentType st
|
||||
img, _, err = image.Decode(bytes.NewReader(data))
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("图片解码失败:%v", err)
|
||||
return "", fmt.Errorf("图片解码失败:%w", err)
|
||||
}
|
||||
bounds := img.Bounds()
|
||||
w, h := bounds.Dx(), bounds.Dy()
|
||||
@ -129,11 +129,11 @@ func (s *AvatarService) ProcessImage(userID uint, file io.Reader, contentType st
|
||||
// 6. 编码为 WebP,目标 ≤100KB
|
||||
webpData, err := encodeWebP(resized, avatarTargetKB*1024)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("图片编码失败:%v", err)
|
||||
return "", fmt.Errorf("图片编码失败:%w", err)
|
||||
}
|
||||
|
||||
// 7. 确保存储目录存在
|
||||
if err := os.MkdirAll(s.storageDir, 0755); err != nil {
|
||||
if err := os.MkdirAll(s.storageDir, 0o755); err != nil { //nolint:gosec // 头像存储目录标准权限
|
||||
return "", fmt.Errorf("创建存储目录失败")
|
||||
}
|
||||
|
||||
@ -143,11 +143,11 @@ func (s *AvatarService) ProcessImage(userID uint, file io.Reader, contentType st
|
||||
filename := fmt.Sprintf("%d_%d.webp", userID, ts)
|
||||
tmpPath := filepath.Join(s.storageDir, filename+".tmp")
|
||||
finalPath := filepath.Join(s.storageDir, filename)
|
||||
if err := os.WriteFile(tmpPath, webpData, 0644); err != nil {
|
||||
if err := os.WriteFile(tmpPath, webpData, 0o644); err != nil { //nolint:gosec // 上传头像标准权限
|
||||
return "", fmt.Errorf("写入临时文件失败")
|
||||
}
|
||||
if err := os.Rename(tmpPath, finalPath); err != nil {
|
||||
os.Remove(tmpPath)
|
||||
_ = os.Remove(tmpPath)
|
||||
return "", fmt.Errorf("保存文件失败")
|
||||
}
|
||||
|
||||
@ -163,7 +163,7 @@ func (s *AvatarService) CleanOldAvatars(userID uint) {
|
||||
}
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() && strings.HasPrefix(e.Name(), prefix) && strings.HasSuffix(e.Name(), ".webp") {
|
||||
os.Remove(filepath.Join(s.storageDir, e.Name()))
|
||||
_ = os.Remove(filepath.Join(s.storageDir, e.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -242,4 +242,3 @@ func encodeWebP(img image.Image, targetBytes int) ([]byte, error) {
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user