[MM-63436] Replace Exif parser dependency (#30479)
* Replace Exif parser dependency * Improve forward seeking logic * Fix linting * Stop decoding upon finding tag * Use latest version of imagemeta dependency * Don't skip TIFF reader tests * Log improvements --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2454de5b4a
Коммит
f8e16780ef
@@ -4,12 +4,14 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/anthonynsimon/bild/transform"
|
||||
"github.com/rwcarlsen/goexif/exif"
|
||||
"github.com/bep/imagemeta"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -33,6 +35,8 @@ const (
|
||||
RotatedCW
|
||||
)
|
||||
|
||||
var errStopDecoding = fmt.Errorf("stop decoding")
|
||||
|
||||
// MakeImageUpright changes the orientation of the given image.
|
||||
func MakeImageUpright(img image.Image, orientation int) image.Image {
|
||||
switch orientation {
|
||||
@@ -55,23 +59,101 @@ func MakeImageUpright(img image.Image, orientation int) image.Image {
|
||||
}
|
||||
}
|
||||
|
||||
type fwSeeker struct {
|
||||
r io.Reader
|
||||
pos int64
|
||||
}
|
||||
|
||||
func (f *fwSeeker) Read(p []byte) (int, error) {
|
||||
n, err := f.r.Read(p)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
f.pos += int64(n)
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (f *fwSeeker) Seek(offset int64, whence int) (int64, error) {
|
||||
isForwardSeek := (whence == io.SeekStart && offset >= f.pos) ||
|
||||
(whence == io.SeekCurrent && offset >= 0)
|
||||
|
||||
// We only support seeking forward.
|
||||
if !isForwardSeek {
|
||||
return 0, fmt.Errorf("seeking backwards is not supported")
|
||||
}
|
||||
|
||||
toRead := offset
|
||||
if whence == io.SeekStart {
|
||||
toRead -= f.pos
|
||||
}
|
||||
|
||||
// Seeking forward means we can simply discard the data.
|
||||
n, err := io.CopyN(io.Discard, f.r, toRead)
|
||||
if err != nil {
|
||||
return n, fmt.Errorf("failed to seek: %w", err)
|
||||
}
|
||||
|
||||
f.pos += n
|
||||
|
||||
return f.pos, nil
|
||||
}
|
||||
|
||||
// GetImageOrientation reads the input data and returns the EXIF encoded
|
||||
// image orientation.
|
||||
func GetImageOrientation(input io.Reader) (int, error) {
|
||||
exifData, err := exif.Decode(input)
|
||||
if err != nil {
|
||||
// image orientation. Supported formats are JPEG, PNG, TIFF, and WebP.
|
||||
// Passing an io.ReadSeeker is preferable as we can't guarantee a plain
|
||||
// io.Reader will work for all formats (e.g. TIFF requires backwards seeking).
|
||||
func GetImageOrientation(input io.Reader, format string) (int, error) {
|
||||
orientation := Upright
|
||||
|
||||
// Strip the "image/" prefix from the format in case it's a MIME type.
|
||||
format, _ = strings.CutPrefix(format, "image/")
|
||||
|
||||
var imgFormat imagemeta.ImageFormat
|
||||
switch format {
|
||||
case "jpeg":
|
||||
imgFormat = imagemeta.JPEG
|
||||
case "png":
|
||||
imgFormat = imagemeta.PNG
|
||||
case "tiff":
|
||||
imgFormat = imagemeta.TIFF
|
||||
case "webp":
|
||||
imgFormat = imagemeta.WebP
|
||||
default:
|
||||
// We don't support EXIF on any other format.
|
||||
return orientation, fmt.Errorf("unsupported image format: %s", format)
|
||||
}
|
||||
|
||||
var rs io.ReadSeeker
|
||||
if r, ok := input.(io.ReadSeeker); ok {
|
||||
rs = r
|
||||
} else {
|
||||
rs = &fwSeeker{r: input}
|
||||
}
|
||||
|
||||
opts := imagemeta.Options{
|
||||
R: rs,
|
||||
HandleTag: func(tag imagemeta.TagInfo) error {
|
||||
if tag.Tag == "Orientation" {
|
||||
if o, ok := tag.Value.(uint16); ok {
|
||||
orientation = int(o)
|
||||
// Stop decoding after we've found the orientation tag]
|
||||
// since it's the only one we care about.
|
||||
return errStopDecoding
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
ShouldHandleTag: func(tag imagemeta.TagInfo) bool {
|
||||
// We only care about the orientation tag.
|
||||
return tag.Tag == "Orientation"
|
||||
},
|
||||
Sources: imagemeta.EXIF, // We only care about EXIF data.
|
||||
ImageFormat: imgFormat,
|
||||
}
|
||||
|
||||
if err := imagemeta.Decode(opts); err != nil && !errors.Is(err, errStopDecoding) {
|
||||
return Upright, fmt.Errorf("failed to decode exif data: %w", err)
|
||||
}
|
||||
|
||||
tag, err := exifData.Get("Orientation")
|
||||
if err != nil {
|
||||
return Upright, fmt.Errorf("failed to get orientation field from exif data: %w", err)
|
||||
}
|
||||
|
||||
orientation, err := tag.Int(0)
|
||||
if err != nil {
|
||||
return Upright, fmt.Errorf("failed to get value from exif tag: %w", err)
|
||||
}
|
||||
|
||||
return orientation, nil
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user