b_file_chunk.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package internal
  2. import (
  3. "context"
  4. "github.com/gogf/gf/util/guid"
  5. "gxt-file-server/app/agent"
  6. "gxt-file-server/app/errors"
  7. "gxt-file-server/app/model"
  8. "gxt-file-server/app/schema"
  9. "log"
  10. )
  11. // NewFileChunk 创建demo
  12. func NewFileChunk(mFileChunk model.IFileChunk) *FileChunk {
  13. return &FileChunk{
  14. FileChunkModel: mFileChunk,
  15. }
  16. }
  17. // FileChunk 示例程序
  18. type FileChunk struct {
  19. FileChunkModel model.IFileChunk
  20. }
  21. // Query 查询数据
  22. func (a *FileChunk) Query(ctx context.Context, params schema.FileChunkQueryParam, opts ...schema.FileChunkQueryOptions) (*schema.FileChunkQueryResult, error) {
  23. return a.FileChunkModel.Query(ctx, params, opts...)
  24. }
  25. // Get 查询指定数据
  26. func (a *FileChunk) Get(ctx context.Context, recordID string, opts ...schema.FileChunkQueryOptions) (*schema.FileChunk, error) {
  27. item, err := a.FileChunkModel.Get(ctx, recordID, opts...)
  28. if err != nil {
  29. return nil, err
  30. } else if item == nil {
  31. return nil, errors.ErrNotFound
  32. }
  33. return item, nil
  34. }
  35. func (a *FileChunk) getUpdate(ctx context.Context, recordID string) (*schema.FileChunk, error) {
  36. return a.Get(ctx, recordID)
  37. }
  38. // Create 创建数据
  39. func (a *FileChunk) Create(ctx context.Context, item schema.FileChunk) (*schema.FileChunk, error) {
  40. item.RecordID = guid.S()
  41. err := a.FileChunkModel.Create(ctx, item)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return a.getUpdate(ctx, item.RecordID)
  46. }
  47. // Update 更新数据
  48. func (a *FileChunk) Update(ctx context.Context, recordID string, item schema.FileChunk) (*schema.FileChunk, error) {
  49. oldItem, err := a.FileChunkModel.Get(ctx, recordID)
  50. if err != nil {
  51. return nil, err
  52. } else if oldItem == nil {
  53. return nil, errors.ErrNotFound
  54. }
  55. err = a.FileChunkModel.Update(ctx, recordID, item)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return a.getUpdate(ctx, recordID)
  60. }
  61. // Delete 删除数据
  62. func (a *FileChunk) Delete(ctx context.Context, recordID string) error {
  63. oldItem, err := a.FileChunkModel.Get(ctx, recordID)
  64. if err != nil {
  65. return err
  66. } else if oldItem == nil {
  67. return errors.ErrNotFound
  68. }
  69. return a.FileChunkModel.Delete(ctx, recordID)
  70. }
  71. //ClearChunks 清理过期的文件分块
  72. func (f *FileChunk) ClearChunks(ctx context.Context) error {
  73. results, err := f.FileChunkModel.Query(ctx, schema.FileChunkQueryParam{
  74. IsClear: true,
  75. })
  76. if err != nil {
  77. return err
  78. }
  79. log.Printf("共检测出%d块过期文件", len(results.Data))
  80. for _, v := range results.Data {
  81. //删除文件
  82. err = agent.DefaultAgent().RemoveObject(ctx, v.Path)
  83. if err != nil {
  84. return err
  85. }
  86. //删除数据
  87. err = f.FileChunkModel.Delete(ctx, v.RecordID)
  88. if err != nil {
  89. return err
  90. }
  91. log.Printf("删除文件:%v\n", v.Path)
  92. }
  93. return nil
  94. }