storage

package
v0.0.0-...-6237511 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteHandlerFunc

func DeleteHandlerFunc(storage Storage, repository FileRepository) http.HandlerFunc

DeleteHandlerFunc handles deletion request

func DownloadHandlerFunc

func DownloadHandlerFunc(storage Storage, cache Cache, resizer image.Resizer, repository FileRepository) http.HandlerFunc

DownloadHandlerFunc handles downloading request

func UploadHandlerFunc

func UploadHandlerFunc(storage Storage, repository FileRepository) http.HandlerFunc

UpdateHandlerFunc handles uploading request

Types

type Cache

type Cache interface {
	Exists(path string) bool
	Retrieve(path string) (io.ReadCloser, error)
	Store(body io.Reader, path string) error
}

Cache uses to storing or retrieving files from hidden or inaccessible place

type DiskCache

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

DiskCache implements Cache interface with local disk storage

func NewDiskCache

func NewDiskCache(fs afero.Fs, filePath string) (*DiskCache, error)

NewDiskCache returns a new DiskCache instance

func (*DiskCache) Close

func (c *DiskCache) Close()

Close closes the doneCh which will breaking an infinite-loop file deletion function

func (DiskCache) Delete

func (c DiskCache) Delete(path string) error

Delete deletes a single file or directory from the give path

func (DiskCache) Exists

func (c DiskCache) Exists(path string) bool

Exists checks if a file or directory exists

func (DiskCache) Retrieve

func (c DiskCache) Retrieve(path string) (io.ReadCloser, error)

Retrieve returns a single file content from the given path

func (DiskCache) Store

func (c DiskCache) Store(body io.Reader, path string) error

Store writes file content to the given path

type File

type File struct {
	// Identifier of the file
	ID primitive.ObjectID `bson:"_id" json:"id" graphql:"-"`

	// An uploaded file path
	Path string `bson:"path" json:"path" graphql:"path"`

	// An original file name
	FileName string `bson:"fileName" json:"fileName" graphql:"fileName"`

	// Valid URL string composes with file name and ID
	Slug string `bson:"slug" json:"slug" graphql:"slug"`

	// An optional field #1 for using in some storage server
	OptionalField1 string `bson:"optionalField1" json:"optionalField1,omitempty" graphql:"optionalField1"`

	// An optional field #2 for using in some storage server
	OptionalField2 string `bson:"optionalField2" json:"optionalField2,omitempty" graphql:"optionalField2"`

	// An optional field #3 for using in some storage server
	OptionalField3 string `bson:"optionalField3" json:"optionalField3,omitempty" graphql:"optionalField3"`

	// Date-time that the file was created
	CreatedAt time.Time `bson:"createdAt" json:"createdAt" graphql:"createdAt"`

	// Date-time that the file was updated
	UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt" graphql:"updatedAt"`
}

File is an uploaded object on the storage server

func (File) MarshalJSON

func (f File) MarshalJSON() ([]byte, error)

MarshalJSON is a custom JSON marshaling function of file entity

type FileRepository

type FileRepository interface {
	Create(ctx context.Context, file File) (File, error)
	Delete(ctx context.Context, id interface{}) error
	FindAllByIDs(ctx context.Context, ids interface{}) ([]File, error)
	FindByID(ctx context.Context, id interface{}) (File, error)
}

A fileRepository interface

func NewFileRepository

func NewFileRepository(db mongo.Database) FileRepository

NewFileRepository returns a MongoFileRepository instance

type MongoFileRepository

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

MongoFileRepository implements fileRepository on MongoDB

func (MongoFileRepository) Create

func (repo MongoFileRepository) Create(ctx context.Context, file File) (File, error)

Create inserts a new file record whether exist or not

func (MongoFileRepository) Delete

func (repo MongoFileRepository) Delete(ctx context.Context, id interface{}) error

Delete performs deletion a file record by its ID

func (MongoFileRepository) FindAllByIDs

func (repo MongoFileRepository) FindAllByIDs(ctx context.Context, ids interface{}) ([]File, error)

FindAllByIDs returns list of files from list of IDs

func (MongoFileRepository) FindByID

func (repo MongoFileRepository) FindByID(ctx context.Context, id interface{}) (File, error)

FindByID returns a single file from its ID

type Slug

type Slug string

Slug is a valid URL string composes with file name and ID

func (Slug) GetID

func (s Slug) GetID() (interface{}, error)

GetID returns an ID from the slug string

func (Slug) MustGetID

func (s Slug) MustGetID() interface{}

MustGetID always return ID from the slug string

type Storage

type Storage interface {
	Delete(ctx context.Context, path string) error
	Download(ctx context.Context, path string) (io.ReadCloser, error)
	Upload(ctx context.Context, body io.Reader, path string) error
}

Storage uses to storing or retrieving file from cloud or remote server

Directories

Path Synopsis
Package mock_storage is a generated GoMock package.
Package mock_storage is a generated GoMock package.

Jump to

Keyboard shortcuts

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