mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
* chore(lint): ban panic in production code Add a forbidigo rule that rejects panic outside tests and test helpers (_test.go, tests/ and storagetest/). Its message says to return an error, and to use log.Fatal only for startup failures. The existing production panics are gone: - model.MustParseArtworkID was only used by core/artwork tests. It is now a Gomega helper in those test suites, and it also panicked with the parsed ID instead of the error. - isDBInitialized in db/migrations panicked on a query error and never closed its rows. It now uses QueryRowContext and logs the error. The two http.ErrAbortHandler panics in the stream and API v1 handlers stay, because net/http requires them to abort a response. They are marked with //nolint:forbidigo. cmd/wire_injectors.go is not linted, because of its wireinject build tag. * chore(lint): ban panic in tests too The panic ban excluded test files and test helpers, so agents could read it as permission to panic in tests. The rule now applies everywhere, and its message says what to use instead: return an error, or Expect(...)/Fail(...) in tests. Test panics are replaced with Gomega assertions, and the helpers that assert call GinkgoHelper() so failures point at the calling spec. The fake HTTP clients in the deezer tests and getDirEntry use Fail(...). storagetest.FakeFS.UpdateTags now asserts with Gomega instead of panicking. Seven test panics stay, marked //nolint:forbidigo: they exercise panic recovery or stand in for log.Fatal. * refactor(tests): share panicking test helpers Move mustParseArtworkID out of the two core/artwork suites into tests.MustParseArtworkID, and add conf.PanicOnLogFatal so the conf specs share one panicking logFatal stub and one //nolint. The MustPath spec now asserts the fatal message instead of capturing it. Also merge the back-to-back user and artist loops in the persistence suite setup.
158 lines
4.4 KiB
Go
158 lines
4.4 KiB
Go
package artwork
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"image"
|
|
"image/color"
|
|
"image/gif"
|
|
"image/png"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Animation detection", func() {
|
|
Describe("isAnimatedGIF", func() {
|
|
It("detects an animated GIF with multiple frames", func() {
|
|
Expect(isAnimatedGIF(createAnimatedGIF(2))).To(BeTrue())
|
|
})
|
|
|
|
It("detects an animated GIF with many frames", func() {
|
|
Expect(isAnimatedGIF(createAnimatedGIF(5))).To(BeTrue())
|
|
})
|
|
|
|
It("does not flag a static GIF (single frame)", func() {
|
|
Expect(isAnimatedGIF(createAnimatedGIF(1))).To(BeFalse())
|
|
})
|
|
|
|
It("returns false for non-GIF data", func() {
|
|
Expect(isAnimatedGIF(nil)).To(BeFalse())
|
|
Expect(isAnimatedGIF([]byte{0xFF, 0xD8})).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
Describe("isAnimatedWebP", func() {
|
|
It("detects an animated WebP with ANMF chunk", func() {
|
|
Expect(isAnimatedWebP(createAnimatedWebPBytes())).To(BeTrue())
|
|
})
|
|
|
|
It("does not flag a static WebP (no ANMF chunk)", func() {
|
|
Expect(isAnimatedWebP(createStaticWebPBytes())).To(BeFalse())
|
|
})
|
|
|
|
It("returns false for non-WebP data", func() {
|
|
Expect(isAnimatedWebP(nil)).To(BeFalse())
|
|
Expect(isAnimatedWebP([]byte{0xFF, 0xD8})).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
Describe("isAnimatedPNG", func() {
|
|
It("detects an APNG with acTL chunk", func() {
|
|
Expect(isAnimatedPNG(createAPNGBytes())).To(BeTrue())
|
|
})
|
|
|
|
It("does not flag a static PNG (no acTL chunk)", func() {
|
|
Expect(isAnimatedPNG(createStaticPNGBytes())).To(BeFalse())
|
|
})
|
|
|
|
It("returns false for non-PNG data", func() {
|
|
Expect(isAnimatedPNG(nil)).To(BeFalse())
|
|
Expect(isAnimatedPNG([]byte{0xFF, 0xD8})).To(BeFalse())
|
|
})
|
|
})
|
|
})
|
|
|
|
// createAnimatedGIF creates a minimal animated GIF with the given number of frames.
|
|
func createAnimatedGIF(frames int) []byte {
|
|
GinkgoHelper()
|
|
g := &gif.GIF{
|
|
LoopCount: 0,
|
|
}
|
|
for range frames {
|
|
img := image.NewPaletted(image.Rect(0, 0, 2, 2), color.Palette{color.Black, color.White})
|
|
g.Image = append(g.Image, img)
|
|
g.Delay = append(g.Delay, 10)
|
|
}
|
|
var buf bytes.Buffer
|
|
Expect(gif.EncodeAll(&buf, g)).To(Succeed())
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// writeUint32LE appends a little-endian uint32 to the buffer.
|
|
func writeUint32LE(buf *bytes.Buffer, v uint32) {
|
|
b := make([]byte, 4)
|
|
binary.LittleEndian.PutUint32(b, v)
|
|
buf.Write(b)
|
|
}
|
|
|
|
// writeUint32BE appends a big-endian uint32 to the buffer.
|
|
func writeUint32BE(buf *bytes.Buffer, v uint32) {
|
|
b := make([]byte, 4)
|
|
binary.BigEndian.PutUint32(b, v)
|
|
buf.Write(b)
|
|
}
|
|
|
|
// createAnimatedWebPBytes creates a minimal RIFF/WEBP container with an ANMF chunk.
|
|
func createAnimatedWebPBytes() []byte {
|
|
var buf bytes.Buffer
|
|
buf.WriteString("RIFF")
|
|
writeUint32LE(&buf, 100) // file size placeholder
|
|
buf.WriteString("WEBP")
|
|
// VP8X chunk (extended format, required for animation)
|
|
buf.WriteString("VP8X")
|
|
writeUint32LE(&buf, 10)
|
|
buf.Write(make([]byte, 10))
|
|
// ANIM chunk (animation parameters)
|
|
buf.WriteString("ANIM")
|
|
writeUint32LE(&buf, 6)
|
|
buf.Write(make([]byte, 6))
|
|
// ANMF chunk (animation frame)
|
|
buf.WriteString("ANMF")
|
|
writeUint32LE(&buf, 16)
|
|
buf.Write(make([]byte, 16))
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// createStaticWebPBytes creates a minimal RIFF/WEBP container without ANMF chunks.
|
|
func createStaticWebPBytes() []byte {
|
|
var buf bytes.Buffer
|
|
buf.WriteString("RIFF")
|
|
writeUint32LE(&buf, 20) // file size
|
|
buf.WriteString("WEBP")
|
|
// VP8 chunk (simple lossy format)
|
|
buf.WriteString("VP8 ")
|
|
writeUint32LE(&buf, 4)
|
|
buf.Write(make([]byte, 4))
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// createAPNGBytes creates a minimal PNG with an acTL chunk (making it APNG).
|
|
func createAPNGBytes() []byte {
|
|
// Start with a real PNG
|
|
staticPNG := createStaticPNGBytes()
|
|
|
|
// Insert an acTL chunk after the IHDR chunk.
|
|
// PNG structure: signature (8) + IHDR chunk (4 len + 4 type + 13 data + 4 crc = 25)
|
|
ihdrEnd := 8 + 25
|
|
var buf bytes.Buffer
|
|
buf.Write(staticPNG[:ihdrEnd])
|
|
// Write acTL chunk: length=8, type="acTL", data=num_frames(4)+num_plays(4), CRC=4
|
|
writeUint32BE(&buf, 8) // chunk data length
|
|
buf.WriteString("acTL")
|
|
writeUint32BE(&buf, 2) // num_frames
|
|
writeUint32BE(&buf, 0) // num_plays (0 = infinite)
|
|
writeUint32BE(&buf, 0) // CRC placeholder
|
|
buf.Write(staticPNG[ihdrEnd:])
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// createStaticPNGBytes creates a minimal valid static PNG.
|
|
func createStaticPNGBytes() []byte {
|
|
GinkgoHelper()
|
|
img := image.NewRGBA(image.Rect(0, 0, 2, 2))
|
|
var buf bytes.Buffer
|
|
Expect(png.Encode(&buf, img)).To(Succeed())
|
|
return buf.Bytes()
|
|
}
|