models

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2019 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package models map program entity to database table

Index

Constants

View Source
const ChunkSize = 1 << 20

ChunkSize represent chunk size, default: 1MB

View Source
const Hidden = int8(1)

Hidden represent some file is hidden

View Source
const IsDir = int8(1)

IsDir represent a file model is a directory actually

View Source
const SecretLength = 12

SecretLength set the secret length

View Source
const TokenNonReadOnly = int8(0)

TokenNonReadOnly represent common token

View Source
const TokenReadOnly = int8(1)

TokenReadOnly represent read only token

Variables

View Source
var (
	// ErrInvalidChunkID when try to get the path of chunk, if the id of chunk
	// is less than 10000, this error will be panic
	ErrInvalidChunkID = errors.New("invalid chunk id")
	// ErrChunkExceedLimit the size of chunk is limited, if the content that is
	// more than ChunkSize is appended to chunk, it will panic this error
	ErrChunkExceedLimit = fmt.Errorf("total length exceed limit: %d bytes", ChunkSize)
)
View Source
var (
	// ErrFileExisted represent that the path has been occupied
	ErrFileExisted = errors.New("file has already existed")
	// ErrOverwriteDir represent that try to overwrite some directory
	ErrOverwriteDir = errors.New("directory can't be overwritten")
	// ErrAppendToDir represent that try to append content to directory
	ErrAppendToDir = errors.New("can't append data to directory")
	// ErrReadDir represent that can't read data from directory, only file
	ErrReadDir = errors.New("can't read a directory")
	// ErrAccessDenied represent a file can't be accessed by some tokens
	ErrAccessDenied = errors.New("file can't be accessed by some tokens")
	// ErrDeleteNonEmptyDir represent delete non-empty directory
	ErrDeleteNonEmptyDir = errors.New("delete non-empty directory")
)
View Source
var (
	// ErrInvalidObject represent a invalid object
	ErrInvalidObject = errors.New("invalid object")
	// ErrObjectNoChunks represent that a object has no any chunks
	ErrObjectNoChunks = errors.New("object has no any chunks")
	// ErrInvalidSeekWhence represent invalid seek whence
	ErrInvalidSeekWhence = errors.New("invalid seek whence")
	// ErrNegativePosition represent negative position
	ErrNegativePosition = errors.New("negative read position")
)
View Source
var (
	// NewAppForTest export newAppForTest for other package
	NewAppForTest = newAppForTest

	// SetUpTestCaseWithTrx is a helper method for helping to finish test
	SetUpTestCaseWithTrx = setUpTestCaseWithTrx

	// NewTokenForTest export newTokenForTest for other package
	NewTokenForTest = newTokenForTest

	// NewArbitrarilyTokenForTest export newArbitrarilyTokenForTest
	NewArbitrarilyTokenForTest = newArbitrarilyTokenForTest

	// NewTempDirForTest create a test directory for test
	NewTempDirForTest = newTempDirForTest
)

Functions

func CountObjectChunkByChunkID

func CountObjectChunkByChunkID(chunkID uint64, db *gorm.DB) (int, error)

CountObjectChunkByChunkID will count the middle value by chunkId

func DeleteAppByUIDPermanently

func DeleteAppByUIDPermanently(uid string, db *gorm.DB) error

DeleteAppByUIDPermanently delete an app permanently

func DeleteAppByUIDSoft

func DeleteAppByUIDSoft(uid string, db *gorm.DB) error

DeleteAppByUIDSoft soft delete an app by uid

func DeleteAppPermanently

func DeleteAppPermanently(app *App, db *gorm.DB) error

DeleteAppPermanently delete app app permanently

func DeleteAppSoft

func DeleteAppSoft(app *App, db *gorm.DB) error

DeleteAppSoft execute a soft delete, just mark app is deleted

func NewObjectReader

func NewObjectReader(object *Object, rootPath *string, db *gorm.DB) (io.ReadSeeker, error)

NewObjectReader is used to create a reader that read data from underlying chunk

func NewSecret

func NewSecret() string

NewSecret is used generate secret for app and token

func Random

func Random(length uint) []byte

Random is used to generate random bytes

func RandomWithMD5 added in v1.0.10

func RandomWithMD5(length uint) string

RandomWithMD5 is used to generate random and hashed by md5

func UID

func UID() string

UID is used to generate uid

Types

type App

