From 86f16790ea2e4eede6403f3df857371060de17f6 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Tue, 31 Oct 2023 14:24:31 +0100 Subject: [PATCH] JPEG: Read files with invalid color data from the beginning #3843 Using a ReadSeeker is necessary to read images with invalid color metadata from the beginning in order to avoid decoding errors. Signed-off-by: Michael Mayer --- internal/thumb/open_jpeg.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/internal/thumb/open_jpeg.go b/internal/thumb/open_jpeg.go index 0a94d08a0..90916da0f 100644 --- a/internal/thumb/open_jpeg.go +++ b/internal/thumb/open_jpeg.go @@ -15,18 +15,28 @@ import ( ) // decodeImage opens an image and decodes its color metadata. -func decodeImage(reader io.Reader, logName string) (md *meta.Data, img image.Image, err error) { +func decodeImage(reader io.ReadSeeker, logName string) (metaData *meta.Data, img image.Image, err error) { // Read color metadata. - md, imgStream, err := autometa.Load(reader) + metaData, imgReader, err := autometa.Load(reader) - if err != nil { - log.Warnf("thumb: %s in %s (read color metadata)", err, logName) - img, err = imaging.Decode(reader) - } else { - img, err = imaging.Decode(imgStream) + if err == nil { + img, err = imaging.Decode(imgReader) + return metaData, img, err } - return md, img, err + // Log color metadata read error. + log.Warnf("thumb: %s in %s (read color metadata)", err, logName) + + // Seek to the file start in order to avoid decoding errors, + // see https://github.com/photoprism/photoprism/issues/3843 + if _, err = reader.Seek(0, io.SeekStart); err != nil { + log.Errorf("thumb: %s in %s (seek to file start)", err, logName) + } + + // Decode image file. + img, err = imaging.Decode(reader) + + return metaData, img, err } // OpenJpeg loads a JPEG image from disk, rotates it, and converts the color profile if necessary.