| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace App\Module\Cleanup\AdminControllers\Actions;
- use App\Module\Cleanup\Models\CleanupBackup;
- use Dcat\Admin\Actions\RowAction;
- use Dcat\Admin\Actions\Response;
- use Illuminate\Http\Request;
- /**
- * 查看备份文件Action
- *
- * 用于查看备份的文件列表
- */
- class ViewBackupFilesAction extends RowAction
- {
- /**
- * 按钮标题
- */
- protected $title = '查看文件';
- /**
- * 处理请求
- */
- public function handle(Request $request)
- {
- try {
- $backupId = $this->getKey();
-
- $backup = CleanupBackup::with('files')->find($backupId);
-
- if (!$backup) {
- return $this->response()
- ->error('备份不存在');
- }
-
- $files = $backup->files;
-
- if ($files->isEmpty()) {
- return $this->response()
- ->info('该备份暂无文件');
- }
-
- $html = '<div class="table-responsive"><table class="table table-striped table-sm">';
- $html .= '<thead><tr><th>表名</th><th>文件名</th><th>文件大小</th><th>文件路径</th><th>备份类型</th><th>压缩类型</th><th>哈希值</th></tr></thead>';
- $html .= '<tbody>';
-
- $backupTypes = [1 => 'SQL', 2 => 'JSON', 3 => 'CSV'];
- $compressionTypes = [1 => '无压缩', 2 => 'GZIP', 3 => 'ZIP'];
-
- foreach ($files as $file) {
- $backupType = $backupTypes[$file->backup_type] ?? '未知';
- $compressionType = $compressionTypes[$file->compression_type] ?? '未知';
- $fileSize = $this->formatBytes($file->file_size);
- $hash = $file->file_hash ? substr($file->file_hash, 0, 16) . '...' : '-';
-
- $html .= "<tr>";
- $html .= "<td>{$file->table_name}</td>";
- $html .= "<td>{$file->file_name}</td>";
- $html .= "<td>{$fileSize}</td>";
- $html .= "<td><small>{$file->file_path}</small></td>";
- $html .= "<td><span class='badge badge-info'>{$backupType}</span></td>";
- $html .= "<td><span class='badge badge-secondary'>{$compressionType}</span></td>";
- $html .= "<td><small>{$hash}</small></td>";
- $html .= "</tr>";
- }
-
- $html .= '</tbody></table></div>';
-
- // 添加统计信息
- $totalSize = $files->sum('file_size');
- $totalFiles = $files->count();
-
- $html .= '<div class="row mt-3">';
- $html .= '<div class="col-md-6"><div class="card card-body text-center">';
- $html .= '<h5 class="text-primary">' . $totalFiles . '</h5>';
- $html .= '<small>文件总数</small>';
- $html .= '</div></div>';
-
- $html .= '<div class="col-md-6"><div class="card card-body text-center">';
- $html .= '<h5 class="text-info">' . $this->formatBytes($totalSize) . '</h5>';
- $html .= '<small>文件总大小</small>';
- $html .= '</div></div>';
- $html .= '</div>';
-
- return $this->response()
- ->success('备份文件列表')
- ->detail($html);
-
- } catch (\Exception $e) {
- return $this->response()
- ->error('查看失败:' . $e->getMessage());
- }
- }
- /**
- * 格式化字节大小
- */
- private function formatBytes($bytes)
- {
- if ($bytes == 0) return '0 B';
-
- $units = ['B', 'KB', 'MB', 'GB', 'TB'];
- $base = log($bytes, 1024);
- $index = floor($base);
-
- if ($index >= count($units)) {
- $index = count($units) - 1;
- }
-
- $size = round(pow(1024, $base - $index), 2);
- return $size . ' ' . $units[$index];
- }
- /**
- * 渲染按钮
- */
- public function render()
- {
- return <<<HTML
- <a href="javascript:void(0);" class="btn btn-info btn-xs" data-action="{$this->getHandleRoute()}">
- <i class="fa fa-files-o"></i> {$this->title}
- </a>
- HTML;
- }
- }
|