ViewBackupFilesAction.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Models\CleanupBackup;
  4. use Dcat\Admin\Actions\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 查看备份文件Action
  9. *
  10. * 用于查看备份的文件列表
  11. */
  12. class ViewBackupFilesAction 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. $backup = CleanupBackup::with('files')->find($backupId);
  26. if (!$backup) {
  27. return $this->response()
  28. ->error('备份不存在');
  29. }
  30. $files = $backup->files;
  31. if ($files->isEmpty()) {
  32. return $this->response()
  33. ->info('该备份暂无文件');
  34. }
  35. $html = '<div class="table-responsive"><table class="table table-striped table-sm">';
  36. $html .= '<thead><tr><th>表名</th><th>文件名</th><th>文件大小</th><th>文件路径</th><th>备份类型</th><th>压缩类型</th><th>哈希值</th></tr></thead>';
  37. $html .= '<tbody>';
  38. $backupTypes = [1 => 'SQL', 2 => 'JSON', 3 => 'CSV'];
  39. $compressionTypes = [1 => '无压缩', 2 => 'GZIP', 3 => 'ZIP'];
  40. foreach ($files as $file) {
  41. $backupType = $backupTypes[$file->backup_type] ?? '未知';
  42. $compressionType = $compressionTypes[$file->compression_type] ?? '未知';
  43. $fileSize = $this->formatBytes($file->file_size);
  44. $hash = $file->file_hash ? substr($file->file_hash, 0, 16) . '...' : '-';
  45. $html .= "<tr>";
  46. $html .= "<td>{$file->table_name}</td>";
  47. $html .= "<td>{$file->file_name}</td>";
  48. $html .= "<td>{$fileSize}</td>";
  49. $html .= "<td><small>{$file->file_path}</small></td>";
  50. $html .= "<td><span class='badge badge-info'>{$backupType}</span></td>";
  51. $html .= "<td><span class='badge badge-secondary'>{$compressionType}</span></td>";
  52. $html .= "<td><small>{$hash}</small></td>";
  53. $html .= "</tr>";
  54. }
  55. $html .= '</tbody></table></div>';
  56. // 添加统计信息
  57. $totalSize = $files->sum('file_size');
  58. $totalFiles = $files->count();
  59. $html .= '<div class="row mt-3">';
  60. $html .= '<div class="col-md-6"><div class="card card-body text-center">';
  61. $html .= '<h5 class="text-primary">' . $totalFiles . '</h5>';
  62. $html .= '<small>文件总数</small>';
  63. $html .= '</div></div>';
  64. $html .= '<div class="col-md-6"><div class="card card-body text-center">';
  65. $html .= '<h5 class="text-info">' . $this->formatBytes($totalSize) . '</h5>';
  66. $html .= '<small>文件总大小</small>';
  67. $html .= '</div></div>';
  68. $html .= '</div>';
  69. return $this->response()
  70. ->success('备份文件列表')
  71. ->detail($html);
  72. } catch (\Exception $e) {
  73. return $this->response()
  74. ->error('查看失败:' . $e->getMessage());
  75. }
  76. }
  77. /**
  78. * 格式化字节大小
  79. */
  80. private function formatBytes($bytes)
  81. {
  82. if ($bytes == 0) return '0 B';
  83. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  84. $base = log($bytes, 1024);
  85. $index = floor($base);
  86. if ($index >= count($units)) {
  87. $index = count($units) - 1;
  88. }
  89. $size = round(pow(1024, $base - $index), 2);
  90. return $size . ' ' . $units[$index];
  91. }
  92. /**
  93. * 渲染按钮
  94. */
  95. public function render()
  96. {
  97. return <<<HTML
  98. <a href="javascript:void(0);" class="btn btn-info btn-xs" data-action="{$this->getHandleRoute()}">
  99. <i class="fa fa-files-o"></i> {$this->title}
  100. </a>
  101. HTML;
  102. }
  103. }