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