|
|
@@ -417,4 +417,75 @@ class AuthController
|
|
|
if ($token) return app('json')->success('ok', compact('token'));
|
|
|
return app('json')->fail('未授权');
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 处理包含gzip压缩内容的txt文件,解压拼接后下载
|
|
|
+ * @param Request $request
|
|
|
+ * @return \think\response\Stream|mixed
|
|
|
+ */
|
|
|
+ public function processCompressedFile(Request $request)
|
|
|
+ {
|
|
|
+ // 获取上传的文件
|
|
|
+ $file = $request->file('file');
|
|
|
+ if (!$file) {
|
|
|
+ return app('json')->fail('请上传txt文件');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证文件类型
|
|
|
+ try {
|
|
|
+ $file->validate(['ext' => 'txt']);
|
|
|
+ $info = $file->move(ROOT_PATH . 'runtime/temp');
|
|
|
+ if (!$info) {
|
|
|
+ return app('json')->fail('文件上传失败: ' . $file->getError());
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return app('json')->fail('文件验证失败: ' . $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取文件内容
|
|
|
+ $filePath = $info->getRealPath();
|
|
|
+ $content = file_get_contents($filePath);
|
|
|
+
|
|
|
+ // 删除临时上传文件
|
|
|
+ @unlink($filePath);
|
|
|
+
|
|
|
+ // 按双引号分割内容
|
|
|
+ $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;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($decodedContent)) {
|
|
|
+ return app('json')->fail('未解析到有效内容');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成临时文件
|
|
|
+ $tempFileName = 'decoded_' . time() . '.txt';
|
|
|
+ $tempFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $tempFileName;
|
|
|
+ file_put_contents($tempFilePath, $decodedContent);
|
|
|
+
|
|
|
+ // 触发下载并自动清理临时文件
|
|
|
+ return response()
|
|
|
+ ->file($tempFilePath)
|
|
|
+ ->header('Content-Type', 'text/plain')
|
|
|
+ ->header('Content-Disposition', 'attachment; filename="' . $tempFileName . '"');
|
|
|
+// ->autoDelete($tempFilePath);
|
|
|
+ }
|
|
|
}
|