type App struct {
	ID        uint64     `gorm:"type:BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;primary_key"`
	UID       string     `gorm:"type:CHAR(32) NOT NULL;UNIQUE;column:uid"`
	Secret    string     `gorm:"type:CHAR(32) NOT NULL"`
	Name      string     `gorm:"type:VARCHAR(100) NOT NULL"`
	Note      *string    `gorm:"type:VARCHAR(500) NULL"`
	CreatedAt time.Time  `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:createdAt"`
	UpdatedAt time.Time  `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:updatedAt"`
	DeletedAt *time.Time `gorm:"type:TIMESTAMP(6);INDEX;column:deletedAt"`
}

App represent an application in system

func FindAppByUID

func FindAppByUID(uid string, db *gorm.DB) (*App, error)

FindAppByUID find an application by uid

func FindAppByUIDWithTrashed

func FindAppByUIDWithTrashed(uid string, db *gorm.DB) (*App, error)

FindAppByUIDWithTrashed find an application by uid with trashed

func NewApp

func NewApp(name string, note *string, db *gorm.DB) (*App, error)

NewApp generate a new application by name and note

func (*App) AfterCreate

func (app *App) AfterCreate(tx *gorm.DB) error

AfterCreate hooks will be called automatically after app created

func (*App) TableName

func (app *App) TableName() string

TableName represent table name

type Chunk

type Chunk struct {
	ID        uint64    `gorm:"type:BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;primary_key"`
	Size      int       `gorm:"type:int;column:size"`
	Hash      string    `gorm:"type:CHAR(64) NOT NULL;UNIQUE;column:hash"`
	CreatedAt time.Time `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:createdAt"`
	UpdatedAt time.Time `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:updatedAt"`
}

Chunk represents every chunk of file

func CreateChunkFromBytes

func CreateChunkFromBytes(p []byte, rootPath *string, db *gorm.DB) (chunk *Chunk, err error)

CreateChunkFromBytes will crate a chunk from the specify byte content

func CreateEmptyContentChunk

func CreateEmptyContentChunk(rootPath *string, db *gorm.DB) (chunk *Chunk, err error)

CreateEmptyContentChunk is used to create a chunk with empty content

func FindChunkByHash

func FindChunkByHash(h string, db *gorm.DB) (*Chunk, error)

FindChunkByHash will find chunk by the specify hash

func (*Chunk) AppendBytes

func (c *Chunk) AppendBytes(p []byte, rootPath *string, db *gorm.DB) (chunk *Chunk, writeCount int, err error)

AppendBytes is used to append bytes to chunk. Firstly, this function will check whether there is already a chunk its hash value is equal to the hash of complete content. If exist, return it, otherwise, append content to origin chunk.

func (Chunk) Path

func (c Chunk) Path(rootPath *string) (path string, err error)

Path represent the actual storage path

func (*Chunk) Reader

func (c *Chunk) Reader(rootPath *string) (file *os.File, err error)

Reader return a reader with buffer

func (Chunk) TableName

func (c Chunk) TableName() string

TableName represent table name

type Down

type Down = func(*testing.T)

Down represent rollback function type

type File

type File struct {
	ID            uint64     `gorm:"type:BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;primary_key"`
	UID           string     `gorm:"type:CHAR(32) NOT NULL;UNIQUE;column:uid"`
	PID           uint64     `gorm:"type:BIGINT(20) UNSIGNED NOT NULL;column:pid"`
	AppID         uint64     `gorm:"type:BIGINT(20) UNSIGNED NOT NULL;column:appId"`
	ObjectID      uint64     `gorm:"type:BIGINT(20) UNSIGNED NOT NULL;column:objectId"`
	Size          int        `gorm:"type:int;column:size"`
	Name          string     `gorm:"type:VARCHAR(255);NOT NULL;column:name"`
	Ext           string     `gorm:"type:VARCHAR(255);NOT NULL;column:ext"`
	IsDir         int8       `gorm:"type:tinyint;column:isDir;DEFAULT:0"`
	Hidden        int8       `gorm:"type:tinyint;column:hidden;DEFAULT:0"`
	DownloadCount uint64     `gorm:"type:BIGINT(20);column:downloadCount;DEFAULT:0"`
	CreatedAt     time.Time  `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:createdAt"`
	UpdatedAt     time.Time  `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:updatedAt"`
	DeletedAt     *time.Time `gorm:"type:TIMESTAMP(6);INDEX;column:deletedAt"`

	App       App       `gorm:"foreignkey:appId;association_autoupdate:false;association_autocreate:false"`
	Object    Object    `gorm:"foreignkey:objectId;association_autoupdate:false;association_autocreate:false"`
	Parent    *File     `gorm:"foreignkey:id;association_foreignkey:pid;association_autoupdate:false;association_autocreate:false"`
	Children  []File    `gorm:"foreignkey:pid;association_foreignkey:id;association_autoupdate:false;association_autocreate:false"`
	Histories []History `gorm:"foreignkey:fileId;association_autoupdate:false;association_autocreate:false"`
}

