feat(openapi): integrate springdoc and API integration guide
- add springdoc-openapi and expose /v3/api-docs, /v3/api-docs.yaml and Swagger UI - keep API docs responses unwrapped by replacing the global return handler - add Chinese @Tag/@Operation/@Parameter docs for file and async task controllers - add docs/api-integration.md and local export scripts for AI agent consumption - ignore local OpenAPI snapshots and force LF for shell scripts
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# Shell 脚本使用 LF,避免 Windows 下 CRLF 导致执行失败
|
||||
*.sh text eol=lf
|
||||
@@ -6,6 +6,10 @@
|
||||
/AGENTS.md
|
||||
/.workbuddy
|
||||
|
||||
# OpenAPI 本地导出(scripts/export-openapi.*)
|
||||
/docs/openapi.local.yaml
|
||||
/docs/openapi.local.json
|
||||
|
||||
multilingualField/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/multilingualField/
|
||||
|
||||
@@ -0,0 +1,271 @@
|
||||
# TimiServerAPI 接口对接说明
|
||||
|
||||
> 接口的路径、方法、参数、请求体、响应体以 `docs/openapi.local.yaml` 为准。本文只补充 OpenAPI 无法完整表达的横切约定:统一响应包装、鉴权、错误码、分页、二进制响应与 SSE。
|
||||
>
|
||||
> 本文不重复 OpenAPI 中的接口清单和字段说明。
|
||||
|
||||
- 服务版本:`1.0.79`
|
||||
- OpenAPI 版本:`3.1.0`
|
||||
- 默认本地地址:`http://localhost:18092`(无 context-path)
|
||||
- OpenAPI JSON:`/v3/api-docs`
|
||||
- OpenAPI YAML:`/v3/api-docs.yaml`
|
||||
- Swagger UI:`/swagger-ui.html`
|
||||
|
||||
---
|
||||
|
||||
## 1. 文档快照与覆盖范围
|
||||
|
||||
### 1.1 导出本地快照
|
||||
|
||||
仓库提供两个导出脚本,默认输出 `docs/openapi.local.yaml`:
|
||||
|
||||
```powershell
|
||||
# PowerShell
|
||||
.\scripts\export-openapi.ps1
|
||||
|
||||
# 指定地址与文件名(.json 后缀会导出 JSON)
|
||||
.\scripts\export-openapi.ps1 -BaseUrl http://192.168.1.10:18092 -OutFile docs\openapi.local.json
|
||||
```
|
||||
|
||||
```bash
|
||||
# Bash
|
||||
bash scripts/export-openapi.sh
|
||||
|
||||
bash scripts/export-openapi.sh http://192.168.1.10:18092 docs/openapi.local.json
|
||||
```
|
||||
|
||||
`docs/openapi.local.*` 是本地快照,已在 `.gitignore` 中忽略,不应提交。需要给远程 AI Agent 使用时,优先发送该文件,而不是 `localhost` 地址。
|
||||
|
||||
### 1.2 覆盖范围说明
|
||||
|
||||
SpringDoc 会扫描项目中的全部 Controller,因此 OpenAPI 文件里会包含用户、标签、系统设置等模块的接口。本次只对文件模块与异步任务模块的 7 个 Controller 补充了完整的中文 `@Tag` / `@Operation` / `@Parameter` 描述;这些接口的路径、参数、请求体和响应结构均已包含在 OpenAPI 中。其他模块的接口属于 SpringDoc 自动生成的基础描述,业务语义可能不完整,复杂接口请结合源码或联调确认。
|
||||
|
||||
OpenAPI 中还声明了全局 `Token` 安全要求,但登录、注册等公开接口实际可能不需要登录。**是否需要鉴权以接口实际行为为准**;公开接口已在 OpenAPI 中按操作单独标记 `security`。
|
||||
|
||||
---
|
||||
|
||||
## 2. 基础约定
|
||||
|
||||
### 2.1 请求格式
|
||||
|
||||
- 普通接口:`Content-Type: application/json`
|
||||
- 上传类接口:`multipart/form-data`
|
||||
- 字符编码:UTF-8
|
||||
- 时间字段:除特别说明外,均为 **epoch 毫秒**(`long`)
|
||||
- 大小字段:字节(`long`)
|
||||
|
||||
### 2.2 鉴权
|
||||
|
||||
请求头优先使用:
|
||||
|
||||
```http
|
||||
Token: <登录令牌或 API Key>
|
||||
```
|
||||
|
||||
服务端也兼容以下写法:
|
||||
|
||||
```http
|
||||
token: <登录令牌或 API Key>
|
||||
```
|
||||
|
||||
以及 URL 参数:
|
||||
|
||||
```text
|
||||
?token=<登录令牌或 API Key>
|
||||
```
|
||||
|
||||
> 生产环境建议只使用 `Token` 请求头。URL 参数容易进入访问日志、浏览器历史和 Referer。
|
||||
>
|
||||
> 浏览器原生 `EventSource` 不能设置自定义请求头,SSE 场景才使用 `?token=`。
|
||||
|
||||
支持两种凭证:
|
||||
|
||||
1. **登录令牌**:通过登录接口获取,具体接口见 OpenAPI。
|
||||
2. **API Key**:以 `tk_` 开头,格式为 `tk_<8位十六进制>_<32位十六进制>`。API Key 创建后只返回一次明文,不能用于管理 API Key 本身。
|
||||
|
||||
### 2.3 统一响应包装(TimiResponse)
|
||||
|
||||
除二进制、SSE 等特殊接口外,所有 JSON 接口都会被统一包装为:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 20000,
|
||||
"message": "可选,面向用户的提示",
|
||||
"messageCode": "可选,国际化消息键",
|
||||
"data": {}
|
||||
}
|
||||
```
|
||||
|
||||
字段说明:
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `code` | number | 业务代码,**不是 HTTP 状态码** |
|
||||
| `message` | string | 提示信息,可能根据语言返回不同文案 |
|
||||
| `messageCode` | string | 国际化消息键,可能不存在 |
|
||||
| `data` | any | 业务数据;为 `null` 时通常被省略 |
|
||||
|
||||
判断成功请使用:
|
||||
|
||||
```ts
|
||||
const success = response.code <= 30000; // 20000 成功,20001 表示成功但忽略请求
|
||||
```
|
||||
|
||||
不要只根据 HTTP 状态码判断业务结果。普通 JSON 接口即使业务失败,HTTP 状态通常仍是 `200`。
|
||||
|
||||
### 2.4 错误码
|
||||
|
||||
| code | 含义 |
|
||||
| --- | --- |
|
||||
| `20000` | 成功 |
|
||||
| `20001` | 成功,忽略请求 |
|
||||
| `40000` | 缺少参数 / 未登录 |
|
||||
| `40001` | 参数不合法 |
|
||||
| `40002` | 参数已过期 |
|
||||
| `40100` | 无权限 |
|
||||
| `40101` | 权限错误 |
|
||||
| `40102` | 非法请求 / 请求过于频繁 |
|
||||
| `40301` | 数据已存在 |
|
||||
| `40400` | 数据不存在 |
|
||||
| `40401` | 返回数据异常 |
|
||||
| `40402` | 数据被禁用 |
|
||||
| `40403` | 上游服务超时 |
|
||||
| `50000` | 服务异常 |
|
||||
| `50001` | 不支持的操作 |
|
||||
| `50002` | 服务内部变量缺失 |
|
||||
| `50003` | 服务已关闭 |
|
||||
| `50300` | 服务繁忙 |
|
||||
|
||||
### 2.5 多语言
|
||||
|
||||
可通过以下任一方式指定语言:
|
||||
|
||||
```http
|
||||
Accept-Language: zh-CN
|
||||
```
|
||||
|
||||
```text
|
||||
?lang=zh_CN
|
||||
```
|
||||
|
||||
默认 `zh_CN`。
|
||||
|
||||
### 2.6 分页
|
||||
|
||||
分页接口的请求体通常是 `Page<T>`:
|
||||
|
||||
```json
|
||||
{
|
||||
"index": 0,
|
||||
"size": 16,
|
||||
"equalsExample": {},
|
||||
"likesExample": {},
|
||||
"equalsLogic": "AND",
|
||||
"likesLogic": "OR",
|
||||
"orderMap": {
|
||||
"created_at": "DESC"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- `index`:页码,**从 0 开始**,默认 `0`
|
||||
- `size`:每页数量,默认 `16`;部分接口会限制最大值(例如 100)
|
||||
- `equalsExample`:精确匹配示例
|
||||
- `likesExample`:模糊匹配示例
|
||||
- `equalsLogic`:精确条件连接方式,`AND` / `OR`
|
||||
- `likesLogic`:模糊条件连接方式,`AND` / `OR`
|
||||
- `orderMap`:排序字段与方向,字段通常使用数据库下划线命名
|
||||
|
||||
分页响应通常是 `PageResult<T>`:
|
||||
|
||||
```json
|
||||
{
|
||||
"total": 128,
|
||||
"pages": 8,
|
||||
"list": [],
|
||||
"empty": false,
|
||||
"notEmpty": true
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. 特殊响应类型
|
||||
|
||||
### 3.1 二进制响应
|
||||
|
||||
部分接口直接返回二进制数据或字节流,不经过 `TimiResponse`:
|
||||
|
||||
- 成功时直接返回字节流
|
||||
- 失败时使用标准 HTTP 状态码,例如 `400`、`403`、`404`、`500`
|
||||
- 响应头可能包含 `Content-Type`、`Content-Length`、`Content-Disposition`、`ETag`、`Last-Modified`、`Accept-Ranges`、`Content-Range`
|
||||
- 前端应使用 `blob` / `arrayBuffer`,不能按 JSON 解析
|
||||
|
||||
具体哪些接口属于二进制响应,以 OpenAPI 中定义的响应 `Content-Type` 为准。
|
||||
|
||||
### 3.2 SSE
|
||||
|
||||
部分长任务接口使用 `text/event-stream`,不经过 `TimiResponse`。事件类型如下:
|
||||
|
||||
| event | 说明 |
|
||||
| --- | --- |
|
||||
| `snapshot` | 连接建立后的当前任务快照 |
|
||||
| `progress` | 状态或进度发生变化 |
|
||||
| `heartbeat` | 心跳,约 15 秒一次 |
|
||||
| `done` | 任务成功,随后服务端关闭连接 |
|
||||
| `error` | 任务失败、被中断、死亡,或订阅异常,随后关闭连接 |
|
||||
|
||||
说明:
|
||||
|
||||
- 事件 `data` 是 JSON,字段和状态枚举以 OpenAPI 中对应的任务模型为准。
|
||||
- 任务结束后服务端主动关闭连接;断线后可回退到 OpenAPI 中的任务详情接口轮询。
|
||||
- 浏览器原生 `EventSource` 不能设置 `Token` 请求头,使用查询参数传递令牌。
|
||||
|
||||
```ts
|
||||
const source = new EventSource(`${sseUrl}?token=${encodeURIComponent(token)}`);
|
||||
|
||||
source.addEventListener("progress", (event) => {
|
||||
const task = JSON.parse(event.data);
|
||||
console.log(task.progress, task.status);
|
||||
});
|
||||
|
||||
source.addEventListener("done", (event) => {
|
||||
const task = JSON.parse(event.data);
|
||||
source.close();
|
||||
});
|
||||
```
|
||||
|
||||
其中 `sseUrl` 从 OpenAPI 中对应的 SSE 接口获取。
|
||||
|
||||
---
|
||||
|
||||
## 4. AI Agent 对接建议
|
||||
|
||||
1. **优先使用导出的 OpenAPI 文件**,不要只给 `localhost` URL。远程 Agent 无法访问本机地址。
|
||||
2. 用 OpenAPI 生成 TypeScript 类型和请求客户端,再在客户端层统一补充 `TimiResponse` 包装。
|
||||
3. 不要假设 OpenAPI 的返回类型就是最终响应体;普通 JSON 接口需要取 `data`。
|
||||
4. 二进制接口单独处理:使用 `blob` / `arrayBuffer`,按 HTTP 状态判断成败。
|
||||
5. SSE 单独处理:使用 `EventSource` 或 fetch 流式读取,`Token` 放查询参数。
|
||||
6. 分页接口使用 `Page<T>` / `PageResult<T>`,`index` 从 0 开始。
|
||||
7. 业务错误优先看 `code`,不要只看 HTTP 状态。
|
||||
8. 接口路径、参数和字段结构以 OpenAPI 为准,不要根据本文猜测。
|
||||
|
||||
推荐给 AI Agent 的简要约束:
|
||||
|
||||
```text
|
||||
这是一个 Spring Boot 接口项目。接口路径、参数和响应结构以 OpenAPI 为准。
|
||||
普通 JSON 接口返回 TimiResponse 包装:{ code, message?, messageCode?, data? },
|
||||
code <= 30000 表示成功。鉴权使用 Token 请求头。
|
||||
二进制接口不包装,按 HTTP 状态判断;SSE 接口不包装,Token 放查询参数。
|
||||
分页请求使用 Page<T>,index 从 0 开始。
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. 已知限制
|
||||
|
||||
- OpenAPI 的响应 Schema 不包含 `TimiResponse` 包装。
|
||||
- 只有文件模块和异步任务模块的 7 个 Controller 有较完整的中文接口描述,其他模块为基础自动生成信息。
|
||||
- 二进制接口的响应体无法在 OpenAPI 中完整表达,需结合本文和实际响应头处理。
|
||||
- OpenAPI 全局声明了 `Token`,公开接口的实际鉴权行为以接口为准。
|
||||
- 当前没有提交固定的 `docs/openapi.yaml` 版本化文件;需要时使用导出脚本生成本地快照,或后续接入 CI 导出。
|
||||
@@ -195,6 +195,11 @@
|
||||
<artifactId>timi-spring</artifactId>
|
||||
<version>0.0.20</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.10.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
导出本地 OpenAPI 文档。
|
||||
|
||||
.DESCRIPTION
|
||||
从本地已启动的服务获取 /v3/api-docs.yaml(或 JSON),写入 docs 目录。
|
||||
生成的文件已被 .gitignore 忽略,不会提交。
|
||||
|
||||
.PARAMETER BaseUrl
|
||||
服务地址,默认 http://localhost:18092。
|
||||
|
||||
.PARAMETER OutFile
|
||||
输出文件路径,默认 docs/openapi.local.yaml。
|
||||
扩展名为 .json 时导出 JSON,否则导出 YAML。
|
||||
|
||||
.EXAMPLE
|
||||
.\scripts\export-openapi.ps1
|
||||
|
||||
.EXAMPLE
|
||||
.\scripts\export-openapi.ps1 -BaseUrl http://192.168.1.10:18092 -OutFile docs/openapi.local.json
|
||||
#>
|
||||
param(
|
||||
[string]$BaseUrl = "http://localhost:18092",
|
||||
[string]$OutFile
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($OutFile)) {
|
||||
$OutFile = Join-Path (Split-Path -Parent $PSScriptRoot) "docs\openapi.local.yaml"
|
||||
}
|
||||
|
||||
$BaseUrl = $BaseUrl.TrimEnd("/")
|
||||
$OutFile = [System.IO.Path]::GetFullPath($OutFile)
|
||||
$OutDir = Split-Path -Parent $OutFile
|
||||
|
||||
if (-not (Test-Path -LiteralPath $OutDir)) {
|
||||
New-Item -ItemType Directory -Path $OutDir -Force | Out-Null
|
||||
}
|
||||
|
||||
if ([System.IO.Path]::GetExtension($OutFile) -ieq ".json") {
|
||||
$ApiPath = "/v3/api-docs"
|
||||
} else {
|
||||
$ApiPath = "/v3/api-docs.yaml"
|
||||
}
|
||||
|
||||
$Url = "$BaseUrl$ApiPath"
|
||||
Write-Host "Fetching $Url"
|
||||
Invoke-WebRequest -Uri $Url -OutFile $OutFile -UseBasicParsing
|
||||
Write-Host "OpenAPI exported to $OutFile"
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env bash
|
||||
# 导出本地 OpenAPI 文档。
|
||||
#
|
||||
# 用法:
|
||||
# scripts/export-openapi.sh [baseUrl] [outFile]
|
||||
#
|
||||
# 输出文件扩展名为 .json 时导出 JSON,否则导出 YAML。
|
||||
# 生成的文件已被 .gitignore 忽略,不会提交。
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
BASE_URL="${1:-http://localhost:18092}"
|
||||
OUT_FILE="${2:-$ROOT_DIR/docs/openapi.local.yaml}"
|
||||
|
||||
BASE_URL="${BASE_URL%/}"
|
||||
mkdir -p "$(dirname "$OUT_FILE")"
|
||||
|
||||
case "$OUT_FILE" in
|
||||
*.json)
|
||||
API_PATH="/v3/api-docs"
|
||||
;;
|
||||
*)
|
||||
API_PATH="/v3/api-docs.yaml"
|
||||
;;
|
||||
esac
|
||||
|
||||
echo "Fetching ${BASE_URL}${API_PATH}"
|
||||
curl -fsS "${BASE_URL}${API_PATH}" -o "$OUT_FILE"
|
||||
echo "OpenAPI exported to $OUT_FILE"
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.imyeyu.api.config;
|
||||
|
||||
import io.swagger.v3.oas.models.Components;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* OpenAPI 接口文档配置
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-11-24
|
||||
*/
|
||||
@Configuration
|
||||
public class OpenApiConfig {
|
||||
|
||||
/**
|
||||
* 移除 timi-spring 默认的全局返回处理器,由 {@link OpenApiGlobalReturnHandler} 替代。
|
||||
*
|
||||
* <p>默认处理器会把所有响应包装为 TimiResponse,导致 SpringDoc 的接口描述无法被 Swagger UI 解析。</p>
|
||||
*
|
||||
* @return Bean 定义注册后处理器
|
||||
*/
|
||||
@Bean
|
||||
public static BeanDefinitionRegistryPostProcessor openApiGlobalReturnHandlerReplacer() {
|
||||
return new BeanDefinitionRegistryPostProcessor() {
|
||||
|
||||
@Override
|
||||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
|
||||
for (String beanName : registry.getBeanDefinitionNames()) {
|
||||
BeanDefinition definition = registry.getBeanDefinition(beanName);
|
||||
if ("globalReturnHandler".equals(beanName)
|
||||
|| "com.imyeyu.spring.util.GlobalReturnHandler".equals(definition.getBeanClassName())) {
|
||||
registry.removeBeanDefinition(beanName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口文档信息与认证方式
|
||||
*
|
||||
* @return OpenAPI 配置
|
||||
*/
|
||||
@Bean
|
||||
public OpenAPI timiServerOpenAPI() {
|
||||
return new OpenAPI()
|
||||
.info(new Info()
|
||||
.title("TimiServerAPI")
|
||||
.description("夜雨综合数据中心接口")
|
||||
.version("1.0.79"))
|
||||
.components(new Components()
|
||||
.addSecuritySchemes("Token", new SecurityScheme()
|
||||
.name("Token")
|
||||
.type(SecurityScheme.Type.APIKEY)
|
||||
.in(SecurityScheme.In.HEADER)
|
||||
.description("登录令牌或 API Key,也可通过 URL 参数 token 传递")))
|
||||
.addSecurityItem(new SecurityRequirement().addList("Token"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册字节数组消息转换器,确保接口文档以原始 JSON 输出。
|
||||
*
|
||||
* <p>项目的消息转换器配置仅注册了 Jackson 转换器,{@code byte[]} 会被序列化为 Base64 字符串,
|
||||
* 导致 Swagger UI 无法解析接口文档。</p>
|
||||
*
|
||||
* @return Web MVC 配置
|
||||
*/
|
||||
@Bean
|
||||
public WebMvcConfigurer openApiByteArrayMessageConverter() {
|
||||
return new WebMvcConfigurer() {
|
||||
|
||||
@Override
|
||||
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
converters.add(0, new ByteArrayHttpMessageConverter());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.imyeyu.api.config;
|
||||
|
||||
import com.imyeyu.spring.util.GlobalReturnHandler;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
/**
|
||||
* 全局返回处理器,跳过 SpringDoc 接口。
|
||||
*
|
||||
* <p>接口文档响应需要保持原始 JSON 结构,不能包装为 TimiResponse。</p>
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-11-24
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class OpenApiGlobalReturnHandler extends GlobalReturnHandler {
|
||||
|
||||
@Override
|
||||
public boolean supports(@NonNull MethodParameter returnType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
Class<?> containingClass = returnType.getContainingClass();
|
||||
if (containingClass != null && containingClass.getName().startsWith("org.springdoc.")) {
|
||||
return false;
|
||||
}
|
||||
return super.supports(returnType, converterType);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,9 @@ import com.imyeyu.api.modules.system.vo.AsyncTaskView;
|
||||
import com.imyeyu.api.modules.user.service.UserLoginService;
|
||||
import com.imyeyu.spring.annotation.AOPLog;
|
||||
import com.imyeyu.spring.annotation.IgnoreGlobalReturn;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -26,6 +29,7 @@ import java.util.List;
|
||||
* @author 夜雨
|
||||
* @version 2022-08-13 19:38
|
||||
*/
|
||||
@Tag(name = "系统-异步任务", description = "异步任务查询、控制与进度订阅接口")
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@@ -36,9 +40,12 @@ public class AsyncTaskController {
|
||||
private final AsyncTaskEventService eventService;
|
||||
private final UserLoginService userLoginService;
|
||||
|
||||
@Operation(summary = "创建调试任务", description = "创建一个持续指定秒数的调试任务,返回任务 ID。")
|
||||
@AOPLog
|
||||
@PostMapping("/debug")
|
||||
public String debug(@RequestParam("second") Integer second) {
|
||||
public String debug(
|
||||
@Parameter(description = "任务持续秒数,默认 60")
|
||||
@RequestParam(value = "second", required = false) Integer second) {
|
||||
if (second == null) {
|
||||
second = 60;
|
||||
}
|
||||
@@ -47,8 +54,11 @@ public class AsyncTaskController {
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
@Operation(summary = "获取任务详情")
|
||||
@GetMapping("/detail")
|
||||
public AbstractAsyncTask detail(@RequestParam ("uuid") String uuid) {
|
||||
public AbstractAsyncTask detail(
|
||||
@Parameter(description = "任务 ID", required = true)
|
||||
@RequestParam("uuid") String uuid) {
|
||||
return service.getAsyncTask(uuid);
|
||||
}
|
||||
|
||||
@@ -61,32 +71,45 @@ public class AsyncTaskController {
|
||||
* @param uuid 任务 ID
|
||||
* @return SSE 事件流
|
||||
*/
|
||||
@Operation(summary = "订阅任务事件", description = "以 SSE 推送任务状态与进度,任务结束后服务端主动关闭连接。")
|
||||
@IgnoreGlobalReturn
|
||||
@GetMapping(value = "/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public SseEmitter events(@RequestParam("uuid") String uuid) {
|
||||
public SseEmitter events(
|
||||
@Parameter(description = "任务 ID", required = true)
|
||||
@RequestParam("uuid") String uuid) {
|
||||
return eventService.subscribe(uuid, userLoginService.getRequireLoginUserId());
|
||||
}
|
||||
|
||||
@Operation(summary = "获取任务列表")
|
||||
@PostMapping("/list")
|
||||
public List<AsyncTaskView> list() {
|
||||
return service.listAllView();
|
||||
}
|
||||
|
||||
@Operation(summary = "启动任务")
|
||||
@AOPLog
|
||||
@PostMapping("/start")
|
||||
public void start(@RequestParam ("uuid") String uuid) {
|
||||
public void start(
|
||||
@Parameter(description = "任务 ID", required = true)
|
||||
@RequestParam("uuid") String uuid) {
|
||||
service.start(uuid);
|
||||
}
|
||||
|
||||
@Operation(summary = "暂停任务")
|
||||
@AOPLog
|
||||
@PostMapping("/pause")
|
||||
public void pause(@RequestParam ("uuid") String uuid) {
|
||||
public void pause(
|
||||
@Parameter(description = "任务 ID", required = true)
|
||||
@RequestParam("uuid") String uuid) {
|
||||
service.pause(uuid);
|
||||
}
|
||||
|
||||
@Operation(summary = "中断任务")
|
||||
@AOPLog
|
||||
@PostMapping("/interrupt")
|
||||
public void interrupt(@RequestParam ("uuid") String uuid) {
|
||||
public void interrupt(
|
||||
@Parameter(description = "任务 ID", required = true)
|
||||
@RequestParam("uuid") String uuid) {
|
||||
service.interrupt(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ import com.imyeyu.api.modules.user.service.PermissionChecker;
|
||||
import com.imyeyu.api.modules.user.service.UserLoginService;
|
||||
import com.imyeyu.spring.annotation.AOPLog;
|
||||
import com.imyeyu.spring.annotation.RequestRateLimit;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -31,6 +33,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
* @author 夜雨
|
||||
* @version 2026-11-24
|
||||
*/
|
||||
@Tag(name = "系统-文件归档", description = "文件压缩、解压、复制与移动异步任务接口")
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/system/file")
|
||||
@@ -41,6 +44,7 @@ public class FileArchiveController {
|
||||
private final UserLoginService userLoginService;
|
||||
private final PermissionChecker permissionChecker;
|
||||
|
||||
@Operation(summary = "创建 ZIP 压缩任务", description = "将 list 中的文件或目录压缩到 to,返回异步任务 ID。")
|
||||
@AOPLog
|
||||
@OperationAudit(AuditOperation.FILE_ARCHIVE)
|
||||
@RequestRateLimit
|
||||
@@ -52,6 +56,7 @@ public class FileArchiveController {
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
@Operation(summary = "创建 ZIP 解压任务", description = "将 file 解压到 to,返回异步任务 ID。")
|
||||
@AOPLog
|
||||
@OperationAudit(AuditOperation.FILE_ARCHIVE)
|
||||
@RequestRateLimit
|
||||
@@ -63,6 +68,7 @@ public class FileArchiveController {
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
@Operation(summary = "创建 TAR 压缩任务", description = "将 list 中的文件或目录打包到 to,返回异步任务 ID。")
|
||||
@AOPLog
|
||||
@OperationAudit(AuditOperation.FILE_ARCHIVE)
|
||||
@RequestRateLimit
|
||||
@@ -74,6 +80,7 @@ public class FileArchiveController {
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
@Operation(summary = "创建 TAR 解压任务", description = "将 file 解压到 to,返回异步任务 ID。")
|
||||
@AOPLog
|
||||
@OperationAudit(AuditOperation.FILE_ARCHIVE)
|
||||
@RequestRateLimit
|
||||
@@ -85,6 +92,7 @@ public class FileArchiveController {
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
@Operation(summary = "创建移动任务", description = "将 list 中的文件或目录移动到 to,返回异步任务 ID。")
|
||||
@AOPLog
|
||||
@OperationAudit(AuditOperation.FILE_ARCHIVE)
|
||||
@RequestRateLimit
|
||||
@@ -96,6 +104,7 @@ public class FileArchiveController {
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
@Operation(summary = "创建复制任务", description = "将 list 中的文件或目录复制到 to,返回异步任务 ID。")
|
||||
@AOPLog
|
||||
@OperationAudit(AuditOperation.FILE_ARCHIVE)
|
||||
@RequestRateLimit
|
||||
|
||||
@@ -5,6 +5,9 @@ import com.imyeyu.api.bean.CorePermissionCode;
|
||||
import com.imyeyu.api.modules.system.service.FileContentService;
|
||||
import com.imyeyu.spring.annotation.IgnoreGlobalReturn;
|
||||
import com.imyeyu.spring.annotation.RequestRateLimit;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -20,6 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
* @author 夜雨
|
||||
* @version 2026-11-24
|
||||
*/
|
||||
@Tag(name = "系统-文件内容", description = "文件内容读取、下载与预览接口")
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/system/file")
|
||||
@@ -27,27 +31,39 @@ public class FileContentController {
|
||||
|
||||
private final FileContentService contentService;
|
||||
|
||||
@Operation(summary = "读取文件", description = "以内联方式输出文件内容,支持 Range 与条件请求。")
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@IgnoreGlobalReturn
|
||||
@GetMapping("/read/{*path}")
|
||||
public void read(HttpServletRequest req, HttpServletResponse resp, @PathVariable("path") String path) {
|
||||
public void read(
|
||||
HttpServletRequest req, HttpServletResponse resp,
|
||||
@Parameter(description = "文件虚拟路径,以 / 开头", required = true)
|
||||
@PathVariable("path") String path) {
|
||||
FileBinaryResponder.serve(resp, () -> contentService.serve(req, resp, path, false));
|
||||
}
|
||||
|
||||
@Operation(summary = "下载文件", description = "以附件方式输出文件内容,支持 Range 与 HEAD 请求。")
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@IgnoreGlobalReturn
|
||||
@RequestMapping(value = "/download/{*path}", method = {RequestMethod.GET, RequestMethod.HEAD})
|
||||
public void download(HttpServletRequest req, HttpServletResponse resp, @PathVariable("path") String path) {
|
||||
public void download(
|
||||
HttpServletRequest req, HttpServletResponse resp,
|
||||
@Parameter(description = "文件虚拟路径,以 / 开头", required = true)
|
||||
@PathVariable("path") String path) {
|
||||
FileBinaryResponder.serve(resp, () -> contentService.serve(req, resp, path, true));
|
||||
}
|
||||
|
||||
@Operation(summary = "预览文件", description = "图片和视频返回 PNG 缩略图,PDF 与文本返回原始内容。")
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@IgnoreGlobalReturn
|
||||
@GetMapping("/preview/{*path}")
|
||||
public void preview(HttpServletRequest req, HttpServletResponse resp, @PathVariable("path") String path) {
|
||||
public void preview(
|
||||
HttpServletRequest req, HttpServletResponse resp,
|
||||
@Parameter(description = "文件虚拟路径,以 / 开头", required = true)
|
||||
@PathVariable("path") String path) {
|
||||
FileBinaryResponder.serve(resp, () -> contentService.preview(req, resp, path));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@ import com.imyeyu.api.modules.user.service.PermissionChecker;
|
||||
import com.imyeyu.api.modules.user.service.UserLoginService;
|
||||
import com.imyeyu.spring.annotation.AOPLog;
|
||||
import com.imyeyu.spring.annotation.RequestRateLimit;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -33,6 +36,7 @@ import java.util.List;
|
||||
* @author 夜雨
|
||||
* @version 2022-01-01 20:00
|
||||
*/
|
||||
@Tag(name = "系统-文件", description = "文件元数据、目录与检索接口")
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/system/file")
|
||||
@@ -44,22 +48,29 @@ public class FileController {
|
||||
private final UserLoginService userLoginService;
|
||||
private final PermissionChecker permissionChecker;
|
||||
|
||||
@Operation(summary = "获取文件属性", description = "返回指定路径的文件或目录属性。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@GetMapping("/object/{*path}")
|
||||
public ServerFile object(@PathVariable("path") String path) {
|
||||
public ServerFile object(
|
||||
@Parameter(description = "文件虚拟路径,以 / 开头", required = true)
|
||||
@PathVariable("path") String path) {
|
||||
return fileService.object(path);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取目录列表", description = "返回指定目录下的文件与子目录列表。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@GetMapping("/list")
|
||||
public List<ServerFile> list(@RequestParam(defaultValue = "/") String path) {
|
||||
public List<ServerFile> list(
|
||||
@Parameter(description = "目录虚拟路径,默认为根目录 /")
|
||||
@RequestParam(defaultValue = "/") String path) {
|
||||
return fileService.list(path);
|
||||
}
|
||||
|
||||
@Operation(summary = "搜索文件", description = "在指定目录下按关键字搜索,返回匹配项、是否截断及扫描数量。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@@ -68,22 +79,29 @@ public class FileController {
|
||||
return fileService.search(request.path(), request.keyword());
|
||||
}
|
||||
|
||||
@Operation(summary = "判断路径是否存在")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@GetMapping("/exist")
|
||||
public boolean exist(@RequestParam String path) {
|
||||
public boolean exist(
|
||||
@Parameter(description = "文件或目录虚拟路径", required = true)
|
||||
@RequestParam String path) {
|
||||
return fileService.exists(path);
|
||||
}
|
||||
|
||||
@Operation(summary = "计算文件 SHA-256", description = "同步计算单个文件的 SHA-256,大文件请谨慎调用。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@GetMapping("/hash")
|
||||
public FileService.FileHash hash(@RequestParam String path) {
|
||||
public FileService.FileHash hash(
|
||||
@Parameter(description = "文件虚拟路径", required = true)
|
||||
@RequestParam String path) {
|
||||
return fileService.hash(path);
|
||||
}
|
||||
|
||||
@Operation(summary = "统计目录大小", description = "创建异步任务统计目录大小,返回任务 ID,可通过异步任务接口订阅进度。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@@ -96,6 +114,7 @@ public class FileController {
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
@Operation(summary = "创建目录")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_WRITE)
|
||||
@@ -104,6 +123,7 @@ public class FileController {
|
||||
return fileService.mkdir(request.path(), request.name());
|
||||
}
|
||||
|
||||
@Operation(summary = "重命名或移动", description = "将文件或目录从 from 重命名或移动到 to。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_WRITE)
|
||||
|
||||
@@ -12,6 +12,10 @@ import com.imyeyu.api.modules.user.service.UserLoginService;
|
||||
import com.imyeyu.spring.annotation.AOPLog;
|
||||
import com.imyeyu.spring.annotation.IgnoreGlobalReturn;
|
||||
import com.imyeyu.spring.annotation.RequestRateLimit;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
@@ -34,6 +38,7 @@ import java.util.List;
|
||||
* @author 夜雨
|
||||
* @version 2026-11-24
|
||||
*/
|
||||
@Tag(name = "系统-文件分享", description = "文件分享创建、管理与公开下载接口")
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/system/file")
|
||||
@@ -44,6 +49,7 @@ public class FileShareController {
|
||||
private final UserLoginService userLoginService;
|
||||
private final LoggerService loggerService;
|
||||
|
||||
@Operation(summary = "创建文件分享", description = "对指定文件创建分享链接,可设置访问密码、过期时间与最大下载次数。")
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission({CorePermissionCode.FILE_READ, CorePermissionCode.FILE_SHARE})
|
||||
@PostMapping("/share")
|
||||
@@ -51,6 +57,7 @@ public class FileShareController {
|
||||
return shareService.create(request.path(), request.password(), request.expiresAt(), request.maxDownloads(), userLoginService.getRequireLoginUserId());
|
||||
}
|
||||
|
||||
@Operation(summary = "获取分享列表")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_SHARE)
|
||||
@@ -59,19 +66,29 @@ public class FileShareController {
|
||||
return shareService.list();
|
||||
}
|
||||
|
||||
@Operation(summary = "取消文件分享")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_SHARE)
|
||||
@DeleteMapping("/share/{id}")
|
||||
public FileShare revokeShare(@PathVariable String id) {
|
||||
public FileShare revokeShare(
|
||||
@Parameter(description = "分享记录 ID", required = true)
|
||||
@PathVariable String id) {
|
||||
return shareService.revoke(id);
|
||||
}
|
||||
|
||||
/// 公开分享下载,只校验令牌与密码,不要求登录权限。
|
||||
@Operation(summary = "下载分享文件", description = "公开接口,仅校验分享令牌与访问密码,不要求登录。")
|
||||
@SecurityRequirements
|
||||
@RequestRateLimit
|
||||
@IgnoreGlobalReturn
|
||||
@RequestMapping(value = "/share/{token}/download", method = {RequestMethod.GET, RequestMethod.HEAD})
|
||||
public void downloadShare(@PathVariable String token, @RequestParam(required = false) String password, HttpServletRequest req, HttpServletResponse resp) {
|
||||
public void downloadShare(
|
||||
@Parameter(description = "分享令牌", required = true)
|
||||
@PathVariable String token,
|
||||
@Parameter(description = "分享访问密码,未设置密码时省略")
|
||||
@RequestParam(required = false) String password,
|
||||
HttpServletRequest req, HttpServletResponse resp) {
|
||||
FileBinaryResponder.serve(resp, () -> {
|
||||
boolean consume = !"HEAD".equalsIgnoreCase(req.getMethod());
|
||||
FileShare share = shareService.open(token, password, consume);
|
||||
|
||||
@@ -10,6 +10,9 @@ import com.imyeyu.api.modules.system.vo.IdRequest;
|
||||
import com.imyeyu.api.modules.user.service.UserLoginService;
|
||||
import com.imyeyu.spring.annotation.AOPLog;
|
||||
import com.imyeyu.spring.annotation.RequestRateLimit;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
@@ -28,6 +31,7 @@ import java.util.List;
|
||||
* @author 夜雨
|
||||
* @version 2026-11-24
|
||||
*/
|
||||
@Tag(name = "系统-文件回收站", description = "文件删除、恢复与回收站清理接口")
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/system/file")
|
||||
@@ -36,6 +40,7 @@ public class FileTrashController {
|
||||
private final FileTrashService trashService;
|
||||
private final UserLoginService userLoginService;
|
||||
|
||||
@Operation(summary = "删除到回收站", description = "将 list 中的文件或目录移入回收站,返回删除记录。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_DELETE)
|
||||
@@ -44,6 +49,7 @@ public class FileTrashController {
|
||||
return trashService.trash(request.list(), userLoginService.getRequireLoginUserId());
|
||||
}
|
||||
|
||||
@Operation(summary = "获取回收站列表")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@@ -52,6 +58,7 @@ public class FileTrashController {
|
||||
return trashService.list();
|
||||
}
|
||||
|
||||
@Operation(summary = "恢复回收站文件", description = "将回收站记录恢复到删除前的路径。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_WRITE)
|
||||
@@ -60,14 +67,18 @@ public class FileTrashController {
|
||||
return trashService.restore(request.id());
|
||||
}
|
||||
|
||||
@Operation(summary = "彻底删除回收站记录")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_DELETE)
|
||||
@DeleteMapping("/trash/{id}")
|
||||
public void purge(@PathVariable String id) {
|
||||
public void purge(
|
||||
@Parameter(description = "回收站记录 ID", required = true)
|
||||
@PathVariable String id) {
|
||||
trashService.purge(id);
|
||||
}
|
||||
|
||||
@Operation(summary = "清空回收站", description = "彻底删除回收站中的全部记录,返回清理数量。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_DELETE)
|
||||
|
||||
@@ -8,6 +8,9 @@ import com.imyeyu.api.modules.system.vo.UploadCompleteRequest;
|
||||
import com.imyeyu.api.modules.system.vo.UploadInitRequest;
|
||||
import com.imyeyu.spring.annotation.AOPLog;
|
||||
import com.imyeyu.spring.annotation.RequestRateLimit;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -24,6 +27,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
* @author 夜雨
|
||||
* @version 2026-11-24
|
||||
*/
|
||||
@Tag(name = "系统-文件上传", description = "文件直传与分片上传接口")
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/system/file")
|
||||
@@ -31,19 +35,25 @@ public class FileUploadController {
|
||||
|
||||
private final FileUploadService uploadService;
|
||||
|
||||
@Operation(summary = "上传文件", description = "以 multipart/form-data 直接上传文件,overwrite 为 true 时覆盖同名文件。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_WRITE)
|
||||
@PostMapping("/upload")
|
||||
public ServerFile upload(
|
||||
@Parameter(description = "目标目录虚拟路径", required = true)
|
||||
@RequestParam String path,
|
||||
@Parameter(description = "保存的文件名", required = true)
|
||||
@RequestParam String name,
|
||||
@Parameter(description = "是否覆盖同名文件,默认 false")
|
||||
@RequestParam(defaultValue = "false") boolean overwrite,
|
||||
@Parameter(description = "文件内容", required = true)
|
||||
@RequestPart("file") MultipartFile file
|
||||
) {
|
||||
return uploadService.upload(path, name, file, overwrite);
|
||||
}
|
||||
|
||||
@Operation(summary = "初始化分片上传", description = "按文件大小与 SHA-256 创建上传会话,返回分片大小与分片数量。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_WRITE)
|
||||
@@ -52,13 +62,21 @@ public class FileUploadController {
|
||||
return uploadService.init(request.path(), request.name(), request.size(), request.sha256());
|
||||
}
|
||||
|
||||
@Operation(summary = "上传分片", description = "上传指定索引的分片,分片索引从 0 开始。")
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_WRITE)
|
||||
@PostMapping("/upload/chunk")
|
||||
public void uploadChunk(@RequestParam String uploadId, @RequestParam int index, @RequestPart("file") MultipartFile file) {
|
||||
public void uploadChunk(
|
||||
@Parameter(description = "上传会话 ID", required = true)
|
||||
@RequestParam String uploadId,
|
||||
@Parameter(description = "分片索引,从 0 开始", required = true)
|
||||
@RequestParam int index,
|
||||
@Parameter(description = "分片内容", required = true)
|
||||
@RequestPart("file") MultipartFile file) {
|
||||
uploadService.chunk(uploadId, index, file);
|
||||
}
|
||||
|
||||
@Operation(summary = "完成分片上传", description = "合并全部分片并校验 SHA-256,返回最终文件属性。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_WRITE)
|
||||
|
||||
@@ -31,6 +31,12 @@ spring:
|
||||
required: true
|
||||
default-encoding: UTF-8
|
||||
|
||||
# OpenAPI 接口文档
|
||||
springdoc:
|
||||
swagger-ui:
|
||||
tags-sorter: alpha
|
||||
operations-sorter: method
|
||||
|
||||
# GAO 智能体写能力
|
||||
gao:
|
||||
ai:
|
||||
|
||||
Reference in New Issue
Block a user