image

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: MIT Imports: 33 Imported by: 0

Documentation

Overview

Package image handles storing, resizing and retrieval of images Provides Store with Save and Load implementations on top of local file system and bolt db. 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

func CachedImgID added in v1.6.0

func CachedImgID(imgURL string) (string, error)

CachedImgID generates ID for a cached image. ID would look like: "cached_images/<sha1-of-image-url-hostname>-<sha1-of-image-entire-url>" <sha1-of-image-url-hostname> - would allow us to identify all images from particular site if ever needed <sha1-of-image-entire-url> - would allow us to avoid storing duplicates of the same image

(as accurate as deduplication based on potentially mutable url can be)

func Sha1Str added in v1.6.0

func Sha1Str(s string) string

Sha1Str converts provided string to sha1

Types

type Bolt added in v1.5.0

type Bolt struct {
	// 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, options bolt.Options) (*Bolt, error)

NewBoltStorage create bolt image store

func (*Bolt) Cleanup added in v1.5.0

func (b *Bolt) Cleanup(_ 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) Info added in v1.6.0

func (b *Bolt) Info() (StoreInfo, error)

Info returns meta information about storage

func (*Bolt) Load added in v1.5.0

func (b *Bolt) Load(id string) ([]byte, error)

Load image from DB

func (*Bolt) Save added in v1.5.0

func (b *Bolt) Save(id string, img []byte) error

Save saves image for given id to staging bucket in DB

type FileSystem

type FileSystem struct {
	Location   string
	Staging    string
	Partitions 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(_ 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) Info added in v1.6.0

func (f *FileSystem) Info() (StoreInfo, error)

Info returns meta information about storage

func (*FileSystem) Load

func (f *FileSystem) Load(id string) ([]byte, error)

Load image from FS. Uses id to get partition subdirectory.

func (*FileSystem) Save

func (f *FileSystem) Save(id string, img []byte) error

Save saves image with given id to local FS, staging directory. Files partitioned across multiple subdirectories, and the final path includes part, i.e. /location/user1/03/123-4567

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) Info added in v1.6.0

func (_m *MockStore) Info() (StoreInfo, error)

Info provides a mock function with given fields:

func (*MockStore) Load

func (_m *MockStore) Load(id string) ([]byte, error)

Load provides a mock function with given fields: id

func (*MockStore) Save

func (_m *MockStore) Save(id string, img []byte) error

Save provides a mock function with given fields: id, img

type RPC added in v1.6.0

type RPC struct {
	jrpc.Client
}

RPC implements remote engine and delegates all Calls to remote http server

func (*RPC) Cleanup added in v1.6.0

func (r *RPC) Cleanup(_ context.Context, ttl time.Duration) error

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

func (*RPC) Commit added in v1.6.0

func (r *RPC) Commit(id string) error

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

func (*RPC) Info added in v1.6.0

func (r *RPC) Info() (StoreInfo, error)

Info returns meta information about storage

func (*RPC) Load added in v1.6.0

func (r *RPC) Load(id string) ([]byte, error)

Load image with given id

func (*RPC) Save added in v1.6.0

func (r *RPC) Save(id string, img []byte) error

Save saves image with given id to staging.

type Service

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

Service wraps Store with common functions needed for any store implementation It also provides async Submit with func param retrieving all submitting ids. Submitted ids committed (i.e. moved from staging to final) on commitTTL expiration.

func NewService added in v1.6.0

func NewService(s Store, p ServiceParams) *Service

NewService returns new Service instance

func (*Service) Cleanup

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

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

func (*Service) Close

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

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) ImgContentType added in v1.6.0

func (s *Service) ImgContentType(img []byte) string

ImgContentType returns content type for provided image

func (*Service) Info added in v1.6.0

func (s *Service) Info() (StoreInfo, error)

Info returns meta information about storage

func (*Service) Load added in v1.6.0

func (s *Service) Load(id string) ([]byte, error)

Load wraps storage Load function.

func (*Service) Save added in v1.6.0

func (s *Service) Save(userID string, r io.Reader) (id string, err error)

Save wraps storage Save function, validating and resizing the image before calling it.

func (*Service) SaveWithID added in v1.6.0

func (s *Service) SaveWithID(id string, r io.Reader) error

SaveWithID wraps storage Save function, validating and resizing the image before calling it.

func (*Service) Submit

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

Submit multiple ids via function for delayed commit

type ServiceParams added in v1.6.0

type ServiceParams struct {
	EditDuration time.Duration // edit period for comments
	ImageAPI     string        // image api matching path
	ProxyAPI     string        // proxy api matching path
	MaxSize      int
	MaxHeight    int
	MaxWidth     int
	// contains filtered or unexported fields
}

ServiceParams contains externally adjustable parameters of Service

type Store

type Store interface {
	Info() (StoreInfo, error)         // get meta information about storage
	Save(id string, img []byte) error // store image with passed id to staging
	Load(id string) ([]byte, error)   // load image by ID

	Commit(id string) error                               // move image from staging to permanent
	Cleanup(ctx context.Context, ttl time.Duration) error // run removal loop for old images on staging
}

Store defines interface for saving and loading pictures. Declares two-stage save with Commit. Save stores to staging area and Commit moves to the final location. Two-stage commit scheme is used for not storing images which are uploaded but later never used in the comments, e.g. when somebody uploaded a picture but did not sent the comment.

type StoreInfo added in v1.6.0

type StoreInfo struct {
	FirstStagingImageTS time.Time
}

StoreInfo contains image store meta information

Jump to

Keyboard shortcuts

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