|
|
@@ -419,13 +419,13 @@ class AuthController
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 处理包含gzip压缩内容的txt文件,解压拼接后保存到public目录并触发下载
|
|
|
+ * 处理包含gzip压缩内容的txt文件,解压后仅保存到public目录
|
|
|
* @param Request $request
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function processCompressedFile(Request $request)
|
|
|
{
|
|
|
- // 获取上传的文件信息(原生PHP方式)
|
|
|
+ // 获取上传的文件信息
|
|
|
$uploadedFile = $_FILES['file'] ?? null;
|
|
|
if (!$uploadedFile || $uploadedFile['error'] !== UPLOAD_ERR_OK) {
|
|
|
return app('json')->fail('文件上传失败: ' . ($uploadedFile['error'] ?? '未知错误'));
|
|
|
@@ -437,51 +437,46 @@ class AuthController
|
|
|
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('文件移动失败,请检查临时目录权限');
|
|
|
}
|
|
|
|
|
|
// 读取文件内容
|
|
|
$content = file_get_contents($tempFilePath);
|
|
|
- // 删除临时文件
|
|
|
- @unlink($tempFilePath);
|
|
|
+ @unlink($tempFilePath); // 删除临时文件
|
|
|
|
|
|
if (empty($content)) {
|
|
|
return app('json')->fail('上传的文件内容为空');
|
|
|
}
|
|
|
|
|
|
- // 按双引号分割内容
|
|
|
+ // 处理压缩内容
|
|
|
$parts = explode('"', $content);
|
|
|
-
|
|
|
- // 处理每个压缩部分
|
|
|
$decodedContent = '';
|
|
|
foreach ($parts as $part) {
|
|
|
$part = trim($part);
|
|
|
if (empty($part)) continue;
|
|
|
|
|
|
- // Base64解码
|
|
|
$decoded = base64_decode($part);
|
|
|
if ($decoded === false) {
|
|
|
return app('json')->fail('Base64解码失败,检查文件格式');
|
|
|
}
|
|
|
|
|
|
- // gzip解压
|
|
|
$uncompressed = gzdecode($decoded);
|
|
|
if ($uncompressed === false) {
|
|
|
return app('json')->fail('gzip解压失败,检查压缩内容');
|
|
|
}
|
|
|
+
|
|
|
$decodedContent .= $uncompressed;
|
|
|
}
|
|
|
|
|
|
@@ -489,25 +484,25 @@ class AuthController
|
|
|
return app('json')->fail('未解析到有效内容');
|
|
|
}
|
|
|
|
|
|
- // 定义保存目录(public/downloads)
|
|
|
+ // 保存到public目录
|
|
|
$saveDir = app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'downloads' . DIRECTORY_SEPARATOR;
|
|
|
if (!is_dir($saveDir)) {
|
|
|
mkdir($saveDir, 0755, true);
|
|
|
}
|
|
|
|
|
|
- // 生成唯一文件名
|
|
|
$fileName = 'decoded_' . date('YmdHis') . '_' . uniqid() . '.txt';
|
|
|
$savePath = $saveDir . $fileName;
|
|
|
|
|
|
- // 保存文件到public目录
|
|
|
+ // 写入文件
|
|
|
if (!file_put_contents($savePath, $decodedContent)) {
|
|
|
return app('json')->fail('文件保存失败,请检查public目录权限');
|
|
|
}
|
|
|
|
|
|
- // 触发浏览器下载
|
|
|
- return response()
|
|
|
- ->file($savePath)
|
|
|
- ->header('Content-Type', 'text/plain')
|
|
|
- ->header('Content-Disposition', 'attachment; filename="' . $fileName . '"');
|
|
|
+ // 仅返回保存成功的信息(无下载功能)
|
|
|
+ return app('json')->success([
|
|
|
+ 'message' => '文件已成功保存到public目录',
|
|
|
+ 'local_path' => $savePath, // 服务器本地保存路径
|
|
|
+ 'access_url' => '/downloads/' . $fileName // 网页访问路径(需确保public为根目录)
|
|
|
+ ]);
|
|
|
}
|
|
|
}
|