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

*现在解压不管成不成功,都会重新加载当前位置
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

@ -40,7 +40,7 @@ class HomeController extends Controller
'rules' => [ 'rules' => [
[ [
'allow' => true, 'allow' => true,
'actions' => ['index', 'download', 'preview', 'rename', 'delete', 'upload', 'new-folder', 'download-folder', 'multi-ff-zip-dl', 'zip', 'unzip', 'paste', 'search','checksum'], 'actions' => ['index', 'download', 'preview', 'rename', 'delete', 'upload', 'new-folder', 'download-folder', 'multi-ff-zip-dl', 'zip', 'unzip', 'paste', 'search', 'checksum'],
'roles' => ['user'], 'roles' => ['user'],
], ],
], ],
@ -635,36 +635,33 @@ class HomeController extends Controller
throw new NotFoundHttpException('Failed to open the archive.'); throw new NotFoundHttpException('Failed to open the archive.');
} }
$now_time = time(); $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)) { if (!is_dir($targetDirectory)) {
mkdir($targetDirectory, 0777, true); mkdir($targetDirectory, 0777, true);
} }
try { try {
$archive->extract($targetDirectory); if (!FileSizeHelper::hasEnoughSpace($archive->getOriginalSize())) {
Yii::$app->response->format = Response::FORMAT_JSON;
if (!FileSizeHelper::hasEnoughSpace()) {
$this->deleteDirectory($targetDirectory);
Yii::$app->session->setFlash('error', '解压失败,空间不足'); Yii::$app->session->setFlash('error', '解压失败,空间不足');
return [ return [
'status' => 500, 'status' => 500,
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
'parentDirectory' => dirname($relativePath), 'parentDirectory' => dirname($relativePath),
]; ];
} else { }
$archive->extract($targetDirectory);
Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->session->setFlash('success', '解压成功'); Yii::$app->session->setFlash('success', '解压成功');
return [ return [
'status' => 200, 'status' => 200,
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time, 'parentDirectory' => dirname($relativePath),
]; ];
}
} catch (ArchiveExtractionException) { } catch (ArchiveExtractionException) {
$this->deleteDirectory($targetDirectory); $this->deleteDirectory($targetDirectory);
Yii::$app->session->setFlash('error', '解压过程中出现错误'); Yii::$app->session->setFlash('error', '解压过程中出现错误');
Yii::$app->response->format = Response::FORMAT_JSON; Yii::$app->response->format = Response::FORMAT_JSON;
return [ return [
'status' => 500, 'status' => 500,
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
'parentDirectory' => dirname($relativePath), 'parentDirectory' => dirname($relativePath),
]; ];
} }
@ -675,7 +672,8 @@ class HomeController extends Controller
* *
* @return Response * @return Response
*/ */
public function actionPaste(): Response public
function actionPaste(): Response
{ {
// 获取请求中的操作类型、相对路径和目标目录 // 获取请求中的操作类型、相对路径和目标目录
$operation = Yii::$app->request->post('operation'); $operation = Yii::$app->request->post('operation');
@ -764,7 +762,8 @@ class HomeController extends Controller
* @param string $destination 目标目录路径 * @param string $destination 目标目录路径
* @return bool 操作是否成功 * @return bool 操作是否成功
*/ */
protected function copyDirectory(string $source, string $destination): bool protected
function copyDirectory(string $source, string $destination): bool
{ {
// 创建目标目录 // 创建目标目录
if (!mkdir($destination)) { if (!mkdir($destination)) {
@ -809,7 +808,8 @@ class HomeController extends Controller
* @param string $destination 目标目录路径 * @param string $destination 目标目录路径
* @return bool 操作是否成功 * @return bool 操作是否成功
*/ */
protected function moveDirectory(string $source, string $destination): bool protected
function moveDirectory(string $source, string $destination): bool
{ {
// 创建目标目录 // 创建目标目录
if (!mkdir($destination)) { if (!mkdir($destination)) {
@ -858,7 +858,8 @@ class HomeController extends Controller
* @return array * @return array
* @throws NotFoundHttpException * @throws NotFoundHttpException
*/ */
public function actionChecksum(): array public
function actionChecksum(): array
{ {
$relativePath = Yii::$app->request->post('relativePath'); $relativePath = Yii::$app->request->post('relativePath');
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) { if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
@ -883,10 +884,11 @@ class HomeController extends Controller
/** /**
* @throws NotFoundHttpException * @throws NotFoundHttpException
*/ */
public function actionSearch(): Response public
function actionSearch(): Response
{ {
if (Yii::$app->request->isAjax) { if (Yii::$app->request->isAjax) {
$directory = empty(Yii::$app->request->post('directory'))?'.':Yii::$app->request->post('directory'); $directory = empty(Yii::$app->request->post('directory')) ? '.' : Yii::$app->request->post('directory');
$keyword = Yii::$app->request->post('keyword'); $keyword = Yii::$app->request->post('keyword');
if ($keyword == null) { if ($keyword == null) {
return $this->asJson(['status' => 'error', 'message' => '关键词不能为空']); return $this->asJson(['status' => 'error', 'message' => '关键词不能为空']);
@ -909,7 +911,7 @@ class HomeController extends Controller
$results[] = [ $results[] = [
'name' => $file->getFilename(), 'name' => $file->getFilename(),
'type' => FileTypeDetector::detect($file->getPathname()), 'type' => FileTypeDetector::detect($file->getPathname()),
'relativePath' => $directory.str_replace($absolutePath, '', $file->getPath()) 'relativePath' => $directory . str_replace($absolutePath, '', $file->getPath())
]; ];
} }
} }

View File

@ -104,13 +104,10 @@ $(document).on('click', '.unzip-btn', function () {
dataType: "json", // 期望从服务器接收json格式的响应 dataType: "json", // 期望从服务器接收json格式的响应
success: function (response) { success: function (response) {
// 如果服务器返回的状态码是200说明解压成功 // 如果服务器返回的状态码是200说明解压成功
if (response.status === 200) { if (response.status !== 200) {
// 刷新页面,加载到解压后的目录
window.location.href = 'index.php?r=home%2Findex&directory=' + encodeURIComponent(response.directory);
} else {
console.error('Unzip failed: ' + response.message); 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 () { error: function () {
// 处理错误 // 处理错误
@ -683,10 +680,10 @@ $(document).on('click', '#btnSearch', function () {
success: function (response) { success: function (response) {
if (response.status === 'success') { if (response.status === 'success') {
var table = '<table class="table"><tr><th style="display: table-cell;">文件/文件夹名</th><th>位置</th></tr>'; var table = '<table class="table"><tr><th style="display: table-cell;">文件/文件夹名</th><th>位置</th></tr>';
$.each(response.data, function(index, item) { $.each(response.data, function (index, item) {
let path = item.relativePath; let path = item.relativePath;
let correctedPath = path.replace(/\\/g, '/'); let correctedPath = path.replace(/\\/g, '/');
table += '<tr><td style="display: table-cell;"> <i class="' + item.type+'"></i> '+item.name + '</td>' + '<td><a style="text-decoration: underline;" href="index.php?r=home%2Findex&directory=' + correctedPath + '">' + item.relativePath + '</a></td></tr>'; table += '<tr><td style="display: table-cell;"> <i class="' + item.type + '"></i> ' + item.name + '</td>' + '<td><a style="text-decoration: underline;" href="index.php?r=home%2Findex&directory=' + correctedPath + '">' + item.relativePath + '</a></td></tr>';
}); });
table += '</table>'; table += '</table>';
$('#search-result').html(table); $('#search-result').html(table);
@ -699,10 +696,10 @@ $(document).on('click', '#btnSearch', function () {
error: function () { error: function () {
$('#search-result').html('An error occurred while processing your request.'); $('#search-result').html('An error occurred while processing your request.');
}, },
beforeSend: function() { beforeSend: function () {
$('#loading').show(); $('#loading').show();
}, },
complete: function() { complete: function () {
$('#loading').hide(); $('#loading').hide();
}, },
}); });