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