解压功能实现
This commit is contained in:
parent
7743e2db67
commit
a522af833d
@ -7,6 +7,7 @@ use app\models\RenameForm;
|
|||||||
use app\models\UploadForm;
|
use app\models\UploadForm;
|
||||||
use app\models\ZipForm;
|
use app\models\ZipForm;
|
||||||
use app\utils\FileTypeDetector;
|
use app\utils\FileTypeDetector;
|
||||||
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
|
||||||
use wapmorgan\UnifiedArchive\Exceptions\FileAlreadyExistsException;
|
use wapmorgan\UnifiedArchive\Exceptions\FileAlreadyExistsException;
|
||||||
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
|
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
|
||||||
use wapmorgan\UnifiedArchive\UnifiedArchive;
|
use wapmorgan\UnifiedArchive\UnifiedArchive;
|
||||||
@ -41,6 +42,7 @@ class HomeController extends Controller
|
|||||||
'download-folder' => ['GET'],
|
'download-folder' => ['GET'],
|
||||||
'multi-ff-zip-dl' => ['POST'],
|
'multi-ff-zip-dl' => ['POST'],
|
||||||
'zip' => ['POST'],
|
'zip' => ['POST'],
|
||||||
|
'unzip' => ['POST'],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
@ -509,4 +511,48 @@ class HomeController extends Controller
|
|||||||
}
|
}
|
||||||
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
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),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,8 +96,27 @@ $(document).on('click', '.batch-zip-btn', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$(document).on('click', '.unzip-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 () {
|
$(document).on('click', '.single-rename-btn', function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user