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 3.8 kB

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