images

package
v0.0.0-...-9d7e009 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: AGPL-3.0 Imports: 10 Imported by: 2

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")

	ErrImagesUnavailable = errors.New("alert screenshots are unavailable")
)
View Source
var ErrImageUploadNotSupported = errors.New("image upload is not supported")

ErrImageUploadNotSupported is returned when image uploading is not supported.

Functions

func GetImageToken

func GetImageToken(alert types.Alert) string

GetImageToken is a helper function to retrieve the image token from the alert annotations.

func GetImageURL

func GetImageURL(alert types.Alert) string

GetImageURL is a helper function to retrieve the image url from the alert annotations.

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 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 FakeTokenStore

type FakeTokenStore struct {
	Images map[string]*Image
}

func NewFakeTokenStore

func NewFakeTokenStore(n int) *FakeTokenStore

NewFakeTokenStore returns an token store with N test images. Each image has a URL, but does not have a file on disk. They are mapped to the token test-image-%d

func NewFakeTokenStoreFromImages

func NewFakeTokenStoreFromImages(images map[string]*Image) *FakeTokenStore

func NewFakeTokenStoreWithFile

func NewFakeTokenStoreWithFile(t *testing.T, n int) *FakeTokenStore

NewFakeTokenStoreWithFile returns an image provider with N test images. Each image has a URL and raw data. They are mapped to the token test-image-%d

func (FakeTokenStore) GetImage

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

type Image

type Image struct {
	// URL is the public URL of the image. This URL should not be treated as a trusted source and should not be
	// downloaded directly. RawData should be used to retrieve the image data.
	URL string
	// RawData returns the raw image data. Depending on the provider, this may be a file read, a network request, or
	// unsupported. It's the responsibility of the Provider to ensure that the data is safe to read.
	RawData func(ctx context.Context) (ImageContent, error)
}

func (Image) HasURL

func (i Image) HasURL() bool

type ImageContent

type ImageContent struct {
	// Name is the unique identifier for the image. Usually this will be an image filename, but is not required to be.
	Name string
	// Content is the raw image data.
	Content []byte
}

type Provider

type Provider interface {
	// GetImage takes an alert and returns its associated image.
	GetImage(ctx context.Context, alert types.Alert) (*Image, error)
}

func NewFakeProvider

func NewFakeProvider(n int) Provider

func NewFakeProviderWithFile

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

func NewTokenProvider

func NewTokenProvider(store TokenStore, logger logging.Logger) Provider

type TokenProvider

type TokenProvider struct {
	// contains filtered or unexported fields
}

TokenProvider implements the ImageProvider interface, retrieving images from a store using tokens. Image data should be considered trusted as the stored image URL and content are not user-modifiable.

func (TokenProvider) GetImage

func (i TokenProvider) GetImage(ctx context.Context, alert types.Alert) (*Image, error)

type TokenStore

type TokenStore interface {
	GetImage(ctx context.Context, token string) (*Image, error)
}

type URLProvider

type URLProvider struct{}

URLProvider is a provider that stores a direct reference to an image's public URL in an alert's annotations. The URL is not validated against a database record, so retrieving raw image data is blocked in an attempt to prevent malicious access to untrusted URLs.

func (*URLProvider) GetImage

func (u *URLProvider) GetImage(_ context.Context, alert types.Alert) (*Image, error)

GetImage returns the image associated with a given alert. The URL should be treated as untrusted and notifiers should pass the URL directly without attempting to download the image data.

type UnavailableProvider

type UnavailableProvider struct{}

func (*UnavailableProvider) GetImage

func (u *UnavailableProvider) GetImage(_ context.Context, _ types.Alert) (*Image, error)

Jump to

Keyboard shortcuts

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