refactor: extract magic byte detection into internal/magic package #42
Reference in New Issue
Block a user
Delete Branch "refactor/extract-magic-package"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Extract magic byte detection and MIME type handling from
internal/imgcache/into a new focusedinternal/magic/package.Part of issue #39
Changes
New package:
internal/magic/Moved the following from
internal/imgcache/magic.go:MIMETypetype and constants (MIMETypeJPEG,MIMETypePNG, etc.)DetectFormat()— detects image format from magic bytesValidateMagicBytes()— validates content matches declared MIME typePeekAndValidate()— reads minimum bytes, validates, returns combined readerIsSupportedMIMEType()— checks if a MIME type is supportedMIMEToImageFormat()— converts MIME type to ImageFormatImageFormatToMIME()— converts ImageFormat to MIME stringErrUnknownFormat,ErrMagicByteMismatch,ErrNotEnoughData)detectSVG,skipBOM,normalizeMIMEType)The magic package defines its own
ImageFormattype and constants to avoid circular imports (imgcache→magicfor validation;magiccannot importimgcache).Updated imports
internal/imgcache/service.go: usesmagic.ValidateMagicBytes()internal/imgcache/service_test.go: usesmagic.DetectFormat()andmagic.MIMEToImageFormat()Naming
magic.DetectFormat(),magic.ValidateMagicBytes(), etc.Tests
internal/magic/magic_test.go(all 15 test functions preserved)docker build .passes (includesmake check: fmt, lint, tests)Review: PASS ✅
Clean extraction of magic byte detection into
internal/magic/package.Checklist
magic.gomoved with no logic changesImageFormattype addedmagic_test.gomoved with all testsImageFormatconstants match originalsinternal/imgcache/imgcache.goservice.go→magic.ValidateMagicBytes(),service_test.go→magic.DetectFormat()/magic.MIMEToImageFormat()internal/magic/imports only stdlib (bytes,errors,io,strings)magic.DetectFormat(),magic.ValidateMagicBytes(), etc..golangci.yml,Makefile,Dockerfile,.gitea/workflows/untoucheddocker build .passesNotes
ImageFormattype inimgcache.gois correctly preserved (other packages likeencurl,handlers,urlparserstill referenceimgcache.ImageFormat).imgcache.DetectFormat, etc.) remain anywhere in the codebase.Part of issue #39.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.