DownloadBackupAction.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Dcat\Admin\Grid\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 下载备份Action
  9. *
  10. * 用于下载备份文件
  11. */
  12. class DownloadBackupAction extends RowAction
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '下载备份';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $backupId = $this->getKey();
  25. // 调用服务生成下载链接
  26. $result = CleanupService::generateBackupDownloadUrl($backupId);
  27. if (!$result['success']) {
  28. return $this->response()
  29. ->error('生成下载链接失败:' . $result['message']);
  30. }
  31. $data = $result['data'];
  32. return $this->response()
  33. ->success('下载链接已生成')
  34. ->detail("
  35. 备份名称:{$data['backup_name']}<br>
  36. 文件大小:{$data['file_size']}<br>
  37. 过期时间:{$data['expires_at']}<br>
  38. <br>
  39. <a href='{$data['download_url']}' class='btn btn-primary' target='_blank'>
  40. <i class='fa fa-download'></i> 立即下载
  41. </a>
  42. ");
  43. } catch (\Exception $e) {
  44. return $this->response()
  45. ->error('下载失败:' . $e->getMessage());
  46. }
  47. }
  48. /**
  49. * 确认对话框
  50. */
  51. public function confirm()
  52. {
  53. return [
  54. '确认下载备份?',
  55. '将生成临时下载链接,请及时下载。'
  56. ];
  57. }
  58. /**
  59. * 权限检查
  60. */
  61. public function allowed()
  62. {
  63. $row = $this->row;
  64. return $row->backup_status == 2; // 只有已完成的备份可以下载
  65. }
  66. }