修复"文件解压位置错误"问题

*现在解压不管成不成功,都会重新加载当前位置
This commit is contained in:
Chenx221 2024-04-11 18:46:46 +08:00
parent 160367c160
commit 09df400a94
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 29 additions and 30 deletions

View File

@ -635,36 +635,33 @@ class HomeController extends Controller
throw new NotFoundHttpException('Failed to open the archive.');
}
$now_time = time();
$targetDirectory = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time;
// $targetDirectory = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time;
$targetDirectory = dirname($absolutePath) . '/' . pathinfo($relativePath, PATHINFO_FILENAME) . '_unpacked_' . $now_time;
if (!is_dir($targetDirectory)) {
mkdir($targetDirectory, 0777, true);
}
try {
$archive->extract($targetDirectory);
Yii::$app->response->format = Response::FORMAT_JSON;
if (!FileSizeHelper::hasEnoughSpace()) {
$this->deleteDirectory($targetDirectory);
if (!FileSizeHelper::hasEnoughSpace($archive->getOriginalSize())) {
Yii::$app->session->setFlash('error', '解压失败,空间不足');
return [
'status' => 500,
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
'parentDirectory' => dirname($relativePath),
];
} else {
}
$archive->extract($targetDirectory);
Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->session->setFlash('success', '解压成功');
return [
'status' => 200,
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
'parentDirectory' => dirname($relativePath),
];
}
} catch (ArchiveExtractionException) {
$this->deleteDirectory($targetDirectory);
Yii::$app->session->setFlash('error', '解压过程中出现错误');
Yii::$app->response->format = Response::FORMAT_JSON;
return [
'status' => 500,
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
'parentDirectory' => dirname($relativePath),
];
}
@ -675,7 +672,8 @@ class HomeController extends Controller
*
* @return Response
*/
public function actionPaste(): Response
public
function actionPaste(): Response
{
// 获取请求中的操作类型、相对路径和目标目录
$operation = Yii::$app->request->post('operation');
@ -764,7 +762,8 @@ class HomeController extends Controller
* @param string $destination 目标目录路径
* @return bool 操作是否成功
*/
protected function copyDirectory(string $source, string $destination): bool
protected
function copyDirectory(string $source, string $destination): bool
{
// 创建目标目录
if (!mkdir($destination)) {
@ -809,7 +808,8 @@ class HomeController extends Controller
* @param string $destination 目标目录路径
* @return bool 操作是否成功
*/
protected function moveDirectory(string $source, string $destination): bool
protected
function moveDirectory(string $source, string $destination): bool
{
// 创建目标目录
if (!mkdir($destination)) {
@ -858,7 +858,8 @@ class HomeController extends Controller
* @return array
* @throws NotFoundHttpException
*/
public function actionChecksum(): array
public
function actionChecksum(): array
{
$relativePath = Yii::$app->request->post('relativePath');
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
@ -883,7 +884,8 @@ class HomeController extends Controller
/**
* @throws NotFoundHttpException
*/
public function actionSearch(): Response
public
function actionSearch(): Response
{
if (Yii::$app->request->isAjax) {
$directory = empty(Yii::$app->request->post('directory')) ? '.' : Yii::$app->request->post('directory');

View File

@ -104,13 +104,10 @@ $(document).on('click', '.unzip-btn', function () {
dataType: "json", // 期望从服务器接收json格式的响应
success: function (response) {
// 如果服务器返回的状态码是200说明解压成功
if (response.status === 200) {
// 刷新页面,加载到解压后的目录
window.location.href = 'index.php?r=home%2Findex&directory=' + encodeURIComponent(response.directory);
} else {
if (response.status !== 200) {
console.error('Unzip failed: ' + response.message);
window.location.href = 'index.php?r=home%2Findex&directory=' + encodeURIComponent(response.parentDirectory);
}
window.location.href = 'index.php?r=home%2Findex&directory=' + encodeURIComponent(response.parentDirectory);
},
error: function () {
// 处理错误