gallery

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2022 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CreateCommand      = "cms.media.image.gallery.create"
	DeleteStackCommand = "cms.media.image.gallery.delete_stack"
	TagStackCommand    = "cms.media.image.gallery.tag_stack"
	UntagStackCommand  = "cms.media.image.gallery.untag_stack"
	RenameStackCommand = "cms.media.image.gallery.rename_stack"
	UpdateStackCommand = "cms.media.image.gallery.update_stack"
	SortCommand        = "cms.media.image.gallery.sort"
)

Gallery commands

View Source
const (
	Created       = "cms.media.image.gallery.created"
	ImageUploaded = "cms.media.image.gallery.image_uploaded"
	ImageReplaced = "cms.media.image.gallery.stack_replaced"
	StackDeleted  = "cms.media.image.gallery.stack_deleted"
	StackTagged   = "cms.media.image.gallery.stack_tagged"
	StackUntagged = "cms.media.image.gallery.stack_untagged"
	StackRenamed  = "cms.media.image.gallery.stack_renamed"
	StackUpdated  = "cms.media.image.gallery.stack_updated"
	Sorted        = "cms.media.image.gallery.sorted"
)
View Source
const Aggregate = "cms.media.image.gallery"

Aggregate is the name of the Gallery aggregate.

Variables

View Source
var (
	// ErrEmptyName is returned when an empty name is provided to a Gallery.
	ErrEmptyName = errors.New("empty name")

	// ErrAlreadyCreated is returned when creating a Gallery that was created already.
	ErrAlreadyCreated = errors.New("already created")

	// ErrNotCreated is returned when trying to use a Gallery that hasn't been created yet.
	ErrNotCreated = errors.New("Gallery not created")

	// ErrNotFound is returned when a Gallery cannot be found within a Repository.
	ErrNotFound = errors.New("Gallery not found")

	// ErrStackNotFound is returned when a Stack cannot be found within a Gallery.
	ErrStackNotFound = errors.New("Stack not found")

	// ErrStackCorrupted is returned updating a Stack in an illegal way.
	ErrStackCorrupted = errors.New("stack corrupted")
)

Functions

func Create

func Create(id uuid.UUID, name string) command.Command

Create returns the command to create a gallery.

func DeleteStack

func DeleteStack(galleryID, stackID uuid.UUID) command.Command

DeleteStack returns the command to delete an image from a gallery.

func EventApplier

func EventApplier(impl *Implementation) func(event.Event)

EventApplier returns the event applier function for a gallery implementation.

func HandleCommands

func HandleCommands(ctx context.Context, bus command.Bus, galleries Repository, storage media.Storage) <-chan error

HandleCommands handles commands until ctx is canceled.

func RegisterCommands

func RegisterCommands(r *codec.GobRegistry)

RegisterCommands register the gallery commands into a command registry.

func RegisterEvents

func RegisterEvents(r *codec.GobRegistry)

func RenameStack

func RenameStack(galleryID, stackID uuid.UUID, name string) command.Command

RenameStack returns the command to rename an image of a gallery.

func Sort added in v0.1.4

func Sort(galleryID uuid.UUID, sorting []uuid.UUID) command.Command

Sort returns the command to sort a gallery.

func TagStack

func TagStack(galleryID, stackID uuid.UUID, tags []string) command.Command

TagStack returns the command to tag an image of a gallery.

func UntagStack

func UntagStack(galleryID, stackID uuid.UUID, tags []string) command.Command

UntagStack returns the command to remove tags from an image of a gallery.

func UpdateStack

func UpdateStack(galleryID uuid.UUID, stack Stack) command.Command

UpdateStack returns the command to update a stack of a gallery.

Types

type CreatedData

type CreatedData struct {
	Name string
}
type Gallery struct {
	*aggregate.Base
	*Implementation
	// contains filtered or unexported fields
}

A Gallery is a collection of image stacks. A Stack may contain multiple variants of the same image in different sizes.

func New

func New(id uuid.UUID) *Gallery

New returns a new Gallery.

func (*Gallery) ApplyEvent

func (g *Gallery) ApplyEvent(evt event.Event)

type Image

type Image struct {
	media.Image

	Original bool   `json:"original"`
	Size     string `json:"size"`
}

