image

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2020 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package image handles storing, resizing and retrieval of images Provides Store with Save and Load and one implementation on top of local file system. Service object encloses Store and add common methods, this is the one consumer should use

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bolt added in v1.5.0

type Bolt struct {
	MaxSize   int
	MaxHeight int
	MaxWidth  int
	// contains filtered or unexported fields
}

Bolt provides image Store for images keeping data in bolt DB, restricts max size. It uses 3 buckets to manage images data. Two buckets contains image data (staged and committed images). Third bucket holds insertion timestamps.

func NewBoltStorage added in v1.5.0

func NewBoltStorage(fileName string, maxSize int, maxHeight int, maxWidth int, options bolt.Options) (*Bolt, error)

Create Bolt Store.

func (*Bolt) Cleanup added in v1.5.0

func (b *Bolt) Cleanup(ctx context.Context, ttl time.Duration) error

Cleanup runs scan of staging and removes old data based on ttl

func (*Bolt) Commit added in v1.5.0

func (b *Bolt) Commit(id string) error

Commit file stored in staging bucket by copying it to permanent bucket Data from staging bucket not removed immediately, but would be removed on cleanup

func (*Bolt) Load added in v1.5.0

func (b *Bolt) Load(id string) (io.ReadCloser, int64, error)

Load image from DB returns ReadCloser and caller should call close after processing completed.

func (*Bolt) Save added in v1.5.0

func (b *Bolt) Save(fileName string, userID string, r io.Reader) (id string, err error)

Save data from reader to staging bucket in DB

func (*Bolt) SaveWithID added in v1.5.0

func (b *Bolt) SaveWithID(id string, r io.Reader) (string, error)

SaveWithID saves data from reader with given id

func (*Bolt) SizeLimit added in v1.5.0

func (b *Bolt) SizeLimit() int

SizeLimit returns max size of allowed image

type FileSystem

type FileSystem struct {
	Location   string
	Staging    string
	MaxSize    int
	Partitions int
	MaxHeight  int
	MaxWidth   int
	// contains filtered or unexported fields
}

FileSystem provides image Store for local files. Saves and loads files from Location, restricts max size.

func (*FileSystem) Cleanup

func (f *FileSystem) Cleanup(ctx context.Context, ttl time.Duration) error

Cleanup runs scan of staging and removes old files based on ttl

func (*FileSystem) Commit

func (f *FileSystem) Commit(id string) error

Commit file stored in staging location by moving it to permanent location

func (*FileSystem) Load

func (f *FileSystem) Load(id string) (io.ReadCloser, int64, error)

Load image from FS. Uses id to get partition subdirectory. returns ReadCloser and caller should call close after processing completed.

func (*FileSystem) Save

func (f *FileSystem) Save(fileName string, userID string, r io.Reader) (id string, err error)

Save data from reader for given file name to local FS, staging directory. Returns id as user/uuid Files partitioned across multiple subdirectories and the final path includes part, i.e. /location/user1/03/123-4567

func (*FileSystem) SaveWithID added in v1.5.0

func (f *FileSystem) SaveWithID(id string, r io.Reader) (string, error)

SaveWithID saves data from reader with given id

func (*FileSystem) SizeLimit

func (f *FileSystem) SizeLimit() int

SizeLimit returns max size of allowed image

type MockStore

type MockStore struct {
	mock.Mock
}

MockStore is an autogenerated mock type for the Store type

func (*MockStore) Cleanup

func (_m *MockStore) Cleanup(ctx context.Context, ttl time.Duration) error

Cleanup provides a mock function with given fields: ctx, ttl

func (*MockStore) Commit

func (_m *MockStore) Commit(id string) error

Commit provides a mock function with given fields: id

func (*MockStore) Load

func (_m *MockStore) Load(id string) (io.ReadCloser, int64, error)

Load provides a mock function with given fields: id

func (*MockStore) Save

func (_m *MockStore) Save(fileName string, userID string, r io.Reader) (string, error)

Save provides a mock function with given fields: fileName, userID, r

func (*MockStore) SaveWithID added in v1.5.0

func (_m *MockStore) SaveWithID(id string, r io.Reader) (string, error)

SaveWithID provides a mock function with given fields: id, r

func (*MockStore) SizeLimit

func (_m *MockStore) SizeLimit() int

SizeLimit provides a mock function with given fields:

type Service

type Service struct {
	Store
	TTL      time.Duration // for how long file allowed on staging
	ImageAPI string        // image api matching path
	// contains filtered or unexported fields
}

Service extends Store with common functions needed for any store implementation

func (*Service) Cleanup

func (s *Service) Cleanup(ctx context.Context)

Cleanup runs periodic cleanup with TTL. Blocking loop, should be called inside of goroutine by consumer

func (*Service) Close

func (s *Service) Close()

Close flushes all in-progress submits and enforces waiting commits

func (*Service) ExtractPictures

func (s *Service) ExtractPictures(commentHTML string) (ids []string, err error)

ExtractPictures gets list of images from the doc html and convert from urls to ids, i.e. user/pic.png

func (*Service) Submit

func (s *Service) Submit(idsFn func() []string)

Submit multiple ids via function for delayed commit

type Store

type Store interface {
	SaveWithID(id string, r io.Reader) (string, error)
	Save(fileName string, userID string, r io.Reader) (id string, err error) // get name and reader and returns ID of stored image
	Commit(id string) error                                                  // move image from staging to permanent
	Load(id string) (io.ReadCloser, int64, error)                            // load image by ID. Caller has to close the reader.
	Cleanup(ctx context.Context, ttl time.Duration) error                    // run removal loop for old images on staging
	SizeLimit() int                                                          // max image size
}

Store defines interface for saving and loading pictures. Declares two-stage save with commit

Jump to

Keyboard shortcuts

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