解压功能实现

This commit is contained in:
Chenx221 2024-02-16 11:51:53 +08:00
parent 7743e2db67
commit a522af833d
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 67 additions and 2 deletions

View File

@ -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),
];
}
}
}

View File

@ -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 () {