Image is an image of a Stack.

type ImageReplacedData

type ImageReplacedData struct {
	Stack Stack
}

type ImageUploadedData

type ImageUploadedData struct {
	Stack Stack
}

type Implementation

type Implementation struct {
	Name   string `json:"name"`
	Stacks Stacks `json:"stacks"`
	// contains filtered or unexported fields
}

Implementation can be embedded into structs to implement a Gallery.

type CustomGallery struct {
	*aggregate.Base
	*Implementation

	applyEvent func(event.Event)
}

func NewCustomGallery(id uuid.UUID) *Gallery {
	g := &CustomGallery{
		Base: aggregate.New("custom-gallery", id)
	}
	g.Implementation, g.applyEvent = gallery.NewImplementation(g)
}

func (g *CustomGallery) ApplyEvent(evt event.Event) {
	g.applyEvent(evt)

	switch evt.Name() {
	case "my.custom-gallery.some_event":
		// handle custom events
	}
}

func NewImplementation

func NewImplementation(gallery aggregate.Aggregate) (*Implementation, func(event.Event))

NewImplementation returns the Implementation for the provided Gallery and the event applier for the implementation.

func (*Implementation) Create

func (g *Implementation) Create(name string) error

Create creates the Gallery with the given name.

func (*Implementation) Created added in v0.1.3

func (g *Implementation) Created() bool

Created returns whether the Gallery has been created by using g.Create.

func (*Implementation) Delete

func (g *Implementation) Delete(ctx context.Context, storage media.Storage, stack Stack) error

Delete deletes the given Stack from the Gallery and Storage.

func (*Implementation) FindByTag

func (g *Implementation) FindByTag(tags ...string) []Stack

FindByTag returns the Stacks that have all provided tags.

func (*Implementation) JSON

func (g *Implementation) JSON() JSONGallery

JSON returns the JSONGallery for g.

func (*Implementation) MarshalSnapshot

func (g *Implementation) MarshalSnapshot() ([]byte, error)

MarshalSnapshot implements snapshot.Marshaler.

func (*Implementation) RenameStack

func (g *Implementation) RenameStack(ctx context.Context, stackID uuid.UUID, name string) (Stack, error)

RenameStack renames each Image in the given Stack to name.

func (*Implementation) Replace

func (g *Implementation) Replace(ctx context.Context, storage media.Storage, r io.Reader, stackID uuid.UUID) (Stack, error)

Replace replaced the Images in the given Stack with the image in r.

func (*Implementation) Sort added in v0.1.4

func (g *Implementation) Sort(sorting []uuid.UUID) []uuid.UUID

Sort sorts the stacks by their UUIDs. The provided `sorting` determines the new order of the stacks. Stacks that are present in `sorting` take precedence over all over stacks. It is allowed to pass UUIDs of stacks that don't exist in the gallery. Sort filters these out and returns the UUIDs that are used to actually sort the stacks.

func (*Implementation) Stack

func (g *Implementation) Stack(id uuid.UUID) (Stack, error)

Stack returns the Stack with the given UUID or ErrStackNotFound.

func (*Implementation) Tag

func (g *Implementation) Tag(ctx context.Context, stack Stack, tags ...string) (Stack, error)

Tag tags each Image in the provided Stack through the ImageService.

func (*Implementation) UnmarshalSnapshot

func (g *Implementation) UnmarshalSnapshot(b []byte) error

UnmarshalSnapshot implements snapshot.Unmarshaler.

func (*Implementation) Untag

func (g *Implementation) Untag(ctx context.Context, stack Stack, tags ...string) (Stack, error)

Untag removes tags from each Image of the provided Stack.

func (*Implementation) Update

func (g *Implementation) Update(id uuid.UUID, update func(Stack) Stack) error

Update updates the Stack with the given UUID by calling update with the current Stack and replacing that Stack with the one returned by update.

It is illegal to update the UUID of a Stack. In such cases ErrStackCorrupted is returned.

func (*Implementation) Upload

func (g *Implementation) Upload(ctx context.Context, storage media.Storage, r io.Reader, name, diskName, path string) (Stack, error)

Upload uploads the image in r to storage and returns the Stack for that image.

type JSONGallery

