image

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2021 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Overview

Package image provides the database.Model implementation for the Image entity.

Index

Constants

This section is empty.

Variables

View Source
var (
	MimeTypeQEMU = "application/x-qemu-disk"

	ErrInvalidScheme = errors.New("invalid url scheme")
)

Functions

func DownloadJobInit added in v1.1.0

func DownloadJobInit(db *sqlx.DB, q queue.Queue, log *log.Logger, store fs.Store) queue.InitFunc

DownloadJobInit returns a callback for initializing a download job with the given database connection and file store.

func InitEvent added in v1.1.0

func InitEvent(dis event.Dispatcher) queue.InitFunc

func LoadRelations

func LoadRelations(loaders *database.Loaders, ii ...*Image) error

LoadRelations loads all of the available relations for the given Image models using the given loaders available.

func Model

func Model(ii []*Image) func(int) database.Model

Model is called along with database.ModelSlice to convert the given slice of Image models to a slice of database.Model interfaces.

func NewDownloadJob added in v1.1.0

func NewDownloadJob(db *sqlx.DB, i *Image, url DownloadURL) (queue.Job, error)

NewDownloadJob creates the underlying Download for the given Image and returns it as a queue.Job for background processing.

Types

type Download added in v1.1.0

type Download struct {
	ID         int64          `db:"id"`
	ImageID    int64          `db:"image_id"`
	Source     DownloadURL    `db:"source"`
	Error      sql.NullString `db:"error"`
	CreatedAt  time.Time      `db:"created_at"`
	StartedAt  sql.NullTime   `db:"started_at"`
	FinishedAt sql.NullTime   `db:"finished_at"`

	User  *user.User `db:"-"`
	Image *Image     `db:"-"`
}

type DownloadJob added in v1.1.0

type DownloadJob struct {
	ID      int64
	ImageID int64
	Source  DownloadURL
	// contains filtered or unexported fields
}

func (*DownloadJob) Name added in v1.1.0

func (d *DownloadJob) Name() string

Name implements the queue.Job interface.

func (*DownloadJob) Perform added in v1.1.0

func (d *DownloadJob) Perform() error

Perform implements the queue.Job interface. This will attempt to download the image from the remote URL. This will fail if the URL does not have a valid scheme such as, http, https, or sftp.

type DownloadStore added in v1.1.0

type DownloadStore struct {
	database.Store

	User  *user.User
	Image *Image
}

func NewDownloadStore added in v1.1.0

func NewDownloadStore(db *sqlx.DB, mm ...database.Model) *DownloadStore

func (*DownloadStore) All added in v1.1.0

func (s *DownloadStore) All(opts ...query.Option) ([]*Download, error)

func (*DownloadStore) Bind added in v1.1.0

func (s *DownloadStore) Bind(mm ...database.Model)

func (*DownloadStore) Get added in v1.1.0

func (s *DownloadStore) Get(opts ...query.Option) (*Download, error)

type DownloadURL added in v1.1.0

type DownloadURL struct {
	*url.URL
}

func (*DownloadURL) GobDecode added in v1.1.0

func (u *DownloadURL) GobDecode(p []byte) error

func (*DownloadURL) GobEncode added in v1.1.0

func (u *DownloadURL) GobEncode() ([]byte, error)

func (*DownloadURL) Scan added in v1.1.0

func (u *DownloadURL) Scan(v interface{}) error

func (*DownloadURL) String added in v1.1.0

func (u *DownloadURL) String() string

func (*DownloadURL) UnmarshalText added in v1.1.0

func (u *DownloadURL) UnmarshalText(b []byte) error

func (*DownloadURL) Validate added in v1.1.0

func (u *DownloadURL) Validate() error

func (DownloadURL) Value added in v1.1.0

func (u DownloadURL) Value() (driver.Value, error)

type Event added in v1.1.0

type Event struct {
	Image  *Image
	Action string
	// contains filtered or unexported fields
}

func (*Event) Name added in v1.1.0

func (ev *Event) Name() string

func (*Event) Perform added in v1.1.0

func (ev *Event) Perform() error

type Form

type Form struct {
	namespace.Resource
	*webutil.File

	Images      *Store      `schema:"-"`
	Name        string      `schema:"name" json:"name"`
	DownloadURL DownloadURL `schema:"download_url" json:"download_url"`
}

Form is the type that represents input data for uploading a new driver image.

func (*Form) Fields

func (f *Form) Fields() map[string]string

Fields returns a map containing the namespace, and name fields from the original Form.

func (*Form) Validate

func (f *Form) Validate() error

Validate checks to see if there is a name for the image, and if that name is valid. This will also check the contents of the uploaded file to make sure it is a valid QCOW2 image file. It does this by checking the first four bytes of the file match the ASCII magic number for QCOW2.

type Image

