You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

file_chunk.go 2.9 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/timeutil"
  4. "xorm.io/xorm"
  5. )
  6. const (
  7. FileNotUploaded int = iota
  8. FileUploaded
  9. )
  10. const (
  11. TypeCloudBrainOne = 0
  12. TypeCloudBrainNotebook = 1
  13. TypeCloudBrainTrainJob = 2
  14. )
  15. type FileChunk struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. UUID string `xorm:"uuid UNIQUE"`
  18. Md5 string `xorm:"INDEX"`
  19. IsUploaded int `xorm:"DEFAULT 0"` // not uploaded: 0, uploaded: 1
  20. UploadID string `xorm:"UNIQUE"` //minio upload id
  21. TotalChunks int
  22. Size int64
  23. UserID int64 `xorm:"INDEX"`
  24. Type int `xorm:"INDEX DEFAULT 0"`
  25. CompletedParts []string `xorm:"DEFAULT ''"` // chunkNumber+etag eg: ,1-asqwewqe21312312.2-123hjkas
  26. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  27. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  28. }
  29. // GetFileChunkByMD5 returns fileChunk by given id
  30. func GetFileChunkByMD5(md5 string) (*FileChunk, error) {
  31. return getFileChunkByMD5(x, md5)
  32. }
  33. func getFileChunkByMD5(e Engine, md5 string) (*FileChunk, error) {
  34. fileChunk := new(FileChunk)
  35. if has, err := e.Where("md5 = ?", md5).Get(fileChunk); err != nil {
  36. return nil, err
  37. } else if !has {
  38. return nil, ErrFileChunkNotExist{md5, ""}
  39. }
  40. return fileChunk, nil
  41. }
  42. // GetFileChunkByMD5 returns fileChunk by given id
  43. func GetFileChunkByMD5AndUser(md5 string, userID int64, typeCloudBrain int) (*FileChunk, error) {
  44. return getFileChunkByMD5AndUser(x, md5, userID, typeCloudBrain)
  45. }
  46. func getFileChunkByMD5AndUser(e Engine, md5 string, userID int64, typeCloudBrain int) (*FileChunk, error) {
  47. fileChunk := new(FileChunk)
  48. if has, err := e.Where("md5 = ? and user_id = ? and type = ?", md5, userID, typeCloudBrain).Get(fileChunk); err != nil {
  49. return nil, err
  50. } else if !has {
  51. return nil, ErrFileChunkNotExist{md5, ""}
  52. }
  53. return fileChunk, nil
  54. }
  55. // GetAttachmentByID returns attachment by given id
  56. func GetFileChunkByUUID(uuid string) (*FileChunk, error) {
  57. return getFileChunkByUUID(x, uuid)
  58. }
  59. func getFileChunkByUUID(e Engine, uuid string) (*FileChunk, error) {
  60. fileChunk := new(FileChunk)
  61. if has, err := e.Where("uuid = ?", uuid).Get(fileChunk); err != nil {
  62. return nil, err
  63. } else if !has {
  64. return nil, ErrFileChunkNotExist{"", uuid}
  65. }
  66. return fileChunk, nil
  67. }
  68. // InsertFileChunk insert a record into file_chunk.
  69. func InsertFileChunk(fileChunk *FileChunk) (_ *FileChunk, err error) {
  70. if _, err := x.Insert(fileChunk); err != nil {
  71. return nil, err
  72. }
  73. return fileChunk, nil
  74. }
  75. // UpdateAttachment updates the given attachment in database
  76. func UpdateFileChunk(fileChunk *FileChunk) error {
  77. return updateFileChunk(x, fileChunk)
  78. }
  79. func updateFileChunk(e Engine, fileChunk *FileChunk) error {
  80. var sess *xorm.Session
  81. sess = e.Where("uuid = ?", fileChunk.UUID)
  82. _, err := sess.Cols("is_uploaded").Update(fileChunk)
  83. return err
  84. }