File represent a file or a directory of system. If it's a file it has to associate with an object. Actually, the object hold the real content of file.

func CreateFileFromReader

func CreateFileFromReader(app *App, savePath string, reader io.Reader, hidden int8, rootPath *string, db *gorm.DB) (file *File, err error)

CreateFileFromReader is used to create a file from reader.

func CreateOrGetLastDirectory

func CreateOrGetLastDirectory(app *App, dirPath string, db *gorm.DB) (*File, error)

CreateOrGetLastDirectory is used to get last level directory, there is no difference between a relative path and an absolute path

func CreateOrGetRootPath

func CreateOrGetRootPath(app *App, db *gorm.DB) (*File, error)

CreateOrGetRootPath is used to create or get root directory

func FindFileByPath

func FindFileByPath(app *App, path string, db *gorm.DB, useCache bool) (*File, error)

FindFileByPath is used to find a file by the specify path

func FindFileByPathWithTrashed

func FindFileByPathWithTrashed(app *App, path string, db *gorm.DB) (*File, error)

FindFileByPathWithTrashed is used to find a file by the specify path, include deleted path

func FindFileByUID

func FindFileByUID(uid string, trashed bool, db *gorm.DB) (*File, error)

FindFileByUID is used to find a file by uid

func (*File) AppendFromReader

func (f *File) AppendFromReader(reader io.Reader, hidden int8, rootPath *string, db *gorm.DB) (err error)

AppendFromReader is used to append content from reader to file

func (*File) CanBeAccessedByToken

func (f *File) CanBeAccessedByToken(token *Token, db *gorm.DB) error

CanBeAccessedByToken represent whether the file can be accessed by the token

func (*File) Delete

func (f *File) Delete(forceDelete bool, db *gorm.DB) (err error)

Delete is used to delete file or directory. if the file is a non-empty directory, 'forceDelete' determine to delete or not sub directories and files

func (*File) MoveTo

func (f *File) MoveTo(newPath string, db *gorm.DB) (err error)

MoveTo move file to another path, the input path must be complete and new path. if the input path ios the same as the previous path, nothing changes.

func (*File) OverWriteFromReader

func (f *File) OverWriteFromReader(reader io.Reader, hidden int8, rootPath *string, db *gorm.DB) (err error)

OverWriteFromReader is used to overwrite the object

func (*File) Path

func (f *File) Path(db *gorm.DB) (string, error)

Path is used to get the complete path of file

func (*File) Reader

func (f *File) Reader(rootPath *string, db *gorm.DB) (io.ReadSeeker, error)

Reader is used to get reader that continues to read data from underlying chunk until io.EOF

func (*File) TableName

func (f *File) TableName() string

TableName represent the name of files table

func (*File) UpdateParentSize

func (f *File) UpdateParentSize(size int, db *gorm.DB) error

UpdateParentSize is used to update parent directory size. note, size may be a negative number.

type History

type History struct {
	ID        uint64    `gorm:"type:BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;primary_key"`
	ObjectID  uint64    `gorm:"type:BIGINT(20) UNSIGNED NOT NULL;column:objectId"`
	FileID    uint64    `gorm:"type:BIGINT(20) UNSIGNED NOT NULL;column:fileId"`
	Path      string    `gorm:"type:tinyint;column:path"`
	CreatedAt time.Time `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:createdAt"`
}

History represent the overwrite history of object. By this, we can easily find kinds of versions of the object.

func (*History) TableName

func (h *History) TableName() string

TableName represent the name of history table

type Object

