Skip to content
Permalink
Browse files

Refactor subpackages

  • Loading branch information
DarthSim committed Dec 25, 2019
1 parent 8ce331d commit 7c7ac56a48e46a098ccafd00d6f7ce5aec507b60
@@ -10,7 +10,7 @@ import (
"net/http"
"time"

imagesize "github.com/imgproxy/imgproxy/image_size"
"github.com/imgproxy/imgproxy/imagemeta"
)

var (
@@ -102,20 +102,20 @@ func checkDimensions(width, height int) error {
}

func checkTypeAndDimensions(r io.Reader) (imageType, error) {
meta, err := imagesize.DecodeMeta(r)
if err == imagesize.ErrFormat {
meta, err := imagemeta.DecodeMeta(r)
if err == imagemeta.ErrFormat {
return imageTypeUnknown, errSourceImageTypeNotSupported
}
if err != nil {
return imageTypeUnknown, newUnexpectedError(err.Error(), 0)
}

imgtype, imgtypeOk := imageTypes[meta.Format]
imgtype, imgtypeOk := imageTypes[meta.Format()]
if !imgtypeOk || !imageTypeLoadSupport(imgtype) {
return imageTypeUnknown, errSourceImageTypeNotSupported
}

if err = checkDimensions(meta.Width, meta.Height); err != nil {
if err = checkDimensions(meta.Width(), meta.Height()); err != nil {
return imageTypeUnknown, err
}

@@ -1,4 +1,4 @@
package imagesize
package imagemeta

import (
"bytes"
@@ -12,7 +12,7 @@ type BmpFormatError string

func (e BmpFormatError) Error() string { return "invalid BMP format: " + string(e) }

func DecodeBmpMeta(r io.Reader) (*Meta, error) {
func DecodeBmpMeta(r io.Reader) (Meta, error) {
var tmp [26]byte

if _, err := io.ReadFull(r, tmp[:]); err != nil {
@@ -36,10 +36,10 @@ func DecodeBmpMeta(r io.Reader) (*Meta, error) {
height = int(binary.LittleEndian.Uint16(tmp[20:22]))
}

return &Meta{
Format: "bmp",
Width: width,
Height: height,
return &meta{
format: "bmp",
width: width,
height: height,
}, nil
}

@@ -1,21 +1,21 @@
package imagesize
package imagemeta

import (
"io"
)

func DecodeGifMeta(r io.Reader) (*Meta, error) {
func DecodeGifMeta(r io.Reader) (Meta, error) {
var tmp [10]byte

_, err := io.ReadFull(r, tmp[:])
if err != nil {
return nil, err
}

return &Meta{
Format: "gif",
Width: int(tmp[6]) + int(tmp[7])<<8,
Height: int(tmp[8]) + int(tmp[9])<<8,
return &meta{
format: "gif",
width: int(tmp[6]) + int(tmp[7])<<8,
height: int(tmp[8]) + int(tmp[9])<<8,
}, nil
}

@@ -1,4 +1,4 @@
package imagesize
package imagemeta

import (
"bytes"
@@ -169,17 +169,17 @@ func heicReadBoxes(d *heicDimensionsData, r io.Reader) error {
}
}

func DecodeHeicMeta(r io.Reader) (*Meta, error) {
func DecodeHeicMeta(r io.Reader) (Meta, error) {
d := new(heicDimensionsData)

if err := heicReadBoxes(d, r); err != nil && !d.IsFilled() {
return nil, err
}

return &Meta{
Format: "heic",
Width: int(d.Width),
Height: int(d.Height),
return &meta{
format: "heic",
width: int(d.Width),
height: int(d.Height),
}, nil
}

@@ -1,10 +1,24 @@
package imagesize
package imagemeta

import (
"encoding/binary"
"io"
)

type IcoMeta struct {
Meta
offset int
size int
}

func (m *IcoMeta) BestImageOffset() int {
return m.offset
}

func (m *IcoMeta) BestImageSize() int {
return m.size
}

func icoBestSize(r io.Reader) (width, height byte, offset uint32, size uint32, err error) {
var tmp [16]byte

@@ -35,8 +49,8 @@ func BestIcoPage(r io.Reader) (int, int, error) {
return int(offset), int(size), err
}

func DecodeIcoMeta(r io.Reader) (*Meta, error) {
bwidth, bheight, _, _, err := icoBestSize(r)
func DecodeIcoMeta(r io.Reader) (*IcoMeta, error) {
bwidth, bheight, offset, size, err := icoBestSize(r)
if err != nil {
return nil, err
}
@@ -52,13 +66,20 @@ func DecodeIcoMeta(r io.Reader) (*Meta, error) {
height = 256
}

return &Meta{
Format: "ico",
Width: width,
Height: height,
return &IcoMeta{
Meta: &meta{
format: "ico",
width: width,
height: height,
},
offset: int(offset),
size: int(size),
}, nil
}

func init() {
RegisterFormat("\x00\x00\x01\x00", DecodeIcoMeta)
RegisterFormat(
"\x00\x00\x01\x00",
func(r io.Reader) (Meta, error) { return DecodeIcoMeta(r) },
)
}
@@ -1,4 +1,4 @@
package imagesize
package imagemeta

import (
"bufio"
@@ -8,14 +8,34 @@ import (
"sync/atomic"
)

type Meta struct {
Format string
Width, Height int
type Meta interface {
Format() string
Width() int
Height() int
}

type DecodeMetaFunc func(io.Reader) (Meta, error)

type meta struct {
format string
width, height int
}

func (m *meta) Format() string {
return m.format
}

func (m *meta) Width() int {
return m.width
}

func (m *meta) Height() int {
return m.height
}

type format struct {
magic string
decodeMeta func(io.Reader) (*Meta, error)
decodeMeta DecodeMetaFunc
}

type reader interface {
@@ -49,15 +69,15 @@ func matchMagic(magic string, b []byte) bool {
return true
}

func RegisterFormat(magic string, decodeMeta func(io.Reader) (*Meta, error)) {
func RegisterFormat(magic string, decodeMeta DecodeMetaFunc) {
formatsMu.Lock()
defer formatsMu.Unlock()

formats, _ := atomicFormats.Load().([]format)
atomicFormats.Store(append(formats, format{magic, decodeMeta}))
}

func DecodeMeta(r io.Reader) (*Meta, error) {
func DecodeMeta(r io.Reader) (Meta, error) {
rr := asReader(r)
formats, _ := atomicFormats.Load().([]format)

@@ -1,4 +1,4 @@
package imagesize
package imagemeta

import (
"bufio"
@@ -32,7 +32,7 @@ type JpegFormatError string

func (e JpegFormatError) Error() string { return "invalid JPEG format: " + string(e) }

func DecodeJpegMeta(rr io.Reader) (*Meta, error) {
func DecodeJpegMeta(rr io.Reader) (Meta, error) {
var tmp [512]byte

r := asJpegReader(rr)
@@ -100,10 +100,10 @@ func DecodeJpegMeta(rr io.Reader) (*Meta, error) {
return nil, JpegFormatError("unsupported precision")
}

return &Meta{
Format: "jpeg",
Width: int(tmp[3])<<8 + int(tmp[4]),
Height: int(tmp[1])<<8 + int(tmp[2]),
return &meta{
format: "jpeg",
width: int(tmp[3])<<8 + int(tmp[4]),
height: int(tmp[1])<<8 + int(tmp[2]),
}, nil
}

@@ -1,4 +1,4 @@
package imagesize
package imagemeta

import (
"bytes"
@@ -12,7 +12,7 @@ type PngFormatError string

func (e PngFormatError) Error() string { return "invalid PNG format: " + string(e) }

func DecodePngMeta(r io.Reader) (*Meta, error) {
func DecodePngMeta(r io.Reader) (Meta, error) {
var tmp [16]byte

if _, err := io.ReadFull(r, tmp[:8]); err != nil {
@@ -27,10 +27,10 @@ func DecodePngMeta(r io.Reader) (*Meta, error) {
return nil, err
}

return &Meta{
Format: "png",
Width: int(binary.BigEndian.Uint32(tmp[8:12])),
Height: int(binary.BigEndian.Uint32(tmp[12:16])),
return &meta{
format: "png",
width: int(binary.BigEndian.Uint32(tmp[8:12])),
height: int(binary.BigEndian.Uint32(tmp[12:16])),
}, nil
}

@@ -1,4 +1,4 @@
package imagesize
package imagemeta

import (
"io"
@@ -7,8 +7,8 @@ import (
func init() {
// Register fake svg decoder. Since we need this only for type detecting, we can
// return fake image sizes
decodeMeta := func(io.Reader) (*Meta, error) {
return &Meta{Format: "svg", Width: 1, Height: 1}, nil
decodeMeta := func(io.Reader) (Meta, error) {
return &meta{format: "svg", width: 1, height: 1}, nil
}
RegisterFormat("<?xml ", decodeMeta)
RegisterFormat("<svg", decodeMeta)
@@ -1,4 +1,4 @@
package imagesize
package imagemeta

import (
"bufio"
@@ -37,7 +37,7 @@ type TiffFormatError string

func (e TiffFormatError) Error() string { return "invalid TIFF format: " + string(e) }

func DecodeTiffMeta(rr io.Reader) (*Meta, error) {
func DecodeTiffMeta(rr io.Reader) (Meta, error) {
var (
tmp [12]byte
byteOrder binary.ByteOrder
@@ -104,10 +104,10 @@ func DecodeTiffMeta(rr io.Reader) (*Meta, error) {
}

if width > 0 && height > 0 {
return &Meta{
Format: "tiff",
Width: width,
Height: height,
return &meta{
format: "tiff",
width: width,
height: height,
}, nil
}
}

0 comments on commit 7c7ac56

Please sign in to comment.
You can’t perform that action at this time.