| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Module\Cleanup\AdminControllers\Actions;
- use App\Module\Cleanup\Services\CleanupService;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Dcat\Admin\Actions\Response;
- use Illuminate\Http\Request;
- /**
- * 清理过期备份Action
- *
- * 用于清理过期的备份文件
- */
- class CleanExpiredBackupsAction extends AbstractTool
- {
- /**
- * 按钮标题
- */
- protected $title = '清理过期备份';
- /**
- * 处理请求
- */
- public function handle(Request $request)
- {
- try {
- $dryRun = $request->input('dry_run', false);
-
- // 调用服务清理过期备份
- $result = CleanupService::cleanExpiredBackups($dryRun);
-
- if (!$result['success']) {
- return $this->response()
- ->error('清理失败:' . $result['message']);
- }
-
- $data = $result['data'];
-
- if ($dryRun) {
- return $this->response()
- ->success('预览完成')
- ->detail("
- 发现过期备份:{$data['expired_count']} 个<br>
- 预计释放空间:{$data['estimated_freed_space']}<br>
- <br>
- <small>这是预览模式,没有实际删除任何文件。</small>
- ");
- } else {
- return $this->response()
- ->success('清理完成!')
- ->detail("
- 删除过期备份:{$data['deleted_count']} 个<br>
- 释放空间:{$data['freed_space']}<br>
- 清理时间:{$data['execution_time']}秒
- ")
- ->refresh();
- }
-
- } catch (\Exception $e) {
- return $this->response()
- ->error('清理失败:' . $e->getMessage());
- }
- }
- /**
- * 确认对话框
- */
- public function confirm()
- {
- return [
- '清理过期备份',
- '将删除所有已过期的备份文件,释放存储空间。',
- [
- 'dry_run' => [
- 'type' => 'checkbox',
- 'label' => '预览模式',
- 'checked' => true,
- 'help' => '勾选则只预览,不实际删除文件',
- ]
- ]
- ];
- }
- /**
- * 渲染按钮
- */
- public function render()
- {
- return <<<HTML
- <a href="javascript:void(0);" class="btn btn-warning btn-sm" data-action="{$this->getHandleRoute()}">
- <i class="fa fa-trash-o"></i> {$this->title}
- </a>
- HTML;
- }
- }
|