type JSONGallery struct {
	ID     uuid.UUID `json:"id"`
	Name   string    `json:"name"`
	Stacks Stacks    `json:"stacks"`
}

JSONGallery is the JSON representation of a Gallery.

func (JSONGallery) Stack

func (g JSONGallery) Stack(id uuid.UUID) (Stack, error)

Stack returns the Stack with the given UUID or ErrStackNotFound.

type Lookup

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

Lookup provides lookup of Gallery UUIDs. It is thread-safe.

func NewLookup

func NewLookup() *Lookup

NewLookup returns a new Lookup.

func (*Lookup) ApplyEvent

func (l *Lookup) ApplyEvent(evt event.Event)

ApplyEvent applies aggregate events.

func (*Lookup) GalleryName

func (l *Lookup) GalleryName(name string) (uuid.UUID, bool)

GalleryName returns the UUID of the Gallery with the give name, or false if Lookup has no UUID for name.

func (*Lookup) Project

func (l *Lookup) Project(ctx context.Context, bus event.Bus, store event.Store, opts ...schedule.ContinuousOption) (<-chan error, error)

Project projects the Lookup in a new goroutine and returns a channel of asynchronous errors.

func (*Lookup) StackName

func (l *Lookup) StackName(galleryID uuid.UUID, name string) (uuid.UUID, bool)

StackName returns the UUID of the Stack with the given StackName in the Gallery with the given UUID, or false if Lookup has no UUID for name.

type PNGCompressor

type PNGCompressor image.PNGCompressor

PNGCompressor is a Processor that compresses images using a png.Encoder with a png.CompressionLevel to compress the given image.

PNGCompressor compresses each Image in a Stack in parallel.

func (PNGCompressor) Process

func (comp PNGCompressor) Process(ctx *ProcessorContext) error

Process runs the PNGCompressor on the given ProcessorContext.

type PostProcessor

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

PostProcessor post-processed Stacks of Galleries.

func NewPostProcessor

func NewPostProcessor(enc image.Encoder, storage media.Storage, galleries Repository) *PostProcessor

NewPostProcessor returns a PostProcessor.

func (*PostProcessor) Process

func (svc *PostProcessor) Process(ctx context.Context, stack Stack, pipe ProcessingPipeline, opts ...ProcessorOption) (Stack, error)

Process calls pipe.Process with the provided Stack.

func (*PostProcessor) Run

func (svc *PostProcessor) Run(
	ctx context.Context,
	bus event.Bus,
	pipe ProcessingPipeline,
	opts ...PostProcessorOption,
) (<-chan error, error)

Run starts the PostProcessor in the background and returns a channel of asynchronous processing errors. PostProcessor runs until ctx is canceled.

type PostProcessorOption

type PostProcessorOption func(*postProcessorConfig)

PostProcessorOption is an option for PostProcessor.Run.

func OnProcessed

func OnProcessed(fn func(Stack, *Gallery)) PostProcessorOption

OnProcessed returns a PostProcessorOption that registers fn as a callback function that is called when a Stack has been processed.

func ProcessorLogger added in v0.1.4

func ProcessorLogger(logger Printer) PostProcessorOption

ProcessorLogger returns a PostProcessorOption that provides the post-processor with a logger.

func ProcessorWorkers

func ProcessorWorkers(workers int) PostProcessorOption

ProcessorWorkers returns a PostProcessorOption that configures the worker count for processing Stacks. Default & minimum workers is 1.

type Printer added in v0.1.4

type Printer interface {
	Print(v ...interface{})
}

Printer is the interface for a logger.

type ProcessingPipeline

type ProcessingPipeline []Processor

A ProcessingPipeline is a collection of Processors that are run sequentially on a given Stack to post-process an image.

func (ProcessingPipeline) Process

func (pipe ProcessingPipeline) Process(
	ctx context.Context,
	stack Stack,
	imageEncoder image.Encoder,
	storage media.Storage,
	opts ...ProcessorOption,
) (Stack, error)

Process calls each Processor in the ProcessingPipeline with a ProcesingContext.

type Processor

type Processor interface {
	Process(*ProcessorContext) error
}

A Processor processes an image through a ProcessorContext.

type ProcessorContext