type Object struct {
	ID        uint64    `gorm:"type:BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;primary_key"`
	Size      int       `gorm:"type:int;column:size"`
	Hash      string    `gorm:"type:CHAR(64) NOT NULL;UNIQUE;column:hash"`
	CreatedAt time.Time `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:createdAt"`
	UpdatedAt time.Time `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:updatedAt"`

	Files        []File        `gorm:"foreignkey:objectId;association_autoupdate:false;association_autocreate:false"`
	Chunks       []Chunk       `` /* 158-byte string literal not displayed */
	ObjectChunks []ObjectChunk `gorm:"foreignkey:objectId;association_autoupdate:false;association_autocreate:false"`
	Histories    []History     `gorm:"foreignkey:objectId;association_autoupdate:false;association_autocreate:false"`
}

Object represent a documentation that is correspond to system An object has many chunks, it's saved in disk by chunk. But, a file is a documentation that is correspond to user.

func CreateEmptyObject

func CreateEmptyObject(rootPath *string, db *gorm.DB) (*Object, error)

CreateEmptyObject is used to create an empty object

func CreateObjectFromReader

func CreateObjectFromReader(reader io.Reader, rootPath *string, db *gorm.DB) (object *Object, err error)

CreateObjectFromReader reads data from reader to create an object

func FindObjectByHash

func FindObjectByHash(h string, db *gorm.DB) (*Object, error)

FindObjectByHash will find object by the specify hash

func (*Object) AppendFromReader

func (o *Object) AppendFromReader(reader io.Reader, rootPath *string, db *gorm.DB) (object *Object, readerContentLen int, err error)

AppendFromReader will append content from reader to object

func (*Object) ChunkCount

func (o *Object) ChunkCount(db *gorm.DB) int

ChunkCount count the chunks of object and return

func (*Object) ChunkWithNumber

func (o *Object) ChunkWithNumber(number int, db *gorm.DB) (chunk *Chunk, err error)

ChunkWithNumber is used to load some chunk with number

func (*Object) FileCountWithTrashed

func (o *Object) FileCountWithTrashed(db *gorm.DB) int

FileCountWithTrashed count the files they are associated with this object

func (*Object) LastChunk

func (o *Object) LastChunk(db *gorm.DB) (*Chunk, error)

LastChunk return the last chunk of object

func (*Object) LastChunkNumber

func (o *Object) LastChunkNumber(db *gorm.DB) (int, error)

LastChunkNumber is used to return the last chunk number chunk number starts from 1, 0 represent no chunks

func (*Object) LastObjectChunk

func (o *Object) LastObjectChunk(db *gorm.DB) (*ObjectChunk, error)

LastObjectChunk return the middle value between chunk and object

func (*Object) Reader

func (o *Object) Reader(rootPath *string, db *gorm.DB) (io.ReadSeeker, error)

Reader is used to implement io.Reader

func (Object) TableName

func (o Object) TableName() string

TableName represent the db table name

type ObjectChunk

type ObjectChunk struct {
	ID        uint64    `gorm:"type:BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;primary_key"`
	ObjectID  uint64    `gorm:"type:BIGINT(20) UNSIGNED NOT NULL;column:objectId"`
	ChunkID   uint64    `gorm:"type:BIGINT(20) UNSIGNED NOT NULL;column:chunkId"`
	Number    int       `gorm:"type:int;column:number"`
	HashState *string   `gorm:"type:CHAR(64) NOT NULL;UNIQUE;column:hashState"`
	CreatedAt time.Time `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:createdAt"`
	UpdatedAt time.Time `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:updatedAt"`

	Object Object `gorm:"foreignkey:objectId;association_autoupdate:false;association_autocreate:false"`
	Chunk  Chunk  `gorm:"foreignkey:chunkId;association_autoupdate:false;association_autocreate:false"`
}

ObjectChunk is a middle table, that associates chunk and object

func (ObjectChunk) TableName

func (oc ObjectChunk) TableName() string

TableName represent the db table name

type Request

type Request struct {
	ID            uint64    `gorm:"type:BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;primary_key"`
	Protocol      string    `gorm:"type:CHAR(10) NOT NULL;column:protocol"`
	AppID         *uint64   `gorm:"type:BIGINT(20) UNSIGNED;DEFAULT:NULL;column:appId"`
	Nonce         *string   `gorm:"type:CHAR(48);DEFAULT:NULL;column:nonce"`
	Token         *string   `gorm:"type:CHAR(32);DEFAULT:NULL;column:token"`
	IP            *string   `gorm:"type:CHAR(15);column:ip;DEFAULT:NULL"`
	Method        *string   `gorm:"type:CHAR(10);column:method;DEFAULT:NULL"`
	Service       *string   `gorm:"type:VARCHAR(512);column:service;DEFAULT:NULL"`
	RequestBody   string    `gorm:"type:TEXT;column:requestBody"`
	RequestHeader string    `gorm:"type:TEXT;column:requestHeader"`
	ResponseCode  int       `gorm:"type:int;column:responseCode;DEFAULT:200"`
	ResponseBody  string    `gorm:"type:TEXT;column:responseBody"`
	CreatedAt     time.Time `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:createdAt"`
}

