package models import ( "code.gitea.io/gitea/modules/timeutil" "xorm.io/xorm" ) const ( FileNotUploaded int = iota FileUploaded ) const ( TypeCloudBrainOne = 0 TypeCloudBrainTwo = 1 ) type FileChunk struct { ID int64 `xorm:"pk autoincr"` UUID string `xorm:"uuid UNIQUE"` Md5 string `xorm:"INDEX"` IsUploaded int `xorm:"DEFAULT 0"` // not uploaded: 0, uploaded: 1 UploadID string `xorm:"UNIQUE"` //minio upload id TotalChunks int Size int64 UserID int64 `xorm:"INDEX"` Type int `xorm:"INDEX DEFAULT 0"` CompletedParts []string `xorm:"DEFAULT ''"` // chunkNumber+etag eg: ,1-asqwewqe21312312.2-123hjkas CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } // GetFileChunkByMD5 returns fileChunk by given id func GetFileChunkByMD5(md5 string) (*FileChunk, error) { return getFileChunkByMD5(x, md5) } func getFileChunkByMD5(e Engine, md5 string) (*FileChunk, error) { fileChunk := new(FileChunk) if has, err := e.Where("md5 = ?", md5).Get(fileChunk); err != nil { return nil, err } else if !has { return nil, ErrFileChunkNotExist{md5, ""} } return fileChunk, nil } // GetFileChunkByMD5 returns fileChunk by given id func GetFileChunkByMD5AndUser(md5 string, userID int64, typeCloudBrain int) (*FileChunk, error) { return getFileChunkByMD5AndUser(x, md5, userID, typeCloudBrain) } func getFileChunkByMD5AndUser(e Engine, md5 string, userID int64, typeCloudBrain int) (*FileChunk, error) { fileChunk := new(FileChunk) if has, err := e.Where("md5 = ? and user_id = ? and type = ?", md5, userID, typeCloudBrain).Get(fileChunk); err != nil { return nil, err } else if !has { return nil, ErrFileChunkNotExist{md5, ""} } return fileChunk, nil } // GetAttachmentByID returns attachment by given id func GetFileChunkByUUID(uuid string) (*FileChunk, error) { return getFileChunkByUUID(x, uuid) } func getFileChunkByUUID(e Engine, uuid string) (*FileChunk, error) { fileChunk := new(FileChunk) if has, err := e.Where("uuid = ?", uuid).Get(fileChunk); err != nil { return nil, err } else if !has { return nil, ErrFileChunkNotExist{"", uuid} } return fileChunk, nil } // InsertFileChunk insert a record into file_chunk. func InsertFileChunk(fileChunk *FileChunk) (_ *FileChunk, err error) { if _, err := x.Insert(fileChunk); err != nil { return nil, err } return fileChunk, nil } // UpdateAttachment updates the given attachment in database func UpdateFileChunk(fileChunk *FileChunk) error { return updateFileChunk(x, fileChunk) } func updateFileChunk(e Engine, fileChunk *FileChunk) error { var sess *xorm.Session sess = e.Where("uuid = ?", fileChunk.UUID) _, err := sess.Cols("is_uploaded").Update(fileChunk) return err }