mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-04 18:33:36 +02:00
Apply new errors processing to more code parts
This commit is contained in:
21
vips/bmp.go
21
vips/bmp.go
@@ -9,7 +9,6 @@ import "C"
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
"unsafe"
|
||||
|
||||
@@ -36,10 +35,6 @@ type bmpHeader struct {
|
||||
colorImportant uint32
|
||||
}
|
||||
|
||||
// errBmpUnsupported means that the input BMP image uses a valid but unsupported
|
||||
// feature.
|
||||
var errBmpUnsupported = errors.New("unsupported BMP image")
|
||||
|
||||
func readUint16(b []byte) uint16 {
|
||||
return uint16(b[0]) | uint16(b[1])<<8
|
||||
}
|
||||
@@ -264,7 +259,7 @@ Loop:
|
||||
// If topDown is false, the image rows will be read bottom-up.
|
||||
func (img *Image) decodeBmpRGB(r io.Reader, width, height, bands int, topDown, noAlpha bool) error {
|
||||
if bands != 3 && bands != 4 {
|
||||
return errBmpUnsupported
|
||||
return newVipsError("unsupported BMP image")
|
||||
}
|
||||
|
||||
imgBands := 3
|
||||
@@ -381,13 +376,13 @@ func (img *Image) loadBmp(data []byte, noAlpha bool) error {
|
||||
}
|
||||
|
||||
if string(b[:2]) != "BM" {
|
||||
return errors.New("not a BMP image")
|
||||
return newVipsError("not a BMP image")
|
||||
}
|
||||
|
||||
offset := readUint32(b[10:14])
|
||||
infoLen := readUint32(b[14:18])
|
||||
if infoLen != infoHeaderLen && infoLen != v4InfoHeaderLen && infoLen != v5InfoHeaderLen {
|
||||
return errBmpUnsupported
|
||||
return newVipsError("unsupported BMP image")
|
||||
}
|
||||
|
||||
if _, err := io.ReadFull(r, b[fileHeaderLen+4:fileHeaderLen+infoLen]); err != nil {
|
||||
@@ -405,14 +400,14 @@ func (img *Image) loadBmp(data []byte, noAlpha bool) error {
|
||||
height, topDown = -height, true
|
||||
}
|
||||
if width <= 0 || height <= 0 {
|
||||
return errBmpUnsupported
|
||||
return newVipsError("unsupported BMP image")
|
||||
}
|
||||
|
||||
// We only support 1 plane and 8, 24 or 32 bits per pixel
|
||||
planes, bpp, compression := readUint16(b[26:28]), readUint16(b[28:30]), readUint32(b[30:34])
|
||||
|
||||
if planes != 1 {
|
||||
return errBmpUnsupported
|
||||
return newVipsError("unsupported BMP image")
|
||||
}
|
||||
|
||||
rle := false
|
||||
@@ -447,10 +442,10 @@ func (img *Image) loadBmp(data []byte, noAlpha bool) error {
|
||||
case bpp == 32 && rmask == 0xff0000 && gmask == 0xff00 && bmask == 0xff && amask == 0xff000000:
|
||||
// Go ahead, it's a regular 32-bit image
|
||||
default:
|
||||
return errBmpUnsupported
|
||||
return newVipsError("unsupported BMP image")
|
||||
}
|
||||
default:
|
||||
return errBmpUnsupported
|
||||
return newVipsError("unsupported BMP image")
|
||||
}
|
||||
|
||||
var palette []Color
|
||||
@@ -497,7 +492,7 @@ func (img *Image) loadBmp(data []byte, noAlpha bool) error {
|
||||
return img.decodeBmpRGB(r, width, height, 4, topDown, noAlpha)
|
||||
}
|
||||
|
||||
return errBmpUnsupported
|
||||
return newVipsError("unsupported BMP image")
|
||||
}
|
||||
|
||||
func (img *Image) saveAsBmp() (*imagedata.ImageData, error) {
|
||||
|
@@ -18,7 +18,7 @@ func ColorFromHex(hexcolor string) (Color, error) {
|
||||
c := Color{}
|
||||
|
||||
if !hexColorRegex.MatchString(hexcolor) {
|
||||
return c, fmt.Errorf("Invalid hex color: %s", hexcolor)
|
||||
return c, newColorError("Invalid hex color: %s", hexcolor)
|
||||
}
|
||||
|
||||
if len(hexcolor) == 3 {
|
||||
|
@@ -1,13 +1,32 @@
|
||||
package vips
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/imgproxy/imgproxy/v3/ierrors"
|
||||
)
|
||||
|
||||
type VipsError string
|
||||
type (
|
||||
VipsError string
|
||||
ColorError string
|
||||
)
|
||||
|
||||
func newVipsError(msg string) error {
|
||||
return ierrors.Wrap(VipsError(msg), 2)
|
||||
return ierrors.Wrap(VipsError(msg), 1)
|
||||
}
|
||||
|
||||
func newVipsErrorf(format string, args ...interface{}) error {
|
||||
return ierrors.Wrap(VipsError(fmt.Sprintf(format, args...)), 1)
|
||||
}
|
||||
|
||||
func (e VipsError) Error() string { return string(e) }
|
||||
|
||||
func newColorError(format string, args ...interface{}) error {
|
||||
return ierrors.Wrap(
|
||||
ColorError(fmt.Sprintf(format, args...)),
|
||||
1,
|
||||
ierrors.WithShouldReport(false),
|
||||
)
|
||||
}
|
||||
|
||||
func (e ColorError) Error() string { return string(e) }
|
||||
|
@@ -7,8 +7,6 @@ import "C"
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
"github.com/imgproxy/imgproxy/v3/imagedata"
|
||||
@@ -43,7 +41,7 @@ func (img *Image) loadIco(data []byte, shrink int, scale float64, pages int) err
|
||||
internalType = meta.Format()
|
||||
|
||||
if internalType == imagetype.ICO || !SupportsLoad(internalType) {
|
||||
return fmt.Errorf("Can't load %s from ICO", internalType)
|
||||
return newVipsErrorf("Can't load %s from ICO", internalType)
|
||||
}
|
||||
|
||||
imgdata := imagedata.ImageData{
|
||||
@@ -56,7 +54,7 @@ func (img *Image) loadIco(data []byte, shrink int, scale float64, pages int) err
|
||||
|
||||
func (img *Image) saveAsIco() (*imagedata.ImageData, error) {
|
||||
if img.Width() > 256 || img.Height() > 256 {
|
||||
return nil, errors.New("Image dimensions is too big. Max dimension size for ICO is 256")
|
||||
return nil, newVipsError("Image dimensions is too big. Max dimension size for ICO is 256")
|
||||
}
|
||||
|
||||
var ptr unsafe.Pointer
|
||||
|
@@ -9,7 +9,6 @@ package vips
|
||||
import "C"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -69,7 +68,7 @@ func Init() error {
|
||||
|
||||
if err := C.vips_initialize(); err != 0 {
|
||||
C.vips_shutdown()
|
||||
return errors.New("unable to start vips!")
|
||||
return newVipsError("unable to start vips!")
|
||||
}
|
||||
|
||||
// Disable libvips cache. Since processing pipeline is fine tuned, we won't get much profit from it.
|
||||
@@ -355,7 +354,7 @@ func (img *Image) Load(imgdata *imagedata.ImageData, shrink int, scale float64,
|
||||
case imagetype.TIFF:
|
||||
err = C.vips_tiffload_go(data, dataSize, &tmp)
|
||||
default:
|
||||
return errors.New("Usupported image type to load")
|
||||
return newVipsError("Usupported image type to load")
|
||||
}
|
||||
if err != 0 {
|
||||
return Error()
|
||||
@@ -376,7 +375,7 @@ func (img *Image) Load(imgdata *imagedata.ImageData, shrink int, scale float64,
|
||||
|
||||
func (img *Image) LoadThumbnail(imgdata *imagedata.ImageData) error {
|
||||
if imgdata.Type != imagetype.HEIC && imgdata.Type != imagetype.AVIF {
|
||||
return errors.New("Usupported image type to load thumbnail")
|
||||
return newVipsError("Usupported image type to load thumbnail")
|
||||
}
|
||||
|
||||
var tmp *C.VipsImage
|
||||
@@ -428,7 +427,7 @@ func (img *Image) Save(imgtype imagetype.Type, quality int) (*imagedata.ImageDat
|
||||
case imagetype.TIFF:
|
||||
err = C.vips_tiffsave_go(img.VipsImage, &ptr, &imgsize, C.int(quality))
|
||||
default:
|
||||
return nil, errors.New("Usupported image type to save")
|
||||
return nil, newVipsError("Usupported image type to save")
|
||||
}
|
||||
if err != 0 {
|
||||
cancel()
|
||||
|
Reference in New Issue
Block a user