diff --git a/controllers/HomeController.php b/controllers/HomeController.php index 8848ba8..1dba1f8 100644 --- a/controllers/HomeController.php +++ b/controllers/HomeController.php @@ -7,6 +7,7 @@ use app\models\RenameForm; use app\models\UploadForm; use app\models\ZipForm; use app\utils\FileTypeDetector; +use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException; use wapmorgan\UnifiedArchive\Exceptions\FileAlreadyExistsException; use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException; use wapmorgan\UnifiedArchive\UnifiedArchive; @@ -41,6 +42,7 @@ class HomeController extends Controller 'download-folder' => ['GET'], 'multi-ff-zip-dl' => ['POST'], 'zip' => ['POST'], + 'unzip' => ['POST'], ], ], ] @@ -509,4 +511,48 @@ class HomeController extends Controller } return $this->redirect(['index', 'directory' => $targetDirectory]); } + + /** + * @throws NotFoundHttpException + */ + public function actionUnzip() + { + $relativePath = Yii::$app->request->post('relativePath'); + if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) { + throw new NotFoundHttpException('Invalid file path.'); + } + $absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath; + + if (!file_exists($absolutePath)) { + throw new NotFoundHttpException('File not found.'); + } + $archive = UnifiedArchive::open($absolutePath); + if ($archive === null) { + throw new NotFoundHttpException('Failed to open the archive.'); + } + + $targetDirectory = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . pathinfo($relativePath, PATHINFO_FILENAME) . '_' . time(); + if (!is_dir($targetDirectory)) { + mkdir($targetDirectory, 0777, true); + } + + try { + $archive->extract($targetDirectory); + Yii::$app->session->setFlash('success', 'Folder created successfully.'); + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + return [ + 'status' => 200, + 'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . time(), + ]; + } catch (ArchiveExtractionException $e) { + $this->deleteDirectory($targetDirectory); + Yii::$app->session->setFlash('error', 'Failed to extract the archive.'); + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + return [ + 'status' => 500, + 'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . time(), + 'parentDirectory' => dirname($relativePath), + ]; + } + } } diff --git a/web/js/home_script.js b/web/js/home_script.js index 6b47ce0..3fdc167 100644 --- a/web/js/home_script.js +++ b/web/js/home_script.js @@ -96,8 +96,27 @@ $(document).on('click', '.batch-zip-btn', function () { }); $(document).on('click', '.unzip-btn', function () { - console.log('解压按钮被点击'); - // 在这里添加你的代码 + var relativePath = $('.select-item:checked').first().data('relativePath'); + $.ajax({ + type: "POST", + url: "index.php?r=home%2Funzip", + data: { relativePath: relativePath }, + dataType: "json", // 期望从服务器接收json格式的响应 + success: function(response) { + // 如果服务器返回的状态码是200,说明解压成功 + if (response.status === 200) { + // 刷新页面,加载到解压后的目录 + window.location.href = 'index.php?r=home%2Findex&directory=' + encodeURIComponent(response.directory); + } else { + console.error('Unzip failed: ' + response.message); + window.location.href = 'index.php?r=home%2Findex&directory=' + encodeURIComponent(response.parentDirectory); + } + }, + error: function() { + // 处理错误 + console.error('AJAX request failed.'); + } + }); }); $(document).on('click', '.single-rename-btn', function () {