Skip to content

Commit 4854c2d

Browse files
committed
feat: upload 使用 Filesystem; http 配置 response/upload 嵌套
Made-with: Cursor
1 parent 05b66f9 commit 4854c2d

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{
22
"name": "businessg/hyperf-excel",
33
"type": "library",
4+
"repositories": [
5+
{
6+
"type": "path",
7+
"url": "../base-excel"
8+
}
9+
],
410
"license": "MIT",
511
"keywords": [
612
"php",
@@ -27,7 +33,7 @@
2733
"php": ">=8.1",
2834
"ext-mbstring": "*",
2935
"ext-xlswriter": "*",
30-
"businessg/base-excel": "~1.0.0",
36+
"businessg/base-excel": "*",
3137
"hyperf/redis": "~3.1.0",
3238
"hyperf/async-queue": "~3.1.0",
3339
"hyperf/filesystem": "~3.1.0",

publish/excel.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,35 @@
133133
| info 接口会自动拼接此域名返回完整 URL
134134
| 如果 templateUrl 已是完整地址(http/https 开头),则不拼接
135135
|
136-
| 响应格式:
136+
| 响应格式(response 下):
137137
| - codeField: 响应 JSON 中状态码的字段名,默认 'code'
138138
| - dataField: 响应 JSON 中数据的字段名,默认 'data'
139139
| - messageField: 响应 JSON 中消息的字段名,默认 'message'
140140
| - successCode: 成功时状态码的值,默认 0
141141
|
142+
| 上传配置(upload 接口保存路径):
143+
| - upload.disk: 文件系统磁盘名,对应 config/autoload/file.php 中 storage 的 key
144+
| - upload.dir: 相对路径,导入文件存放目录(相对于 disk 根路径)
145+
|
142146
| 示例: 如果项目约定响应格式为 {"status": 200, "result": {...}, "msg": "ok"}
143-
| 则配置 codeField => 'status', dataField => 'result',
144-
| messageField => 'msg', successCode => 200
147+
| 则配置 response.codeField => 'status', response.dataField => 'result',
148+
| response.messageField => 'msg', response.successCode => 200
145149
|
146150
*/
147151
'http' => [
148152
'enabled' => false,
149153
'prefix' => '',
150154
'middleware' => [],
151155
'domain' => \Hyperf\Support\env('APP_URL', 'http://localhost:9501'),
152-
'codeField' => 'code',
153-
'dataField' => 'data',
154-
'messageField' => 'message',
155-
'successCode' => 0,
156+
'response' => [
157+
'codeField' => 'code',
158+
'dataField' => 'data',
159+
'messageField' => 'message',
160+
'successCode' => 0,
161+
],
162+
'upload' => [
163+
'disk' => 'local',
164+
'dir' => 'excel-import',
165+
],
156166
],
157167
];

src/Http/Controller/ExcelController.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace BusinessG\HyperfExcel\Http\Controller;
66

7+
use BusinessG\BaseExcel\Contract\FilesystemResolverInterface;
78
use BusinessG\BaseExcel\Service\ExcelBusinessService;
9+
use Hyperf\Contract\ConfigInterface;
810
use Hyperf\HttpServer\Contract\RequestInterface;
911
use Hyperf\HttpServer\Contract\ResponseInterface;
1012
use Psr\Container\ContainerInterface;
@@ -15,6 +17,7 @@ class ExcelController
1517
public function __construct(
1618
protected ContainerInterface $container,
1719
protected ExcelBusinessService $service,
20+
protected FilesystemResolverInterface $filesystemResolver,
1821
protected RequestInterface $request,
1922
protected ResponseInterface $response
2023
) {
@@ -89,14 +92,19 @@ public function upload(): PsrResponseInterface
8992
return $this->response->json($this->service->errorResponse(422, '仅支持 xlsx, xls 格式'));
9093
}
9194

92-
$dir = BASE_PATH . '/runtime/excel-import/' . date('Y/m/d');
93-
if (!is_dir($dir)) {
94-
mkdir($dir, 0777, true);
95+
$config = $this->container->get(ConfigInterface::class);
96+
$uploadDisk = $config->get('excel.http.upload.disk', 'local');
97+
$uploadDir = $config->get('excel.http.upload.dir', 'excel-import');
98+
$relativePath = $uploadDir . '/' . date('Y/m/d') . '/' . bin2hex(random_bytes(20)) . '.' . $extension;
99+
100+
$filesystem = $this->filesystemResolver->getFilesystem($uploadDisk);
101+
$stream = fopen($file->getRealPath(), 'r');
102+
$filesystem->writeStream($relativePath, $stream);
103+
if (is_resource($stream)) {
104+
fclose($stream);
95105
}
96106

97-
$fileName = bin2hex(random_bytes(20)) . '.' . $extension;
98-
$fullPath = $dir . '/' . $fileName;
99-
$file->moveTo($fullPath);
107+
$fullPath = $this->getUploadFullPath($config, $uploadDisk, $relativePath);
100108

101109
return $this->response->json(
102110
$this->service->successResponse([
@@ -105,4 +113,13 @@ public function upload(): PsrResponseInterface
105113
])
106114
);
107115
}
116+
117+
protected function getUploadFullPath(ConfigInterface $config, string $disk, string $relativePath): string
118+
{
119+
$root = $config->get('file.storage.' . $disk . '.root');
120+
if ($root !== null && $root !== '') {
121+
return rtrim($root, '/\\') . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $relativePath);
122+
}
123+
return $relativePath;
124+
}
108125
}

0 commit comments

Comments
 (0)