type Image struct {
	ID          int64         `db:"id"`
	UserID      int64         `db:"user_id"`
	AuthorID    int64         `db:"author_id"`
	NamespaceID sql.NullInt64 `db:"namespace_id"`
	Driver      driver.Type   `db:"driver"`
	Hash        string        `db:"hash"`
	Name        string        `db:"name"`
	CreatedAt   time.Time     `db:"created_at"`

	Author    *user.User           `db:"-" gob:"-"`
	User      *user.User           `db:"-" gob:"-"`
	Download  *Download            `db:"-" gob:"-"`
	Namespace *namespace.Namespace `db:"-" gob:"-"`
}

Image is the type that represents an image that has been uploaded by a user.

func FromContext

func FromContext(ctx context.Context) (*Image, bool)

FromContext returns the Image model from the given context, if any.

func (*Image) Bind

func (i *Image) Bind(mm ...database.Model)

Bind implements the database.Binder interface. This will only bind the model if they are pointers to either user.User or namespace.Namespace.

func (*Image) Downloaded added in v1.1.0

func (i *Image) Downloaded() bool

Downloaded returns whether the image was downloaded successfully. If the image has no associated download, then this returns true.

func (*Image) Endpoint

func (i *Image) Endpoint(uri ...string) string

Endpoint implements the database.Model interface. This will return the endpoint to the current Image model.

func (*Image) IsZero

func (i *Image) IsZero() bool

IsZero implements the database.Model interface.

func (*Image) JSON

func (i *Image) JSON(addr string) map[string]interface{}

JSON implements the database.Model interface. This will return a map with the current Image values under each key. If any of the User, or Namespace bound models exist on the Image, then the JSON representation of these models will be in the returned map, under the user, and namespace keys respectively.

func (*Image) Primary

func (i *Image) Primary() (string, int64)

Primary implements the database.Model interface.

func (*Image) SetPrimary

func (i *Image) SetPrimary(id int64)

SetPrimary implements the database.Model interface.

func (*Image) Values

func (i *Image) Values() map[string]interface{}

Values implements the database.Model interface. This will return a map with the following values, user_id, namespace_id, driver, hash, name, and created_at.

type Store

type Store struct {
	database.Store

	// User is the bound user.User model. If not nil this will bind the
	// user.User model to any Image models that are created. If not nil this
	// will append a WHERE clause on the user_id column for all SELECT queries
	// performed.
	User *user.User

	// Namespace is the bound namespace.Namespace model. If not nil this will
	// bind the namespace.Namespace model to any Image models that are created.
	// If not nil this will append a WHERE clause on the namespace_id column for
	// all SELECT queries performed.
	Namespace *namespace.Namespace
	// contains filtered or unexported fields
}

Store is the type for creating and modifying Image models in the database. The Store type can have an underlying fs.Store implementation that is used for storing the contents of an image.

func NewStore

func NewStore(db *sqlx.DB, mm ...database.Model) *Store

NewStore returns a new Store for querying the images table. Each model passed to this function will be bound to the returned Store.

func NewStoreWithBlockStore

func NewStoreWithBlockStore(db *sqlx.DB, blockStore fs.Store, mm ...database.Model) *Store

NewStoreWithBlockStore is functionally the same as NewStore, however it sets the fs.Store to use on the returned Store. This will allow for an image file to be stored.

func (*Store) All

func (s *Store) All(opts ...query.Option) ([]*Image, error)

All returns a slice of Image models, applying each query.Option that is given.

func (*Store) Bind

func (s *Store) Bind(mm ...database.Model)

Bind implements the database.Binder interface. This will only bind the model if they are pointers to either user.User or namespace.Namespace.

func (*Store) Chown

func (s *Store) Chown(from, to int64) error

func (*Store) Create

func (s *Store) Create(authorId int64, hash, name string, t driver.Type, r io.Reader) (*Image, error)

Create creates a new image with the given name for the given driver.Type. The given io.Reader is used to copy the contents of the image to the underlying fs.Store. It is expected for the Store to have a fs.Store set on it, otherwise it will error.

func (*Store) Delete

func (s *Store) Delete(id int64, t driver.Type, hash string) error

Delete deletes the given Image from the database, and removes the underlying image file. It is expected for the Store to have a fs.Store set on it, otherwise it will error.

func (*Store) Get

func (s *Store) Get(opts ...query.Option) (*Image, error)

Get returns a single Image model, applying each query.Option that is given.

func (*Store) Index

func (s *Store) Index(vals url.Values, opts ...query.Option) ([]*Image, database.Paginator, error)

Index returns the paginated results from the images table depending on the values that are present in url.Values. Detailed below are the values that are used from the given url.Values,

search - This applies the database.Search query.Option using the value of name

func (*Store) Load

func (s *Store) Load(key string, vals []interface{}, fn database.LoaderFunc) error

Load loads in a slice of Image models where the given key is in the list of given vals. Each database is loaded individually via a call to the given load callback. This method calls Store.All under the hood, so any bound models will impact the models being loaded.

func (*Store) New

func (s *Store) New() *Image

New returns a new Image binding any non-nil models to it from the current Store.

func (*Store) Paginate

func (s *Store) Paginate(page int64, opts ...query.Option) (database.Paginator, error)

Paginate returns the database.Paginator for the images table for the given page.

func (*Store) SetBlockStore added in v1.1.0

func (s *Store) SetBlockStore(store fs.Store)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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