images

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2023 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ProviderTimeout should be used by all callers for calles to `Images`
	ProviderTimeout = 500 * time.Millisecond
)

Variables

View Source
var (
	ErrImageNotFound = errors.New("image not found")

	// ErrImagesDone is used to stop iteration of subsequent images. It should be
	// returned from forEachFunc when either the intended image has been found or
	// the maximum number of images has been iterated.
	ErrImagesDone = errors.New("images done")

	// ErrImagesNoPath is returned whenever an image is found but has no path on disk.
	ErrImagesNoPath = errors.New("no path for image")

	// ErrImagesNoURL is returned whenever an image is found but has no URL.
	ErrImagesNoURL = errors.New("no URL for image")

	ErrImagesUnavailable = errors.New("alert screenshots are unavailable")

	// ErrNoImageForAlert is returned when no image is associated to a given alert.
	ErrNoImageForAlert = errors.New("no image for alert")
)

Functions

func WithStoredImages

func WithStoredImages(ctx context.Context, l logging.Logger, imageProvider Provider, forEachFunc forEachImageFunc, alerts ...*types.Alert) error

WithStoredImages retrieves the image for each alert and then calls forEachFunc with the index of the alert and the retrieved image struct. If the alert does not have an image token, or the image does not exist then forEachFunc will not be called for that alert. If forEachFunc returns an error, WithStoredImages will return the error and not iterate the remaining alerts. A forEachFunc can return ErrImagesDone to stop the iteration of remaining alerts if the intended image or maximum number of images have been found.

Types

type FakeProvider

type FakeProvider struct {
	Images []*Image
	Bytes  []byte
}

func (*FakeProvider) GetImage

func (f *FakeProvider) GetImage(_ context.Context, token string) (*Image, error)

GetImage returns an image with the same token.

func (*FakeProvider) GetImageURL

func (f *FakeProvider) GetImageURL(_ context.Context, alert *types.Alert) (string, error)

GetImageURL returns the URL of the image associated with a given alert.

func (*FakeProvider) GetRawImage

func (f *FakeProvider) GetRawImage(_ context.Context, alert *types.Alert) (io.ReadCloser, string, error)

GetRawImage returns an io.Reader to read the bytes of the image associated with a given alert.

type Image

type Image struct {
	Token     string
	Path      string
	URL       string
	CreatedAt time.Time
}

func (Image) HasURL

func (i Image) HasURL() bool

type Provider

type Provider interface {
	// GetImage takes a token (identifier) and returns the image that token belongs to.
	// Returns `ErrImageNotFound` if there's no image for said token.
	//
	// Deprecated: This method will be removed when all integrations use GetImageURL and/or GetRawImage,
	// which allow integrations to get just the data they need for adding images to notifications.
	// Use any of those two methods instead.
	GetImage(ctx context.Context, token string) (*Image, error)

	// GetImageURL returns the URL of an image associated with a given alert.
	//	- Returns `ErrImageNotFound` if no image is found.
	//	- Returns `ErrImagesNoURL` if the image doesn't have a URL.
	GetImageURL(ctx context.Context, alert *types.Alert) (string, error)

	// GetRawImage returns an io.Reader to read the bytes of an image associated with a given alert
	// and a string representing the filename.
	//	- Returns `ErrImageNotFound` if no image is found.
	//	- Returns `ErrImagesNoPath` if the image doesn't have a path on disk.
	GetRawImage(ctx context.Context, alert *types.Alert) (io.ReadCloser, string, error)
}

func NewFakeProvider

func NewFakeProvider(n int) Provider

NewFakeProvider returns an image provider with N test images. Each image has a token and a URL, but does not have a file on disk.

func NewFakeProviderWithFile

func NewFakeProviderWithFile(t *testing.T, n int) Provider

NewFakeProviderWithFile returns an image provider with N test images. Each image has a token, path and a URL, where the path is 1x1 transparent PNG on disk. The test should call deleteFunc to delete the images from disk at the end of the test. nolint:deadcode,unused

type UnavailableProvider

type UnavailableProvider struct{}

func (*UnavailableProvider) GetImage

GetImage returns the image with the corresponding token, or ErrImageNotFound.

func (*UnavailableProvider) GetImageURL

func (u *UnavailableProvider) GetImageURL(context.Context, *types.Alert) (string, error)

func (*UnavailableProvider) GetRawImage

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL