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

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