image
v1.1.1
Documentation
pkg.go.dev ↗Package image provides the database.Model implementation for the Image entity.
Variables
MimeTypeQEMU (2 declarations)
var (
MimeTypeQEMU = "application/x-qemu-disk"
ErrInvalidScheme = errors.New("invalid url scheme")
)
Functions
DownloadJobInit
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.
InitEvent
func InitEvent(dis event.Dispatcher) queue.InitFunc
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.
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.
NewDownloadJob
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
Download
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:"-"`
}
DownloadJob
type DownloadJob struct {
ID int64
ImageID int64
Source DownloadURL
// contains filtered or unexported fields
}
DownloadJob.Name
func (d *DownloadJob) Name() string
Name implements the queue.Job interface.
DownloadJob.Perform
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.
DownloadStore
type DownloadStore struct {
database.Store
User *user.User
Image *Image
}
NewDownloadStore
func NewDownloadStore(db *sqlx.DB, mm ...database.Model) *DownloadStore
DownloadStore.All
func (s *DownloadStore) All(opts ...query.Option) ([]*Download, error)
DownloadStore.Bind
func (s *DownloadStore) Bind(mm ...database.Model)
DownloadStore.Get
func (s *DownloadStore) Get(opts ...query.Option) (*Download, error)
DownloadURL
type DownloadURL struct {
*url.URL
}
DownloadURL.GobDecode
func (u *DownloadURL) GobDecode(p []byte) error
DownloadURL.GobEncode
func (u *DownloadURL) GobEncode() ([]byte, error)
DownloadURL.Scan
func (u *DownloadURL) Scan(v interface{}) error
DownloadURL.String
func (u *DownloadURL) String() string
DownloadURL.UnmarshalText
func (u *DownloadURL) UnmarshalText(b []byte) error
DownloadURL.Validate
func (u *DownloadURL) Validate() error
DownloadURL.Value
func (u DownloadURL) Value() (driver.Value, error)
Event
type Event struct {
Image *Image
Action string
// contains filtered or unexported fields
}
Event.Name
func (ev *Event) Name() string
Event.Perform
func (ev *Event) Perform() error
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.
Form.Fields
func (f *Form) Fields() map[string]string
Fields returns a map containing the namespace, and name fields from the original Form.
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.
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.
FromContext
func FromContext(ctx context.Context) (*Image, bool)
FromContext returns the Image model from the given context, if any.
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.
Image.Downloaded
func (i *Image) Downloaded() bool
Downloaded returns whether the image was downloaded successfully. If the image has no associated download, then this returns true.
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.
Image.IsZero
func (i *Image) IsZero() bool
IsZero implements the database.Model interface.
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.
Image.Primary
func (i *Image) Primary() (string, int64)
Primary implements the database.Model interface.
Image.SetPrimary
func (i *Image) SetPrimary(id int64)
SetPrimary implements the database.Model interface.
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.
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.
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.
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.
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.
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.
Store.Chown
func (s *Store) Chown(from, to int64) error
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.
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.
Store.Get
func (s *Store) Get(opts ...query.Option) (*Image, error)
Get returns a single Image model, applying each query.Option that is given.
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
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.
Store.New
func (s *Store) New() *Image
New returns a new Image binding any non-nil models to it from the current Store.
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.
Store.SetBlockStore
func (s *Store) SetBlockStore(store fs.Store)