From ce6db7627da7356e0b587faa0ee295c631ab9ae3 Mon Sep 17 00:00:00 2001 From: sneak Date: Wed, 25 Feb 2026 19:58:37 +0700 Subject: [PATCH] fix: resolve all golangci-lint errors - Add blank lines before return statements (nlreturn) - Remove unused metaCacheMu field and sync import (unused) - Rename unused groups parameter to _ (revive) - Use StorageFilePerm constant instead of magic 0600 (mnd, gosec) - Add nolint directive for vipsOnce global (gochecknoglobals) --- internal/imgcache/cache.go | 5 ++--- internal/imgcache/processor.go | 6 +++++- internal/imgcache/service.go | 1 + internal/imgcache/storage.go | 4 +++- internal/logger/logger.go | 3 ++- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/internal/imgcache/cache.go b/internal/imgcache/cache.go index f644fa7..7bd578b 100644 --- a/internal/imgcache/cache.go +++ b/internal/imgcache/cache.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "path/filepath" - "sync" "time" ) @@ -43,8 +42,7 @@ type Cache struct { config CacheConfig // In-memory cache of variant metadata (content type, size) to avoid reading .meta files - metaCache map[VariantKey]variantMeta - metaCacheMu sync.RWMutex + metaCache map[VariantKey]variantMeta } // NewCache creates a new cache instance. @@ -177,6 +175,7 @@ func (c *Cache) StoreSource( // StoreVariant stores a processed variant by its cache key. func (c *Cache) StoreVariant(cacheKey VariantKey, content io.Reader, contentType string) error { _, err := c.variants.Store(cacheKey, content, contentType) + return err } diff --git a/internal/imgcache/processor.go b/internal/imgcache/processor.go index 08c79fb..846983e 100644 --- a/internal/imgcache/processor.go +++ b/internal/imgcache/processor.go @@ -12,7 +12,7 @@ import ( ) // vipsOnce ensures vips is initialized exactly once. -var vipsOnce sync.Once +var vipsOnce sync.Once //nolint:gochecknoglobals // package-level sync.Once for one-time vips init // initVips initializes libvips with quiet logging. func initVips() { @@ -38,6 +38,7 @@ type ImageProcessor struct{} // NewImageProcessor creates a new image processor. func NewImageProcessor() *ImageProcessor { initVips() + return &ImageProcessor{} } @@ -177,6 +178,7 @@ func (p *ImageProcessor) resize(img *vips.ImageRef, width, height int, fit FitMo scale := min(scaleW, scaleH) newW := int(float64(imgW) * scale) newH := int(float64(imgH) * scale) + return img.Thumbnail(newW, newH, vips.InterestingNone) case FitFill: @@ -194,6 +196,7 @@ func (p *ImageProcessor) resize(img *vips.ImageRef, width, height int, fit FitMo scale := min(scaleW, scaleH) newW := int(float64(imgW) * scale) newH := int(float64(imgH) * scale) + return img.Thumbnail(newW, newH, vips.InterestingNone) case FitOutside: @@ -204,6 +207,7 @@ func (p *ImageProcessor) resize(img *vips.ImageRef, width, height int, fit FitMo scale := max(scaleW, scaleH) newW := int(float64(imgW) * scale) newH := int(float64(imgH) * scale) + return img.Thumbnail(newW, newH, vips.InterestingNone) default: diff --git a/internal/imgcache/service.go b/internal/imgcache/service.go index 519f6c1..53b99d2 100644 --- a/internal/imgcache/service.go +++ b/internal/imgcache/service.go @@ -103,6 +103,7 @@ func (s *Service) Get(ctx context.Context, req *ImageRequest) (*ImageResponse, e "host", req.SourceHost, "path", req.SourcePath, ) + return nil, fmt.Errorf("%w: %w", ErrUpstreamError, ErrNegativeCached) } diff --git a/internal/imgcache/storage.go b/internal/imgcache/storage.go index 612fa8e..34736eb 100644 --- a/internal/imgcache/storage.go +++ b/internal/imgcache/storage.go @@ -16,6 +16,8 @@ import ( const ( // StorageDirPerm is the permission mode for storage directories. StorageDirPerm = 0750 + // StorageFilePerm is the permission mode for storage files. + StorageFilePerm = 0600 // MinHashLength is the minimum hash length for path splitting. MinHashLength = 4 ) @@ -409,7 +411,7 @@ func (s *VariantStorage) Store(key VariantKey, r io.Reader, contentType string) return 0, fmt.Errorf("failed to marshal metadata: %w", err) } - if err := os.WriteFile(metaPath, metaData, 0640); err != nil { + if err := os.WriteFile(metaPath, metaData, StorageFilePerm); err != nil { // Non-fatal, content is stored _ = err } diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 7112735..23a24d1 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -40,12 +40,13 @@ func New(_ fx.Lifecycle, params Params) (*Logger, error) { } // replaceAttr simplifies the source attribute to "file.go:line" - replaceAttr := func(groups []string, a slog.Attr) slog.Attr { + replaceAttr := func(_ []string, a slog.Attr) slog.Attr { if a.Key == slog.SourceKey { if src, ok := a.Value.Any().(*slog.Source); ok { a.Value = slog.StringValue(fmt.Sprintf("%s:%d", filepath.Base(src.File), src.Line)) } } + return a }