Skip to content

Commit 7c7ac56

Browse files
committedDec 25, 2019
Refactor subpackages
1 parent 8ce331d commit 7c7ac56

14 files changed

+123
-79
lines changed
 

‎download.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"net/http"
1111
"time"
1212

13-
imagesize "github.com/imgproxy/imgproxy/image_size"
13+
"github.com/imgproxy/imgproxy/imagemeta"
1414
)
1515

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

104104
func checkTypeAndDimensions(r io.Reader) (imageType, error) {
105-
meta, err := imagesize.DecodeMeta(r)
106-
if err == imagesize.ErrFormat {
105+
meta, err := imagemeta.DecodeMeta(r)
106+
if err == imagemeta.ErrFormat {
107107
return imageTypeUnknown, errSourceImageTypeNotSupported
108108
}
109109
if err != nil {
110110
return imageTypeUnknown, newUnexpectedError(err.Error(), 0)
111111
}
112112

113-
imgtype, imgtypeOk := imageTypes[meta.Format]
113+
imgtype, imgtypeOk := imageTypes[meta.Format()]
114114
if !imgtypeOk || !imageTypeLoadSupport(imgtype) {
115115
return imageTypeUnknown, errSourceImageTypeNotSupported
116116
}
117117

118-
if err = checkDimensions(meta.Width, meta.Height); err != nil {
118+
if err = checkDimensions(meta.Width(), meta.Height()); err != nil {
119119
return imageTypeUnknown, err
120120
}
121121

‎image_size/bmp.go ‎imagemeta/bmp.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package imagesize
1+
package imagemeta
22

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

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

15-
func DecodeBmpMeta(r io.Reader) (*Meta, error) {
15+
func DecodeBmpMeta(r io.Reader) (Meta, error) {
1616
var tmp [26]byte
1717

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

39-
return &Meta{
40-
Format: "bmp",
41-
Width: width,
42-
Height: height,
39+
return &meta{
40+
format: "bmp",
41+
width: width,
42+
height: height,
4343
}, nil
4444
}
4545

‎image_size/gif.go ‎imagemeta/gif.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
package imagesize
1+
package imagemeta
22

33
import (
44
"io"
55
)
66

7-
func DecodeGifMeta(r io.Reader) (*Meta, error) {
7+
func DecodeGifMeta(r io.Reader) (Meta, error) {
88
var tmp [10]byte
99

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

15-
return &Meta{
16-
Format: "gif",
17-
Width: int(tmp[6]) + int(tmp[7])<<8,
18-
Height: int(tmp[8]) + int(tmp[9])<<8,
15+
return &meta{
16+
format: "gif",
17+
width: int(tmp[6]) + int(tmp[7])<<8,
18+
height: int(tmp[8]) + int(tmp[9])<<8,
1919
}, nil
2020
}
2121

‎image_size/heic.go ‎imagemeta/heic.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package imagesize
1+
package imagemeta
22

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

172-
func DecodeHeicMeta(r io.Reader) (*Meta, error) {
172+
func DecodeHeicMeta(r io.Reader) (Meta, error) {
173173
d := new(heicDimensionsData)
174174

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

179-
return &Meta{
180-
Format: "heic",
181-
Width: int(d.Width),
182-
Height: int(d.Height),
179+
return &meta{
180+
format: "heic",
181+
width: int(d.Width),
182+
height: int(d.Height),
183183
}, nil
184184
}
185185

‎image_size/ico.go ‎imagemeta/ico.go

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
package imagesize
1+
package imagemeta
22

33
import (
44
"encoding/binary"
55
"io"
66
)
77

8+
type IcoMeta struct {
9+
Meta
10+
offset int
11+
size int
12+
}
13+
14+
func (m *IcoMeta) BestImageOffset() int {
15+
return m.offset
16+
}
17+
18+
func (m *IcoMeta) BestImageSize() int {
19+
return m.size
20+
}
21+
822
func icoBestSize(r io.Reader) (width, height byte, offset uint32, size uint32, err error) {
923
var tmp [16]byte
1024

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

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

55-
return &Meta{
56-
Format: "ico",
57-
Width: width,
58-
Height: height,
69+
return &IcoMeta{
70+
Meta: &meta{
71+
format: "ico",
72+
width: width,
73+
height: height,
74+
},
75+
offset: int(offset),
76+
size: int(size),
5977
}, nil
6078
}
6179

6280
func init() {
63-
RegisterFormat("\x00\x00\x01\x00", DecodeIcoMeta)
81+
RegisterFormat(
82+
"\x00\x00\x01\x00",
83+
func(r io.Reader) (Meta, error) { return DecodeIcoMeta(r) },
84+
)
6485
}

‎image_size/image_meta.go ‎imagemeta/image_meta.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package imagesize
1+
package imagemeta
22

33
import (
44
"bufio"
@@ -8,14 +8,34 @@ import (
88
"sync/atomic"
99
)
1010

11-
type Meta struct {
12-
Format string
13-
Width, Height int
11+
type Meta interface {
12+
Format() string
13+
Width() int
14+
Height() int
15+
}
16+
17+
type DecodeMetaFunc func(io.Reader) (Meta, error)
18+
19+
type meta struct {
20+
format string
21+
width, height int
22+
}
23+
24+
func (m *meta) Format() string {
25+
return m.format
26+
}
27+
28+
func (m *meta) Width() int {
29+
return m.width
30+
}
31+
32+
func (m *meta) Height() int {
33+
return m.height
1434
}
1535

1636
type format struct {
1737
magic string
18-
decodeMeta func(io.Reader) (*Meta, error)
38+
decodeMeta DecodeMetaFunc
1939
}
2040

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

52-
func RegisterFormat(magic string, decodeMeta func(io.Reader) (*Meta, error)) {
72+
func RegisterFormat(magic string, decodeMeta DecodeMetaFunc) {
5373
formatsMu.Lock()
5474
defer formatsMu.Unlock()
5575

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

60-
func DecodeMeta(r io.Reader) (*Meta, error) {
80+
func DecodeMeta(r io.Reader) (Meta, error) {
6181
rr := asReader(r)
6282
formats, _ := atomicFormats.Load().([]format)
6383

‎image_size/jpeg.go ‎imagemeta/jpeg.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package imagesize
1+
package imagemeta
22

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

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

35-
func DecodeJpegMeta(rr io.Reader) (*Meta, error) {
35+
func DecodeJpegMeta(rr io.Reader) (Meta, error) {
3636
var tmp [512]byte
3737

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

103-
return &Meta{
104-
Format: "jpeg",
105-
Width: int(tmp[3])<<8 + int(tmp[4]),
106-
Height: int(tmp[1])<<8 + int(tmp[2]),
103+
return &meta{
104+
format: "jpeg",
105+
width: int(tmp[3])<<8 + int(tmp[4]),
106+
height: int(tmp[1])<<8 + int(tmp[2]),
107107
}, nil
108108
}
109109

‎image_size/png.go ‎imagemeta/png.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package imagesize
1+
package imagemeta
22

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

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

15-
func DecodePngMeta(r io.Reader) (*Meta, error) {
15+
func DecodePngMeta(r io.Reader) (Meta, error) {
1616
var tmp [16]byte
1717

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

30-
return &Meta{
31-
Format: "png",
32-
Width: int(binary.BigEndian.Uint32(tmp[8:12])),
33-
Height: int(binary.BigEndian.Uint32(tmp[12:16])),
30+
return &meta{
31+
format: "png",
32+
width: int(binary.BigEndian.Uint32(tmp[8:12])),
33+
height: int(binary.BigEndian.Uint32(tmp[12:16])),
3434
}, nil
3535
}
3636

‎image_size/svg.go ‎imagemeta/svg.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package imagesize
1+
package imagemeta
22

33
import (
44
"io"
@@ -7,8 +7,8 @@ import (
77
func init() {
88
// Register fake svg decoder. Since we need this only for type detecting, we can
99
// return fake image sizes
10-
decodeMeta := func(io.Reader) (*Meta, error) {
11-
return &Meta{Format: "svg", Width: 1, Height: 1}, nil
10+
decodeMeta := func(io.Reader) (Meta, error) {
11+
return &meta{format: "svg", width: 1, height: 1}, nil
1212
}
1313
RegisterFormat("<?xml ", decodeMeta)
1414
RegisterFormat("<svg", decodeMeta)

‎image_size/tiff.go ‎imagemeta/tiff.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package imagesize
1+
package imagemeta
22

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

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

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

106106
if width > 0 && height > 0 {
107-
return &Meta{
108-
Format: "tiff",
109-
Width: width,
110-
Height: height,
107+
return &meta{
108+
format: "tiff",
109+
width: width,
110+
height: height,
111111
}, nil
112112
}
113113
}

‎image_size/webp.go ‎imagemeta/webp.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// Original code was cropped and fixed by @DarthSim for imgproxy needs
66

7-
package imagesize
7+
package imagemeta
88

99
import (
1010
"errors"
@@ -25,7 +25,7 @@ var (
2525
webpFccWEBP = riff.FourCC{'W', 'E', 'B', 'P'}
2626
)
2727

28-
func DecodeWebpMeta(r io.Reader) (*Meta, error) {
28+
func DecodeWebpMeta(r io.Reader) (Meta, error) {
2929
formType, riffReader, err := riff.NewReader(r)
3030
if err != nil {
3131
return nil, err
@@ -58,10 +58,10 @@ func DecodeWebpMeta(r io.Reader) (*Meta, error) {
5858

5959
fh, err := d.DecodeFrameHeader()
6060

61-
return &Meta{
62-
Format: "webp",
63-
Width: fh.Width,
64-
Height: fh.Height,
61+
return &meta{
62+
format: "webp",
63+
width: fh.Width,
64+
height: fh.Height,
6565
}, err
6666

6767
case webpFccVP8L:
@@ -70,10 +70,10 @@ func DecodeWebpMeta(r io.Reader) (*Meta, error) {
7070
return nil, err
7171
}
7272

73-
return &Meta{
74-
Format: "webp",
75-
Width: conf.Width,
76-
Height: conf.Height,
73+
return &meta{
74+
format: "webp",
75+
width: conf.Width,
76+
height: conf.Height,
7777
}, nil
7878

7979
case webpFccVP8X:
@@ -88,10 +88,10 @@ func DecodeWebpMeta(r io.Reader) (*Meta, error) {
8888
widthMinusOne := uint32(buf[4]) | uint32(buf[5])<<8 | uint32(buf[6])<<16
8989
heightMinusOne := uint32(buf[7]) | uint32(buf[8])<<8 | uint32(buf[9])<<16
9090

91-
return &Meta{
92-
Format: "webp",
93-
Width: int(widthMinusOne) + 1,
94-
Height: int(heightMinusOne) + 1,
91+
return &meta{
92+
format: "webp",
93+
width: int(widthMinusOne) + 1,
94+
height: int(heightMinusOne) + 1,
9595
}, nil
9696

9797
default:

‎process.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"math"
88
"runtime"
99

10-
imagesize "github.com/imgproxy/imgproxy/image_size"
10+
"github.com/imgproxy/imgproxy/imagemeta"
1111
"golang.org/x/sync/errgroup"
1212
)
1313

@@ -564,26 +564,29 @@ func transformAnimated(ctx context.Context, img *vipsImage, data []byte, po *pro
564564
}
565565

566566
func getIcoData(imgdata *imageData) (*imageData, error) {
567-
offset, size, err := imagesize.BestIcoPage(bytes.NewBuffer(imgdata.Data))
567+
icoMeta, err := imagemeta.DecodeIcoMeta(bytes.NewReader(imgdata.Data))
568568
if err != nil {
569569
return nil, err
570570
}
571571

572+
offset := icoMeta.BestImageOffset()
573+
size := icoMeta.BestImageSize()
574+
572575
data := imgdata.Data[offset : offset+size]
573576

574-
meta, err := imagesize.DecodeMeta(bytes.NewBuffer(data))
577+
meta, err := imagemeta.DecodeMeta(bytes.NewReader(data))
575578
if err != nil {
576579
return nil, err
577580
}
578581

579-
if imgtype, ok := imageTypes[meta.Format]; ok && vipsTypeSupportLoad[imgtype] {
582+
if imgtype, ok := imageTypes[meta.Format()]; ok && vipsTypeSupportLoad[imgtype] {
580583
return &imageData{
581584
Data: data,
582585
Type: imgtype,
583586
}, nil
584587
}
585588

586-
return nil, fmt.Errorf("Can't load %s from ICO", meta.Format)
589+
return nil, fmt.Errorf("Can't load %s from ICO", meta.Format())
587590
}
588591

589592
func saveImageToFitBytes(po *processingOptions, img *vipsImage) ([]byte, context.CancelFunc, error) {

‎processing_options.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"strings"
1313
"sync"
1414

15-
structdiff "github.com/imgproxy/imgproxy/struct-diff"
15+
"github.com/imgproxy/imgproxy/structdiff"
1616
)
1717

1818
type urlOption struct {
File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.