You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

package_blob_upload.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package packages
  4. import (
  5. "context"
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "code.gitea.io/gitea/modules/util"
  11. )
  12. // ErrPackageBlobUploadNotExist indicates a package blob upload not exist error
  13. var ErrPackageBlobUploadNotExist = util.NewNotExistErrorf("package blob upload does not exist")
  14. func init() {
  15. db.RegisterModel(new(PackageBlobUpload))
  16. }
  17. // PackageBlobUpload represents a package blob upload
  18. type PackageBlobUpload struct {
  19. ID string `xorm:"pk"`
  20. BytesReceived int64 `xorm:"NOT NULL DEFAULT 0"`
  21. HashStateBytes []byte `xorm:"BLOB"`
  22. CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
  23. UpdatedUnix timeutil.TimeStamp `xorm:"updated INDEX NOT NULL"`
  24. }
  25. // CreateBlobUpload inserts a blob upload
  26. func CreateBlobUpload(ctx context.Context) (*PackageBlobUpload, error) {
  27. id, err := util.CryptoRandomString(25)
  28. if err != nil {
  29. return nil, err
  30. }
  31. pbu := &PackageBlobUpload{
  32. ID: strings.ToLower(id),
  33. }
  34. _, err = db.GetEngine(ctx).Insert(pbu)
  35. return pbu, err
  36. }
  37. // GetBlobUploadByID gets a blob upload by id
  38. func GetBlobUploadByID(ctx context.Context, id string) (*PackageBlobUpload, error) {
  39. pbu := &PackageBlobUpload{}
  40. has, err := db.GetEngine(ctx).ID(id).Get(pbu)
  41. if err != nil {
  42. return nil, err
  43. }
  44. if !has {
  45. return nil, ErrPackageBlobUploadNotExist
  46. }
  47. return pbu, nil
  48. }
  49. // UpdateBlobUpload updates the blob upload
  50. func UpdateBlobUpload(ctx context.Context, pbu *PackageBlobUpload) error {
  51. _, err := db.GetEngine(ctx).ID(pbu.ID).Update(pbu)
  52. return err
  53. }
  54. // DeleteBlobUploadByID deletes the blob upload
  55. func DeleteBlobUploadByID(ctx context.Context, id string) error {
  56. _, err := db.GetEngine(ctx).ID(id).Delete(&PackageBlobUpload{})
  57. return err
  58. }
  59. // FindExpiredBlobUploads gets all expired blob uploads
  60. func FindExpiredBlobUploads(ctx context.Context, olderThan time.Duration) ([]*PackageBlobUpload, error) {
  61. pbus := make([]*PackageBlobUpload, 0, 10)
  62. return pbus, db.GetEngine(ctx).
  63. Where("updated_unix < ?", time.Now().Add(-olderThan).Unix()).
  64. Find(&pbus)
  65. }