refactor: extract magic byte detection into internal/magic package #42

Open
clawbot wants to merge 1 commits from refactor/extract-magic-package into main
Collaborator

Summary

Extract magic byte detection and MIME type handling from internal/imgcache/ into a new focused internal/magic/ package.

Part of issue #39

Changes

New package: internal/magic/

Moved the following from internal/imgcache/magic.go:

  • MIMEType type and constants (MIMETypeJPEG, MIMETypePNG, etc.)
  • DetectFormat() — detects image format from magic bytes
  • ValidateMagicBytes() — validates content matches declared MIME type
  • PeekAndValidate() — reads minimum bytes, validates, returns combined reader
  • IsSupportedMIMEType() — checks if a MIME type is supported
  • MIMEToImageFormat() — converts MIME type to ImageFormat
  • ImageFormatToMIME() — converts ImageFormat to MIME string
  • All error sentinels (ErrUnknownFormat, ErrMagicByteMismatch, ErrNotEnoughData)
  • All helper functions (detectSVG, skipBOM, normalizeMIMEType)

The magic package defines its own ImageFormat type and constants to avoid circular imports (imgcachemagic for validation; magic cannot import imgcache).

Updated imports

  • internal/imgcache/service.go: uses magic.ValidateMagicBytes()
  • internal/imgcache/service_test.go: uses magic.DetectFormat() and magic.MIMEToImageFormat()

Naming

  • Clean package-qualified names: magic.DetectFormat(), magic.ValidateMagicBytes(), etc.
  • No stuttering names

Tests

  • Full test suite moved to internal/magic/magic_test.go (all 15 test functions preserved)
  • All existing tests pass unchanged
  • docker build . passes (includes make check: fmt, lint, tests)
## Summary Extract magic byte detection and MIME type handling from `internal/imgcache/` into a new focused `internal/magic/` package. Part of [issue #39](https://git.eeqj.de/sneak/pixa/issues/39) ## Changes ### New package: `internal/magic/` Moved the following from `internal/imgcache/magic.go`: - `MIMEType` type and constants (`MIMETypeJPEG`, `MIMETypePNG`, etc.) - `DetectFormat()` — detects image format from magic bytes - `ValidateMagicBytes()` — validates content matches declared MIME type - `PeekAndValidate()` — reads minimum bytes, validates, returns combined reader - `IsSupportedMIMEType()` — checks if a MIME type is supported - `MIMEToImageFormat()` — converts MIME type to ImageFormat - `ImageFormatToMIME()` — converts ImageFormat to MIME string - All error sentinels (`ErrUnknownFormat`, `ErrMagicByteMismatch`, `ErrNotEnoughData`) - All helper functions (`detectSVG`, `skipBOM`, `normalizeMIMEType`) The magic package defines its own `ImageFormat` type and constants to avoid circular imports (`imgcache` → `magic` for validation; `magic` cannot import `imgcache`). ### Updated imports - `internal/imgcache/service.go`: uses `magic.ValidateMagicBytes()` - `internal/imgcache/service_test.go`: uses `magic.DetectFormat()` and `magic.MIMEToImageFormat()` ### Naming - Clean package-qualified names: `magic.DetectFormat()`, `magic.ValidateMagicBytes()`, etc. - No stuttering names ### Tests - Full test suite moved to `internal/magic/magic_test.go` (all 15 test functions preserved) - All existing tests pass unchanged - `docker build .` passes (includes `make check`: fmt, lint, tests)
clawbot added 1 commit 2026-03-26 01:47:00 +01:00
refactor: extract magic byte detection into internal/magic package
All checks were successful
check / check (push) Successful in 52s
dedc729f37
Move MIMEType, DetectFormat, ValidateMagicBytes, PeekAndValidate,
IsSupportedMIMEType, MIMEToImageFormat, and ImageFormatToMIME from
internal/imgcache into a new internal/magic package.

The magic package defines its own ImageFormat type and constants to
avoid circular imports (imgcache imports magic for validation,
magic cannot import imgcache).

Update import sites in imgcache/service.go and service_test.go.

Part of issue #39.
clawbot added the needs-review label 2026-03-26 01:47:37 +01:00
Author
Collaborator

Review: PASS

Clean extraction of magic byte detection into internal/magic/ package.

Checklist

Check Result
magic.go moved with no logic changes Only package declaration, doc comment, and ImageFormat type added
magic_test.go moved with all tests All 13 test functions preserved, only package declaration changed
ImageFormat constants match originals All 6 values identical to internal/imgcache/imgcache.go
Import sites updated service.gomagic.ValidateMagicBytes(), service_test.gomagic.DetectFormat() / magic.MIMEToImageFormat()
No circular imports internal/magic/ imports only stdlib (bytes, errors, io, strings)
No scope creep Only 4 files changed, only magic extraction
No stuttering names magic.DetectFormat(), magic.ValidateMagicBytes(), etc.
No linter/CI/test config changes .golangci.yml, Makefile, Dockerfile, .gitea/workflows/ untouched
docker build . passes All tests pass, build succeeds

Notes

  • The original ImageFormat type in imgcache.go is correctly preserved (other packages like encurl, handlers, urlparser still reference imgcache.ImageFormat).
  • No stale references to old package-qualified names (imgcache.DetectFormat, etc.) remain anywhere in the codebase.
  • Two minor comment improvements: "our ImageFormat" → "an ImageFormat" — appropriate for the new standalone package context.

Part of issue #39.

## Review: PASS ✅ Clean extraction of magic byte detection into `internal/magic/` package. ### Checklist | Check | Result | |---|---| | `magic.go` moved with no logic changes | ✅ Only package declaration, doc comment, and `ImageFormat` type added | | `magic_test.go` moved with all tests | ✅ All 13 test functions preserved, only package declaration changed | | `ImageFormat` constants match originals | ✅ All 6 values identical to `internal/imgcache/imgcache.go` | | Import sites updated | ✅ `service.go` → `magic.ValidateMagicBytes()`, `service_test.go` → `magic.DetectFormat()` / `magic.MIMEToImageFormat()` | | No circular imports | ✅ `internal/magic/` imports only stdlib (`bytes`, `errors`, `io`, `strings`) | | No scope creep | ✅ Only 4 files changed, only magic extraction | | No stuttering names | ✅ `magic.DetectFormat()`, `magic.ValidateMagicBytes()`, etc. | | No linter/CI/test config changes | ✅ `.golangci.yml`, `Makefile`, `Dockerfile`, `.gitea/workflows/` untouched | | `docker build .` passes | ✅ All tests pass, build succeeds | ### Notes - The original `ImageFormat` type in `imgcache.go` is correctly preserved (other packages like `encurl`, `handlers`, `urlparser` still reference `imgcache.ImageFormat`). - No stale references to old package-qualified names (`imgcache.DetectFormat`, etc.) remain anywhere in the codebase. - Two minor comment improvements: "our ImageFormat" → "an ImageFormat" — appropriate for the new standalone package context. Part of [issue #39](https://git.eeqj.de/sneak/pixa/issues/39).
clawbot added merge-ready and removed needs-review labels 2026-03-26 01:50:44 +01:00
sneak was assigned by clawbot 2026-03-26 01:50:44 +01:00
Some checks are pending
check / check (push) Successful in 52s
Check / check (pull_request)
Required
Some required checks are missing.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin refactor/extract-magic-package:refactor/extract-magic-package
git checkout refactor/extract-magic-package
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/pixa#42