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