type ProcessorContext struct {
	context.Context
	// contains filtered or unexported fields
}

ProcessorContext is passed to Processors when they process a Stack. The ProcessorContext provides the Stack and image related functions or services to the Processors.

func (*ProcessorContext) Encode

func (ctx *ProcessorContext) Encode(w io.Writer, img stdimage.Image, format string) error

Encode encodes an image using the appropriate encoder for the specified image format.

func (*ProcessorContext) Encoder

func (ctx *ProcessorContext) Encoder() image.Encoder

Encoder returns the ImageEncoder.

func (*ProcessorContext) Stack

func (ctx *ProcessorContext) Stack() Stack

Stack returns the processed Stack.

func (*ProcessorContext) Storage

func (ctx *ProcessorContext) Storage() media.Storage

Storage returns the media storage.

func (*ProcessorContext) Update

func (ctx *ProcessorContext) Update(fn func(Stack) Stack) error

Update calls fn with the processed Stack and replaces the Stack with the one returned by fn.

Is is illegal to update the UUID of the Stack. ErrStackCorrupted is returned in such cases.

type ProcessorFunc

type ProcessorFunc func(*ProcessorContext) error

ProcessorFunc allows a function to be used as a Processor.

func (ProcessorFunc) Process

func (fn ProcessorFunc) Process(ctx *ProcessorContext) error

Process calls fn(ctx).

type ProcessorOption added in v0.1.4

type ProcessorOption func(*processorConfig)

func WithDebugger added in v0.1.4

func WithDebugger(logger Printer) ProcessorOption

type Repository

type Repository interface {
	// Save saves a Gallery.
	Save(context.Context, *Gallery) error

	// Fetch fetches the Gallery with the given UUID or ErrNotFound.
	Fetch(context.Context, uuid.UUID) (*Gallery, error)

	// Delete deletes a Gallery.
	Delete(context.Context, *Gallery) error

	// Use fetches the Gallery with the given UUID, calls the provided function
	// with that Gallery and saves the Gallery into the repository. If the
	// function returns a non-nil error, the Gallery is not saved and that error
	// is returned.
	Use(ctx context.Context, id uuid.UUID, fn func(*Gallery) error) error
}

Repository handles persistence of Galleries.

func GoesRepository

func GoesRepository(repo aggregate.Repository) Repository

GoesRepository returns a Repository that uses an aggregate.Repository under the hood.

type Resizer

type Resizer image.Resizer

A Resizer is a Processor that resizes the original Image of a Stack into additional dimensions.

func (Resizer) Process

func (r Resizer) Process(ctx *ProcessorContext) error

Process runs the Resizer on the Stack in the given ProcessorContext.

type SortedData added in v0.1.4

type SortedData struct {
	Sorting []uuid.UUID
}

type Stack

type Stack struct {
	ID     uuid.UUID `json:"id"`
	Images []Image   `json:"images"`
}

A Stack represents an image in a gallery. A Stack may have multiple variants of an image.

func (Stack) Original

func (s Stack) Original() Image

Original returns the original image in the Stack.

func (Stack) WithTag

func (s Stack) WithTag(tags ...string) Stack

WithTag adds the given tags to each Image in the Stack and returns the updated Stack. The original Stack is not modified.

func (Stack) WithoutTag

func (s Stack) WithoutTag(tags ...string) Stack

WithoutTag removes the given tags from each Image and returns the updated Stack. The original Stack is not modified.

type StackDeletedData

type StackDeletedData struct {
	Stack Stack
}

type StackRenamedData

type StackRenamedData struct {
	StackID uuid.UUID
	OldName string
	Name    string
}

type StackTaggedData

type StackTaggedData struct {
	StackID uuid.UUID
	Tags    []string
}

type StackUntaggedData

type StackUntaggedData struct {
	StackID uuid.UUID
	Tags    []string
}

type StackUpdatedData

type StackUpdatedData struct {
	Stack Stack
}

type Stacks

type Stacks []Stack

func (Stacks) FindByTags

func (stacks Stacks) FindByTags(tags ...string) Stacks

Directories

Path Synopsis
Package mock_gallery is a generated GoMock package.
Package mock_gallery is a generated GoMock package.

Jump to

Keyboard shortcuts

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