Documentation ¶
Index ¶
- Constants
- Variables
- func GetMediaOriginalURL(owner, mediaId string) (string, error)
- func GetResizedImage(owner, mediaId string, width int, maxBytes int) ([]byte, string, error)
- func GetResizedImageURL(owner, mediaId string, width int) (string, error)
- func Init(repository ARepositoryAdapter, store StoreAdapter, cache CacheAdapter, ...)
- func LoadImagesInCache(ctx context.Context, images ...*ImageToResize) (int, error)
- func Relocate(owner string, ids []string, targetFolder string) error
- func Store(request *StoreRequest) (string, error)
- func SupportResize(filename string) bool
- func WarmUpCacheByFolder(owner, missedStoreKey string, width int) error
- type ARepositoryAdapter
- type AsyncJobAdapter
- type Cache
- type CacheAdapter
- type DestructuredKey
- type ImageToResize
- type ResizerAdapter
- type StoreAdapter
- type StoreRequest
Constants ¶
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 )
const DownloadUrlValidityDuration = 5 * time.Minute
Variables ¶
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 ¶
GetMediaOriginalURL returns a pre-signed URL to download the content. URL only valid a certain time.
func GetResizedImage ¶
GetResizedImage returns the image in the requested size (or rounded up), and the media type.
func GetResizedImageURL ¶
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 analysiscache. Returns how many has been processed
func Store ¶
func Store(request *StoreRequest) (string, error)
Store save the file content, register it, and generates miniature. Return the new filename.
func SupportResize ¶
SupportResize will return true if the file format can be resized (and cached)
func WarmUpCacheByFolder ¶
WarmUpCacheByFolder list medias missing in the analysiscache 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 (*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 analysiscache and return its result.
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 analysiscache 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 ¶
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 analysiscache 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