gofm

package module
v0.0.1-20220707-0001 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: MIT Imports: 15 Imported by: 1

README

golang filemanager

work in progress...nothing yet done!!

The File Manager instance only isolates the management of the files by storing the metadata of the files in separate tables

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFmDatabaseClientError             = errors.New("file manager database client error")        // TODO: should be renamed or adapted
	ErrFmDBClientQueryFailed             = errors.New("file manager database client query failed") // TODO: should be renamed or adapted
	ErrFmFileNotFound                    = errors.New("file manager file not found")
	ErrFmNewFileLocationMissing          = errors.New("file manager file location missing")
	ErrFmNoInputFile                     = errors.New("file manager no input file")
	ErrFileModelShouldBePointerOfAStruct = errors.New("file manager file model should be pointer of a struct")
)
View Source
var (
	// DBFilesTableName -> can be changed as needed...
	DBFilesTableName = "fm_files"
)

Functions

func GetFilesDBTableName

func GetFilesDBTableName() string

Types

type DriverFileInterface

type DriverFileInterface interface {
	Save()
	Copy()
	Move()
	Delete()
	Create()
	Touch()
	List()
}

type File

type File struct {
	// InstanceName, partitioning can be setup!
	FMInstance string `gorm:"primaryKey;size:50;not null;<-:create"`
	ID         UUID   `gorm:"primaryKey;not null;<-:create"`

	// File Name
	Name string `gorm:"size:256"`
	// FullName contains the Name+Extension
	FullName string `gorm:"size:256"`
	// It's the FullName of the file but containing the original name of it!
	OriginalName string `gorm:"size:256;<-:create"`

	// File Description
	Description string `gorm:"size:256"`
	// Size Bytes
	Size int64 `gorm:"<-:create"`
	// CategoryID -> also an optional field
	CategoryID *uuid.UUID
	// Extension -> xls, doc etc...
	Extension string `gorm:"size:30;<-:create"`
	// ContentType -> application/json, application/text, etc...
	ContentType string `gorm:"size:50;<-:create"`

	// these are the locations where the file is stored
	// there can be multiple ones, having as a backup option or for read performance...
	//Locations Locations
	Locations FileLocationsMeta

	FileCreatedAt *time.Time `gorm:"null;index:idx_core_dates;<-:create"`
	FileUpdatedAt *time.Time `gorm:"null;index:idx_core_dates"`

	CreatedAt *time.Time `gorm:"null;index:idx_core_dates;<-:create"`
	UpdatedAt *time.Time `gorm:"null;index:idx_core_dates"`
	DeletedAt *time.Time `gorm:"null;index:idx_core_dates"`

	CreatedByID *uuid.UUID `gorm:"null;<-:create"`
	UpdatedByID *uuid.UUID `gorm:"null"`
	DeletedByID *uuid.UUID `gorm:"null"`
	// contains filtered or unexported fields
}

func (*File) Copy

func (f *File) Copy()

Copy -> copies the current file from current location to another location (destination)

func (*File) Delete

func (f *File) Delete()

Delete - deletes the current file from all available locations

func (*File) Get

func (f *File) Get() gofileDriver.FileInterface

func (*File) GetFromLoc

func (f *File) GetFromLoc(locName string) (gofileDriver.FileInterface, bool)

ReadFromLoc -> sa citeasca din anumita locatie...

func (*File) GetTmpFilePath

func (f *File) GetTmpFilePath() (string, error)

GetTmpFilePath -> it will return a tmp file path which after it will be auto destroyed

func (*File) Move

func (f *File) Move()

Move - moves the current file's location to another location (destination)

func (*File) Read

func (f *File) Read() ([]byte, error)

func (*File) Save

func (f *File) Save() (*File, error)

func (*File) SaveAs

func (f *File) SaveAs()

SaveAs - creates a clone of current file having a new ID

func (*File) SetDefaultFile

func (f *File) SetDefaultFile(file gofileDriver.FileInterface)

func (*File) SetFileManager

func (f *File) SetFileManager(fm *FileManager)

func (*File) SetFilesIndexed

func (f *File) SetFilesIndexed(files map[LocationName]gofileDriver.FileInterface)

func (*File) SetLocationsIndexed

func (f *File) SetLocationsIndexed(locations map[LocationName]FileLocationMeta)

func (*File) TableName

func (f *File) TableName() string

TableName -> Get the Database table name from the file manager https://gorm.io/docs/conventions.html -> NOTE TableName doesn’t allow dynamic name, its result will be cached for future, to use dynamic name, you can use Scopes, for example:

type FileLocationMeta

type FileLocationMeta struct {
	// this is the instance Name
	LocationName string
	FileInfo     gofileDriver.FileInfo
}

type FileLocationsMeta

type FileLocationsMeta []FileLocationMeta

