CleanExpiredBackupsAction.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Dcat\Admin\Grid\Tools\AbstractTool;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 清理过期备份Action
  9. *
  10. * 用于清理过期的备份文件
  11. */
  12. class CleanExpiredBackupsAction extends AbstractTool
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '清理过期备份';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $dryRun = $request->input('dry_run', false);
  25. // 调用服务清理过期备份
  26. $result = CleanupService::cleanExpiredBackups($dryRun);
  27. if (!$result['success']) {
  28. return $this->response()
  29. ->error('清理失败:' . $result['message']);
  30. }
  31. $data = $result['data'];
  32. if ($dryRun) {
  33. return $this->response()
  34. ->success('预览完成')
  35. ->detail("
  36. 发现过期备份:{$data['expired_count']} 个<br>
  37. 预计释放空间:{$data['estimated_freed_space']}<br>
  38. <br>
  39. <small>这是预览模式,没有实际删除任何文件。</small>
  40. ");
  41. } else {
  42. return $this->response()
  43. ->success('清理完成!')
  44. ->detail("
  45. 删除过期备份:{$data['deleted_count']} 个<br>
  46. 释放空间:{$data['freed_space']}<br>
  47. 清理时间:{$data['execution_time']}秒
  48. ")
  49. ->refresh();
  50. }
  51. } catch (\Exception $e) {
  52. return $this->response()
  53. ->error('清理失败:' . $e->getMessage());
  54. }
  55. }
  56. /**
  57. * 确认对话框
  58. */
  59. public function confirm()
  60. {
  61. return [
  62. '清理过期备份',
  63. '将删除所有已过期的备份文件,释放存储空间。',
  64. [
  65. 'dry_run' => [
  66. 'type' => 'checkbox',
  67. 'label' => '预览模式',
  68. 'checked' => true,
  69. 'help' => '勾选则只预览,不实际删除文件',
  70. ]
  71. ]
  72. ];
  73. }
  74. }