JPEG: Call Seek(0, 0) before opening image #2462 #2463

Found this here, although I'm really not sure how this should fix it:
- https://github.com/golang/go/issues/45902#issuecomment-1007953723

According to the tests I added, the error "unexpected EOF" remains!
At least this change shouldn't break anything either.... Help is more
than welcome if someone has more time to read through all the issue
comments.
This commit is contained in:
Michael Mayer 2022-06-25 02:16:42 +02:00
parent bd80401f00
commit 2125f1ae0a
5 changed files with 64 additions and 2 deletions

View file

@ -942,10 +942,19 @@ func (m *MediaFile) DecodeConfig() (_ *image.Config, err error) {
defer file.Close()
// Reset file offset.
// see https://github.com/golang/go/issues/45902#issuecomment-1007953723
_, err = file.Seek(0, 0)
if err != nil {
return nil, fmt.Errorf("%s on seek", err)
}
// Decode image config (dimensions).
cfg, _, err := image.DecodeConfig(file)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s while decoding", err)
}
m.imageConfig = &cfg
@ -1254,6 +1263,15 @@ func (m *MediaFile) ColorProfile() string {
defer fileReader.Close()
// Reset file offset.
// see https://github.com/golang/go/issues/45902#issuecomment-1007953723
_, err = fileReader.Seek(0, 0)
if err != nil {
log.Warnf("media: %s in %s on seek [%s]", err, logName, time.Since(start))
return ""
}
// Read color metadata.
md, _, err := autometa.Load(fileReader)

View file

@ -30,9 +30,18 @@ func OpenJpeg(fileName string, orientation int) (result image.Image, err error)
defer fileReader.Close()
// Reset file offset.
// see https://github.com/golang/go/issues/45902#issuecomment-1007953723
_, err = fileReader.Seek(0, 0)
if err != nil {
return result, fmt.Errorf("%s on seek", err)
}
// Read color metadata.
md, imgStream, err := autometa.Load(fileReader)
// Decode image.
var img image.Image
if err != nil {
@ -43,7 +52,7 @@ func OpenJpeg(fileName string, orientation int) (result image.Image, err error)
}
if err != nil {
return result, err
return result, fmt.Errorf("%s while decoding", err)
}
// Read ICC profile and convert colors if possible.

View file

@ -0,0 +1,35 @@
package thumb
import (
"testing"
)
func TestOpenJpeg(t *testing.T) {
t.Run("testdata/example.jpg", func(t *testing.T) {
img, err := OpenJpeg("testdata/example.jpg", 0)
if err != nil {
t.Fatal(err)
}
if img == nil {
t.Error("img must not be nil")
}
})
t.Run("testdata/broken.jpg", func(t *testing.T) {
if _, err := OpenJpeg("testdata/broken.jpg", 0); err == nil {
t.Error("unexpected EOF while decoding error expected")
}
})
t.Run("testdata/fixed.jpg", func(t *testing.T) {
img, err := OpenJpeg("testdata/fixed.jpg", 0)
if err != nil {
t.Fatal(err)
}
if img == nil {
t.Error("img must not be nil")
}
})
}

BIN
internal/thumb/testdata/broken.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 201 KiB

BIN
internal/thumb/testdata/fixed.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 200 KiB