func (FileLocationsMeta) GormDBDataType

func (FileLocationsMeta) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm DB data type This is the Database data type which is sent to the DB

func (FileLocationsMeta) GormDataType

func (FileLocationsMeta) GormDataType() string

func (*FileLocationsMeta) Scan

func (l *FileLocationsMeta) Scan(value interface{}) error

Scan -> this is the func which is called when it's necessary to read from the Database to the defined variable with this type

func (FileLocationsMeta) Value

func (l FileLocationsMeta) Value() (driver.Value, error)

Value -> this is the func which is called when it's necessary to send to Database, the value should be automatically converted to JSON

type FileManager

type FileManager struct {
	// The DB Client
	DBClient      *gorm.DB
	DBAutoMigrate bool

	// Name -> the name should be short max 50 chars
	Name string

	Locations        Locations
	LocationsIndexed map[string]Location
	// contains filtered or unexported fields
}

FileManager - metadata will be stored

func GetInstance

func GetInstance(fm ...*FileManager) *FileManager

GetInstance -> it should be called once somewhere in the app?! It checks for table structure

func (*FileManager) DB

func (fm *FileManager) DB() *gorm.DB

func (*FileManager) DatabaseAutoMigrate

func (fm *FileManager) DatabaseAutoMigrate()

DatabaseAutoMigrate - create all necessary tables, alter,add columns

func (*FileManager) FindFile

func (fm *FileManager) FindFile(fileModel interface{}, cb func(db *gorm.DB) *gorm.DB) error

FindFile -> it will search for the described file in the database if nothing is found, an error will be returned

func (*FileManager) GetError

func (fm *FileManager) GetError() error

func (*FileManager) GetInternalError

func (fm *FileManager) GetInternalError() error

func (*FileManager) NewFile

func (fm *FileManager) NewFile() *NewFile

NewFile -> create a new file in the DB

type Location

type Location struct {
	// this is used driver name
	//DriverType string
	// Name -> the name is the identity
	Name string

	// the driver is the one that creates the bridge between the client and the destination server (location)
	// this is the instance with which the will interact
	Driver driver.LocationInterface `gorm:"-"`
	// contains filtered or unexported fields
}

func (*Location) TableName

func (f *Location) TableName() string

TableName -> Get the Database table name from the file manager

type LocationName

type LocationName string

type Locations

type Locations []Location

type Locations []Location

func (Locations) GormDBDataType

func (Locations) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm DB data type This is the Database data type which is sent to the DB

func (Locations) GormDataType

func (Locations) GormDataType() string

func (*Locations) Scan

func (l *Locations) Scan(value interface{}) error

Scan -> this is the func which is called when it's necessary to read from the Database to the defined variable with this type

func (Locations) Value

func (l Locations) Value() (driver.Value, error)

Value -> this is the func which is called when it's necessary to send to Database, the value should be automatically converted to JSON

type NewFile

type NewFile struct {
	// InputFile -> should be indicated, other params are optional!
	InputFilePath string
	GraphQLFile   *graphql.Upload

	// FileModel - asta e modelul din partea la client...
	FileModel interface{}

	// ========= Locations ==========\\
	//Locations Locations
	Locations []NewFileLocation
	// contains filtered or unexported fields
}

func (*NewFile) BgSave

func (f *NewFile) BgSave() (*File, error)

TODO: we can do later a BackgroundSave which in case of failure (because of storage failure or interconnection failure)

will retry the saving in specific locations

BgSave -> background save

func (*NewFile) Save

func (f *NewFile) Save() error

Save -> saves the input file to the location (destination)

type NewFileLocation

type NewFileLocation struct {
	// this is the instance name
	LocationName string

	// if none of the lower params are indicated, it will take the current files basedir and it will
	// recreate on the destination location
	//========= Optional Params ===========\\
	// DirPath -> is optional, a new folder path is indicated, the file name remains the same!
	DirPath string

	// FilePath -> is optional, a new full file path is indicated (it includes as the dir path)
	FilePath string
}

type UUID

type UUID uuid.UUID

func (UUID) GormDBDataType

func (UUID) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (*UUID) Scan

func (u *UUID) Scan(src interface{}) error

Scan implements sql.Scanner so UUIDs can be read from databases transparently. Currently, database types that map to string and []byte are supported. Please consult database-specific driver documentation for matching types.

func (UUID) String

func (u UUID) String() string

func (UUID) URN

func (u UUID) URN() string

func (*UUID) UnmarshalJSON

func (u *UUID) UnmarshalJSON(b []byte) error

func (UUID) Value

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

Value implements sql.Valuer so that UUIDs can be written to databases transparently. Currently, UUIDs map to strings. Please consult database-specific driver documentation for matching types.

Jump to

Keyboard shortcuts

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