archive

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2022 License: AGPL-3.0 Imports: 12 Imported by: 4

Documentation

Index

Constants

View Source
const (
	MiniatureCachedWidth     = 360  // MiniatureCachedWidth is the minimum size in which images are stored. Under that, the MiniatureCachedWidth is stored and the image will be re-scaled down on the fly
	MediumQualityCachedWidth = 2400 // MediumQualityCachedWidth is the highest cacheable resolution, consumer should not request above that
)
View Source
const DownloadUrlValidityDuration = 5 * time.Minute

Variables

View Source
var (
	NotFoundError      = errors.New("media is not present in the archive")
	MediaOverflowError = errors.New("media at the requested width is bigger that what the consumer can support")
	CacheableWidths    = []int{MediumQualityCachedWidth, MiniatureCachedWidth} // CacheableWidths are the only resolution cached, array must be sorted DESC.

)

Functions

func GetMediaOriginalURL

func GetMediaOriginalURL(owner, mediaId string) (string, error)

GetMediaOriginalURL returns a pre-signed URL to download the content. URL only valid a certain time.

func GetResizedImage

func GetResizedImage(owner, mediaId string, width int, maxBytes int) ([]byte, string, error)

GetResizedImage returns the image in the requested size (or rounded up), and the media type.

func GetResizedImageURL

func GetResizedImageURL(owner, mediaId string, width int) (string, error)

GetResizedImageURL returns a pre-signed URL to download the resized image ; GetResizedImage must have been called before.

func Init

func Init(repository ARepositoryAdapter, store StoreAdapter, cache CacheAdapter, jobQueue AsyncJobAdapter)

func LoadImagesInCache

func LoadImagesInCache(ctx context.Context, images ...*ImageToResize) (int, error)

LoadImagesInCache generates resized images and store them in the cache. Returns how many has been processed

func Relocate

func Relocate(owner string, ids []string, targetFolder string) error

Relocate physically moves media to the target folder

func Store

func Store(request *StoreRequest) (string, error)

Store save the file content, register it, and generates miniature. Return the new filename.

func SupportResize

func SupportResize(filename string) bool

SupportResize will return true if the file format can be resized (and cached)

func WarmUpCacheByFolder

func WarmUpCacheByFolder(owner, missedStoreKey string, width int) error

WarmUpCacheByFolder list medias missing in the cache and load them

Types

type ARepositoryAdapter

type ARepositoryAdapter interface {
	// FindById returns found location (key) of the media, or a NotFoundError
	FindById(owner, id string) (string, error)

	// FindByIds searches multiple physical location at once
	FindByIds(owner string, ids []string) (map[string]string, error)

	// AddLocation adds (or override) the media location with the new key
	AddLocation(owner, id, key string) error

	// UpdateLocations will update or set location for each id
	UpdateLocations(owner string, locations map[string]string) error

	// FindIdsFromKeyPrefix returns a map id -> storeKey
	FindIdsFromKeyPrefix(keyPrefix string) (map[string]string, error)
}

ARepositoryAdapter is storing the mapping between keys in the main storage and the media ids.

type AsyncJobAdapter

type AsyncJobAdapter interface {
	WarmUpCacheByFolder(owner, missedStoreKey string, width int) error

	LoadImagesInCache(images ...*ImageToResize) error
}

AsyncJobAdapter gives an opportunity to detach heavy processes and run them asynchronously

func NewSyncJobAdapter

func NewSyncJobAdapter() AsyncJobAdapter

type Cache

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

func NewCache

func NewCache() *Cache

func (*Cache) GetOrStore

func (c *Cache) GetOrStore(cacheKey string, contentGenerator func() ([]byte, string, error), postProcess func(io.ReadCloser, int, string, error) ([]byte, string, error)) ([]byte, string, error)

GetOrStore will return cached value if present, or call contentGenerator to cache and return its result.

func (*Cache) Store

func (c *Cache) Store(cacheKey string, contentGenerator func() ([]byte, string, error)) error

Store calls and cache the value only if it wasn't already present.

type CacheAdapter

type CacheAdapter interface {
	// Get retrieve the file store at this key, raise a NotFoundError if the key doesn't exist
	Get(key string) (io.ReadCloser, int, string, error)

	// Put stores the content by overriding exiting file if any
	Put(key string, mediaType string, content io.Reader) error

	// SignedURL returns a pre-authorised URL to download the content
	SignedURL(key string, duration time.Duration) (string, error)

	// WalkCacheByPrefix call the observer for each key found in the cache
	WalkCacheByPrefix(prefix string, observer func(string)) error
}

CacheAdapter is the adapter where the re-sized medias are stored (hot storage - not long term safe)

type DestructuredKey

type DestructuredKey struct {
	Prefix string
	Suffix string
}

DestructuredKey indicates the preferred key: Prefix + Suffix. A counter can be added between the 2 to make the name unique.

type ImageToResize

type ImageToResize struct {
	Owner    string                        // Owner is mandatory
	MediaId  string                        // MediaId is mandatory
	StoreKey string                        // StoreKey is optional
	Widths   []int                         // Widths must have at least 1 value
	Open     func() (io.ReadCloser, error) // Open is optional
}

ImageToResize is a request to cache resized image ; optional arguments might be used when processed synchronously but not when queued.

type ResizerAdapter

type ResizerAdapter interface {
	ResizeImage(reader io.Reader, width int, fast bool) ([]byte, string, error)
	ResizeImageAtDifferentWidths(reader io.Reader, width []int) (map[int][]byte, string, error)
}

ResizerAdapter reduces the image weight and dimensions

var (
	ResizerPort ResizerAdapter = image_resize.NewResizer() // ResizerPort can be overrided for testing purpose
)

type StoreAdapter

type StoreAdapter interface {
	// Download retrieves the file store at this key, raise a NotFoundError if the key doesn't exist
	Download(key string) (io.ReadCloser, error)

	// Upload stores online the file and return the final key used
	Upload(values DestructuredKey, content io.Reader) (string, error)

	// Copy copied the file to a different location, without overriding existing file
	Copy(origin string, destination DestructuredKey) (string, error)

	// Delete permanently stored files (certainly after having been moved.
	Delete(locations []string) error

	// SignedURL returns a pre-authorised URL to download the content
	SignedURL(key string, duration time.Duration) (string, error)
}

StoreAdapter is the adapter where the original medias are stored (cool storage - safe for long term)

type StoreRequest

type StoreRequest struct {
	DateTime         time.Time                     // DateTime is used to name the final file
	FolderName       string                        // FolderName is the location where the file must be physically stored
	Id               string                        // Id is the unique identifier from 'catalog'
	Open             func() (io.ReadCloser, error) // Open creates a new reader to this file
	OriginalFilename string                        // OriginalFilename is used to preserve the right extension
	Owner            string                        // Owner is he tenant to which this media belongs
	SignatureSha256  string                        // SignatureSha256 is the hash of file content, mainly used to generate the filename
}

StoreRequest is used to archive a media

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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