media

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2021 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMimeType is returned by validate when a mimetype is
	// not permitted.
	ErrMimeType = errors.New("mimetype is not permitted")
	// ErrFileTooBig is returned by validate when a file is to
	// big to be uploaded.
	ErrFileTooBig = errors.New("file size to big to be uploaded")
)

Functions

This section is empty.

Types

type ExistsFunc

type ExistsFunc func(fileName string) bool

ExistsFunc is used by the uploader to determine if a media item exists in the library.

type Imager

type Imager interface {
	Decode() (image.Image, error)
	Save(image image.Image, path string, comp int) error
}

Imager describes the methods for decoding and saving different image types such as PNG's and JPG's

type JPG

type JPG struct {
	File        multipart.File
	Compression int
}

JPG implementation of an Imager.

func (*JPG) Decode

func (j *JPG) Decode() (image.Image, error)

Decode

Seeks the file and decodes the file into a new PNG type Returns errors.INTERNAL if there was an error decoding.

func (*JPG) Save

func (j *JPG) Save(img image.Image, path string, comp int) error

Save

Accepts an image.Image, a path for where to save the image and a compression level. Returns errors.INTERNAL if the file could not be saved.

type Library

type Library interface {
	Upload(file *multipart.FileHeader) (domain.Media, error)
	Serve(media domain.Media, path string, acceptWebP bool) ([]byte, domain.Mime, error)
	Validate(file *multipart.FileHeader) error
	Delete(item domain.Media)
}

Library defines methods for media items to save, validate and delete from the local file system.

type PNG

type PNG struct {
	File multipart.File
}

PNG implementation of an Imager.

func (*PNG) Decode

func (p *PNG) Decode() (image.Image, error)

Decode

Seeks the file and decodes the file into a new PNG type Returns errors.INTERNAL if there was an error decoding.

func (*PNG) Save

func (p *PNG) Save(img image.Image, path string, comp int) error

Save

Accepts an image.Image, a path for where to save the image and a compression level. Returns errors.INTERNAL if the file could not be saved.

type Resize

type Resize struct{}

Resize implements the Resizer interface.

func (*Resize) Resize

func (i *Resize) Resize(imager Imager, dest string, media domain.MediaSize, comp int) error

Resize satisfies the Resizer by decoding, cropping and resizing and finally saving the resized image.

type Resizer

type Resizer interface {
	Resize(imager Imager, dest string, media domain.MediaSize, comp int) error
}

Resizer describes the method for resizing images for the library.

type Service

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

Service

Defines the service for uploading, validating, deleting and serving rich media from the Verbis media library.

func New

func New(opts *domain.Options, fn ExistsFunc) *Service

New

Creates a new Service.

func (*Service) Delete

func (s *Service) Delete(item domain.Media)

Delete

Satisfies the Library to remove possible media item combinations from the file system, if the file does not exist (user moved) it will be skipped.

Logs errors.INTERNAL if the file could not be deleted.

func (*Service) Serve

func (s *Service) Serve(media domain.Media, path string, acceptWebP bool) ([]byte, domain.Mime, error)

Serve

Serve is responsible for serving the correct data to the front end. Returns errors.NOTFOUND if the media item was not found.

func (*Service) Upload

func (s *Service) Upload(file *multipart.FileHeader) (domain.Media, error)

Upload

Satisfies the Library to upload a media item to the library. Media items will be opened and saved to the local file system. Images are resized and saved in correspondence to the options. This function expects that validate has been called before it is run.

Returns errors.INTERNAL on any eventuality the file could not be opened. Returns errors.INVALID if the mimetype could not be found.

func (*Service) Validate

func (s *Service) Validate(file *multipart.FileHeader) error

Validate

Satisfies the Library to see if the media item passed is valid. It will check if the file is a valid mime type, if the file size is less than the size specified in the options and finally checks the image boundaries.

Returns errors.INVALID any of the conditions fail.

Jump to

Keyboard shortcuts

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