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