Documentation ¶
Overview ¶
Package models map program entity to database table
Index ¶
- Constants
- Variables
- func CountObjectChunkByChunkID(chunkID uint64, db *gorm.DB) (int, error)
- func DeleteAppByUIDPermanently(uid string, db *gorm.DB) error
- func DeleteAppByUIDSoft(uid string, db *gorm.DB) error
- func DeleteAppPermanently(app *App, db *gorm.DB) error
- func DeleteAppSoft(app *App, db *gorm.DB) error
- func NewObjectReader(object *Object, rootPath *string, db *gorm.DB) (io.ReadSeeker, error)
- func NewSecret() string
- func Random(length uint) []byte
- func RandomWithMd5(length uint) string
- func UID() string
- type App
- type Chunk
- type Down
- type File
- func CreateFileFromReader(app *App, savePath string, reader io.Reader, hidden int8, rootPath *string, ...) (file *File, err error)
- func CreateOrGetLastDirectory(app *App, dirPath string, db *gorm.DB) (*File, error)
- func CreateOrGetRootPath(app *App, db *gorm.DB) (*File, error)
- func FindFileByPath(app *App, path string, db *gorm.DB, useCache bool) (*File, error)
- func FindFileByPathWithTrashed(app *App, path string, db *gorm.DB) (*File, error)
- func FindFileByUID(uid string, trashed bool, db *gorm.DB) (*File, error)
- func (f *File) AppendFromReader(reader io.Reader, hidden int8, rootPath *string, db *gorm.DB) (err error)
- func (f *File) CanBeAccessedByToken(token *Token, db *gorm.DB) error
- func (f *File) Delete(forceDelete bool, db *gorm.DB) (err error)
- func (f *File) MoveTo(newPath string, db *gorm.DB) (err error)
- func (f *File) OverWriteFromReader(reader io.Reader, hidden int8, rootPath *string, db *gorm.DB) (err error)
- func (f *File) Path(db *gorm.DB) (string, error)
- func (f *File) Reader(rootPath *string, db *gorm.DB) (io.ReadSeeker, error)
- func (f *File) TableName() string
- func (f *File) UpdateParentSize(size int, db *gorm.DB) error
- type History
- type Object
- func (o *Object) AppendFromReader(reader io.Reader, rootPath *string, db *gorm.DB) (object *Object, readerContentLen int, err error)
- func (o *Object) ChunkCount(db *gorm.DB) int
- func (o *Object) ChunkWithNumber(number int, db *gorm.DB) (chunk *Chunk, err error)
- func (o *Object) FileCountWithTrashed(db *gorm.DB) int
- func (o *Object) LastChunk(db *gorm.DB) (*Chunk, error)
- func (o *Object) LastChunkNumber(db *gorm.DB) (int, error)
- func (o *Object) LastObjectChunk(db *gorm.DB) (*ObjectChunk, error)
- func (o *Object) Reader(rootPath *string, db *gorm.DB) (io.ReadSeeker, error)
- func (o Object) TableName() string
- type ObjectChunk
- type Request
- func FindRequestWithAppAndNonce(app *App, nonce string, db *gorm.DB) (*Request, error)
- func MustNewHTTPRequest(ip, method, url string, db *gorm.DB) *Request
- func MustNewRequestWithProtocol(protocol string, db *gorm.DB) *Request
- func NewRequestWithProtocol(protocol string, db *gorm.DB) (*Request, error)
- type Token
Constants ¶
const ChunkSize = 1 << 20
ChunkSize represent chunk size, default: 1MB
const Hidden = int8(1)
Hidden represent some file is hidden
const IsDir = int8(1)
IsDir represent a file model is a directory actually
const SecretLength = 12
SecretLength set the secret length
const TokenNonReadOnly = int8(0)
TokenNonReadOnly represent common token
const TokenReadOnly = int8(1)
TokenReadOnly represent read only token
Variables ¶
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) )
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") )
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") )
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 ¶
CountObjectChunkByChunkID will count the middle value by chunkId
func DeleteAppByUIDPermanently ¶
DeleteAppByUIDPermanently delete an app permanently
func DeleteAppByUIDSoft ¶
DeleteAppByUIDSoft soft delete an app by uid
func DeleteAppPermanently ¶
DeleteAppPermanently delete app app permanently
func DeleteAppSoft ¶
DeleteAppSoft execute a soft delete, just mark app is deleted
func NewObjectReader ¶
NewObjectReader is used to create a reader that read data from underlying chunk
func RandomWithMd5 ¶
RandomWithMd5 is used to generate random and hashed by md5
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 ¶
FindAppByUID find an application by uid
func FindAppByUIDWithTrashed ¶
FindAppByUIDWithTrashed find an application by uid with trashed
func (*App) AfterCreate ¶
AfterCreate hooks will be called automatically after app created
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 ¶
CreateChunkFromBytes will crate a chunk from the specify byte content
func CreateEmptyContentChunk ¶
CreateEmptyContentChunk is used to create a chunk with empty content
func FindChunkByHash ¶
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.
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 ¶
CreateOrGetLastDirectory is used to get last level directory, there is no difference between a relative path and an absolute path
func CreateOrGetRootPath ¶
CreateOrGetRootPath is used to create or get root directory
func FindFileByPath ¶
FindFileByPath is used to find a file by the specify path
func FindFileByPathWithTrashed ¶
FindFileByPathWithTrashed is used to find a file by the specify path, include deleted path
func FindFileByUID ¶
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 ¶
CanBeAccessedByToken represent whether the file can be accessed by the token
func (*File) Delete ¶
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 ¶
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) Reader ¶
Reader is used to get reader that continues to read data from underlying chunk until io.EOF
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.
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 ¶
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 ¶
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 ¶
ChunkCount count the chunks of object and return
func (*Object) ChunkWithNumber ¶
ChunkWithNumber is used to load some chunk with number
func (*Object) FileCountWithTrashed ¶
FileCountWithTrashed count the files they are associated with this object
func (*Object) LastChunkNumber ¶
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
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 ¶
FindRequestWithAppAndNonce will find a request with appId and nonce
func MustNewHTTPRequest ¶
MustNewHTTPRequest is used to generate http protocol request record.
func MustNewRequestWithProtocol ¶
MustNewRequestWithProtocol is used to generate new request record. But, if some errors happened, it will panic
func NewRequestWithProtocol ¶
NewRequestWithProtocol is used to generate new request record
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 ¶
FindTokenByUID find a token by uid
func FindTokenByUIDWithTrashed ¶
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 ¶
AllowIPAccess is used to check whether this ip can be allowed to use this token
func (*Token) BeforeSave ¶
BeforeSave will be called before token saved
func (*Token) PathWithScope ¶
PathWithScope will return a complete path with scope of token