store

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MPL-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ReasonBrokenImage                 = "ErrBrokenImage"
	ReasonUnsupportedImageType        = "ErrUnsupportedImageType"
	ReasonUnsupportedSVGFile          = "ErrUnsupportedSVGFile"
	ReasonDownloadFileFailed          = "ErrDownloadFileFailed"
	ReasonFileSizeTooLarge            = "ErrSizeTooLarge"
	ReasonUnknownCloudflareAPIFailure = "ErrUnknownCloudflareAPIFailure"
)

Reason keys for unsupported errors

View Source
const CloudflareImageDeilverURL = "https://imagedelivery.net/%s/%s/public"
View Source
const ImageSizeThreshold = 10 * 1024 * 1024 // 10MB

Variables

View Source
var ImageCachingErrorReasons = map[string]string{
	ReasonBrokenImage:                 "broken image",
	ReasonUnsupportedImageType:        "unsupported image type",
	ReasonUnsupportedSVGFile:          "unsupported SVG File",
	ReasonDownloadFileFailed:          "download file error",
	ReasonFileSizeTooLarge:            "size too large",
	ReasonUnknownCloudflareAPIFailure: "unknown cloudflare api error",
}

Functions

func IsSupportedImageType

func IsSupportedImageType(mimeType string) bool

IsSupportedImageType validates if an image is supported

func NewImageCachingError

func NewImageCachingError(reason string) error

NewImageCachingError returns ImageCachingError if a reson is given. Otherwise, it returns a regular error.

func NewUnsupportedSVG

func NewUnsupportedSVG(url string) error

Types

type ImageCachingError

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

func (*ImageCachingError) Error

func (e *ImageCachingError) Error() string

func (*ImageCachingError) Reason

func (e *ImageCachingError) Reason() string

type ImageMetadata

type ImageMetadata struct {
	AssetID string `json:"assetID" gorm:"index:image_asset_id,unique"`
	ImageID string `json:"fileID"`

	CreatedAt time.Time
	UpdatedAt time.Time
}

func (ImageMetadata) TableName

func (p ImageMetadata) TableName() string

type ImageReader

type ImageReader interface {
	Read() (io.Reader, string, int, error)
}

type ImageStore

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

func New

func New(dsn string, cloudflareAccountHash, cloudflareAccountID, cloudflareAPIToken string) *ImageStore

func (*ImageStore) AutoMigrate

func (s *ImageStore) AutoMigrate() error

func (*ImageStore) CreateOrGetImage

func (s *ImageStore) CreateOrGetImage(ctx context.Context, assetID string) (ImageMetadata, error)

CreateOrGetImage creates an image record in db if it is not existed. Otherwise, returns the existed one

func (*ImageStore) GetImage

func (s *ImageStore) GetImage(ctx context.Context, assetID string) (ImageMetadata, error)

GetImage returns an image metadata object

func (*ImageStore) UploadImage

func (s *ImageStore) UploadImage(ctx context.Context, assetID string, imageReader ImageReader, metadata map[string]interface{}) (ImageMetadata, error)

UploadImage creates a db transaction to download and upload an image to cloudflare. After an image is successfully uploaded to cloudflare, it updates the returned image id into image store. It locks an image record for updating which prevents from duplicated download precess The additional metadata will be attached to the image file when we upload it to cloudflare.

type Metadata

type Metadata map[string]interface{}

type UnsupportedImageCachingError

type UnsupportedImageCachingError interface {
	Error() string
	Reason() string
}

type UnsupportedSVGError

type UnsupportedSVGError struct {
	URL string
}

func (*UnsupportedSVGError) Error

func (e *UnsupportedSVGError) Error() string

func (*UnsupportedSVGError) Reason

func (e *UnsupportedSVGError) Reason() string

Jump to

Keyboard shortcuts

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