Request is used to record client request

func FindRequestWithAppAndNonce

func FindRequestWithAppAndNonce(app *App, nonce string, db *gorm.DB) (*Request, error)

FindRequestWithAppAndNonce will find a request with appId and nonce

func MustNewHTTPRequest

func MustNewHTTPRequest(ip, method, url string, db *gorm.DB) *Request

MustNewHTTPRequest is used to generate http protocol request record.

func MustNewRequestWithProtocol

func MustNewRequestWithProtocol(protocol string, db *gorm.DB) *Request

MustNewRequestWithProtocol is used to generate new request record. But, if some errors happened, it will panic

func NewRequestWithProtocol

func NewRequestWithProtocol(protocol string, db *gorm.DB) (*Request, error)

NewRequestWithProtocol is used to generate new request record

func (*Request) Save

func (r *Request) Save(db *gorm.DB) error

Save is used to persistent update to database

type Token

type Token struct {
	ID             uint64     `gorm:"type:BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;primary_key"`
	UID            string     `gorm:"type:CHAR(32) NOT NULL;UNIQUE;column:uid"`
	Secret         *string    `gorm:"type:CHAR(32)"`
	AppID          uint64     `gorm:"type:BIGINT(20) UNSIGNED NOT NULL;column:appId"`
	IP             *string    `gorm:"type:VARCHAR(1500);column:ip"`
	AvailableTimes int        `gorm:"type:int(10);column:availableTimes;DEFAULT:-1"`
	ReadOnly       int8       `gorm:"type:tinyint;column:readOnly;DEFAULT:0"`
	Path           string     `gorm:"type:tinyint;column:path"`
	ExpiredAt      *time.Time `gorm:"type:TIMESTAMP;column:expiredAt"`
	CreatedAt      time.Time  `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:createdAt"`
	UpdatedAt      time.Time  `gorm:"type:TIMESTAMP(6) NOT NULL;DEFAULT:CURRENT_TIMESTAMP(6);column:updatedAt"`
	DeletedAt      *time.Time `gorm:"type:TIMESTAMP(6);INDEX;column:deletedAt"`

	App App `gorm:"association_foreignkey:id;foreignkey:AppID;association_autoupdate:false;association_autocreate:false"`
}

Token acts as a key, it limits the user that own this token which directories can be accessed. Or only when it's used with specify ip, it will be accepted. Or some tokens only can be used to read file. every token has an expired time, expired token can't be used to do anything.

func FindTokenByUID

func FindTokenByUID(uid string, db *gorm.DB) (*Token, error)

FindTokenByUID find a token by uid

func FindTokenByUIDWithTrashed

func FindTokenByUIDWithTrashed(uid string, db *gorm.DB) (*Token, error)

FindTokenByUIDWithTrashed find a token by uid with trashed

func NewToken

func NewToken(
	app *App, path string, expiredAt *time.Time, ip, secret *string, availableTimes int, readOnly int8, db *gorm.DB,
) (*Token, error)

NewToken will generate a token by input params

func (*Token) AllowIPAccess

func (t *Token) AllowIPAccess(ip string) bool

AllowIPAccess is used to check whether this ip can be allowed to use this token

func (*Token) BeforeSave

func (t *Token) BeforeSave() (err error)

BeforeSave will be called before token saved

func (*Token) PathWithScope

func (t *Token) PathWithScope(path string) string

PathWithScope will return a complete path with scope of token

func (*Token) Scope

func (t *Token) Scope() string

Scope represent token scope. Actually, it's equal to path.

func (*Token) TableName

func (t *Token) TableName() string

TableName represent token table name

func (*Token) UpdateAvailableTimes

func (t *Token) UpdateAvailableTimes(inc int, db *gorm.DB) error

UpdateAvailableTimes is used to update the available times of this token

Jump to

Keyboard shortcuts

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