|
|
@@ -418,37 +418,49 @@ class AuthController
|
|
|
return app('json')->fail('未授权');
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 处理包含gzip压缩内容的txt文件,解压拼接后保存到public目录并触发下载
|
|
|
+ * @param Request $request
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
public function processCompressedFile(Request $request)
|
|
|
{
|
|
|
- // 获取上传的文件
|
|
|
- $file = $request->file('file');
|
|
|
- if (!$file) {
|
|
|
- return app('json')->fail('请上传txt文件');
|
|
|
+ // 获取上传的文件信息(原生PHP方式)
|
|
|
+ $uploadedFile = $_FILES['file'] ?? null;
|
|
|
+ if (!$uploadedFile || $uploadedFile['error'] !== UPLOAD_ERR_OK) {
|
|
|
+ return app('json')->fail('文件上传失败: ' . ($uploadedFile['error'] ?? '未知错误'));
|
|
|
}
|
|
|
|
|
|
// 验证文件类型
|
|
|
- try {
|
|
|
-// $file->validate(['ext' => 'txt']);
|
|
|
- // 使用框架方法获取根目录,替代ROOT_PATH
|
|
|
- $tempDir = app()->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'temp';
|
|
|
- // 确保临时目录存在
|
|
|
- if (!is_dir($tempDir)) {
|
|
|
- mkdir($tempDir, 0755, true);
|
|
|
- }
|
|
|
- $info = $file->move($tempDir);
|
|
|
- if (!$info) {
|
|
|
- return app('json')->fail('文件上传失败: ' . $file->getError());
|
|
|
- }
|
|
|
- } catch (\Exception $e) {
|
|
|
- return app('json')->fail('文件验证失败: ' . $e->getMessage());
|
|
|
+ $fileExt = pathinfo($uploadedFile['name'], PATHINFO_EXTENSION);
|
|
|
+ if (strtolower($fileExt) !== 'txt') {
|
|
|
+ return app('json')->fail('请上传txt格式的文件');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 定义临时目录(确保是字符串路径)
|
|
|
+ $tempDir = app()->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR;
|
|
|
+ // 确保临时目录存在
|
|
|
+ if (!is_dir($tempDir)) {
|
|
|
+ mkdir($tempDir, 0755, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成临时文件名(避免重名)
|
|
|
+ $tempFileName = uniqid() . '.txt';
|
|
|
+ $tempFilePath = $tempDir . $tempFileName;
|
|
|
+
|
|
|
+ // 手动移动上传文件(原生函数,确保参数是字符串路径)
|
|
|
+ if (!move_uploaded_file($uploadedFile['tmp_name'], $tempFilePath)) {
|
|
|
+ return app('json')->fail('文件移动失败,请检查临时目录权限');
|
|
|
}
|
|
|
|
|
|
// 读取文件内容
|
|
|
- $filePath = $info->getRealPath();
|
|
|
- $content = file_get_contents($filePath);
|
|
|
+ $content = file_get_contents($tempFilePath);
|
|
|
+ // 删除临时文件
|
|
|
+ @unlink($tempFilePath);
|
|
|
|
|
|
- // 删除临时上传文件
|
|
|
- @unlink($filePath);
|
|
|
+ if (empty($content)) {
|
|
|
+ return app('json')->fail('上传的文件内容为空');
|
|
|
+ }
|
|
|
|
|
|
// 按双引号分割内容
|
|
|
$parts = explode('"', $content);
|
|
|
@@ -478,23 +490,22 @@ class AuthController
|
|
|
return app('json')->fail('未解析到有效内容');
|
|
|
}
|
|
|
|
|
|
- // 定义保存目录(public/downloads),使用框架方法获取根目录
|
|
|
- $saveDir = app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'downloads';
|
|
|
- // 自动创建目录(如果不存在)
|
|
|
+ // 定义保存目录(public/downloads)
|
|
|
+ $saveDir = app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'downloads' . DIRECTORY_SEPARATOR;
|
|
|
if (!is_dir($saveDir)) {
|
|
|
- mkdir($saveDir, 0755, true); // 0755权限,允许读写执行
|
|
|
+ mkdir($saveDir, 0755, true);
|
|
|
}
|
|
|
|
|
|
- // 生成唯一文件名(避免重名)
|
|
|
+ // 生成唯一文件名
|
|
|
$fileName = 'decoded_' . date('YmdHis') . '_' . uniqid() . '.txt';
|
|
|
- $savePath = $saveDir . DIRECTORY_SEPARATOR . $fileName;
|
|
|
+ $savePath = $saveDir . $fileName;
|
|
|
|
|
|
// 保存文件到public目录
|
|
|
if (!file_put_contents($savePath, $decodedContent)) {
|
|
|
return app('json')->fail('文件保存失败,请检查public目录权限');
|
|
|
}
|
|
|
|
|
|
- // 触发浏览器下载(同时保留本地文件)
|
|
|
+ // 触发浏览器下载
|
|
|
return response()
|
|
|
->file($savePath)
|
|
|
->header('Content-Type', 'text/plain')
|