From 6e7580b8dc8e0b385c07f736ad8ee19c383f20a5 Mon Sep 17 00:00:00 2001 From: Marrrrrrrrry <34876935+Marrrrrrrrry@users.noreply.github.com> Date: Fri, 11 Sep 2026 03:37:24 +0800 Subject: [PATCH] docs: refresh API reference - real version, snake_case fields, EN presign page API version 2.1.0 -> 2.5.6; response examples use the single message field; dashboard/list examples and params match current snake_case contract; drop stale save_path from presign init response; add English presign-upload page and sidebar entry; uploadSize -> upload_size. --- docs/.vitepress/config.mts | 1 + docs/api/index.md | 32 +-- docs/api/presign-upload.md | 5 +- docs/en/api/index.md | 32 +-- docs/en/api/presign-upload.md | 389 ++++++++++++++++++++++++++++++++++ 5 files changed, 425 insertions(+), 34 deletions(-) create mode 100644 docs/en/api/presign-upload.md diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index b360d64..efd8ddb 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -205,6 +205,7 @@ export default defineConfig({ text: 'API Reference', items: [ { text: 'API Overview', link: '/en/api/' }, + { text: 'Presigned Upload', link: '/en/api/presign-upload' }, { text: 'Share API', link: '/en/api/#share-api' }, { text: 'Admin API', link: '/en/api/#admin-api' }, { text: 'Error Response', link: '/en/api/#error-response' }, diff --git a/docs/api/index.md b/docs/api/index.md index aefbde3..f2456cb 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -1,6 +1,6 @@ # FileCodeBox API 文档 -## API 版本: 2.1.0 +## API 版本: 2.5.6 ## 目录 - [认证](#认证) @@ -30,7 +30,7 @@ curl -X POST "http://localhost:12345/admin/login" \ ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "token": "xxx.xxx.xxx", "token_type": "Bearer" @@ -74,7 +74,7 @@ curl -X POST "http://localhost:12345/share/text/" \ ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "code": "abc123" } @@ -125,7 +125,7 @@ curl -X POST "http://localhost:12345/share/file/" \ ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "code": "abc123", "name": "example.txt" @@ -157,7 +157,7 @@ curl -L "http://localhost:12345/share/select/?code=abc123" -o downloaded_file ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "code": "abc123", "name": "example.txt", @@ -184,7 +184,7 @@ curl -L "http://localhost:12345/share/select/?code=abc123" -o downloaded_file ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "code": "abc123", "name": "example.txt", @@ -232,15 +232,15 @@ curl -L "http://localhost:12345/share/select/?code=abc123" -o downloaded_file ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { - "totalFiles": 100, - "storageUsed": "1.5GB", - "sysUptime": "10天", - "yesterdayCount": 50, - "yesterdaySize": "500MB", - "todayCount": 30, - "todaySize": "300MB" + "total_files": 100, + "storage_used": "1610612736", + "sys_uptime": 1725489600.0, + "yesterday_count": 50, + "yesterday_size": "524288000", + "today_count": 30, + "today_size": "314572800", } } ``` @@ -258,13 +258,15 @@ curl -L "http://localhost:12345/share/select/?code=abc123" -o downloaded_file | page | integer | 否 | 1 | 当前页码 | | size | integer | 否 | 10 | 每页数量 | | keyword | string | 否 | "" | 搜索关键词 | +| sort_by | string | 否 | "created_at" | 排序字段(created_at/expired_at/name/size/used_count/code) | +| sort_order | string | 否 | "desc" | 排序方向(asc/desc) | **响应示例:** ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "page": 1, "size": 10, diff --git a/docs/api/presign-upload.md b/docs/api/presign-upload.md index d98b20c..282c352 100644 --- a/docs/api/presign-upload.md +++ b/docs/api/presign-upload.md @@ -76,7 +76,6 @@ Content-Type: application/json "upload_id": "a1b2c3d4e5f6...", "upload_url": "https://bucket.s3.amazonaws.com/path?X-Amz-Signature=...", "mode": "direct", - "save_path": "share/data/2024/01/01/uuid/document.pdf", "expires_in": 900 } } @@ -89,7 +88,6 @@ Content-Type: application/json | upload_id | string | 上传会话 ID,后续操作需要 | | upload_url | string | 上传目标 URL | | mode | string | 上传模式:`direct`(直传 S3)或 `proxy`(服务器代理) | -| save_path | string | 文件存储路径 | | expires_in | integer | URL 有效期(秒),默认 900 秒(15 分钟) | **错误响应** @@ -308,7 +306,6 @@ interface PresignInitResponse { upload_id: string upload_url: string mode: 'direct' | 'proxy' - save_path: string expires_in: number } @@ -448,7 +445,7 @@ async function upload() { ## 注意事项 1. **会话有效期**: 上传会话默认 15 分钟后过期,请在有效期内完成上传 -2. **文件大小限制**: 受系统配置 `uploadSize` 限制 +2. **文件大小限制**: 受系统配置 `upload_size` 限制 3. **过期类型**: 支持 `day`、`hour`、`minute`、`forever`、`count` 4. **CORS**: 直传模式下,S3 需要配置正确的 CORS 策略 5. **重试机制**: 建议实现上传失败重试逻辑 diff --git a/docs/en/api/index.md b/docs/en/api/index.md index 1ec342f..012edf7 100644 --- a/docs/en/api/index.md +++ b/docs/en/api/index.md @@ -1,6 +1,6 @@ # FileCodeBox API Documentation -## API Version: 2.1.0 +## API Version: 2.5.6 ## Table of Contents - [Authentication](#authentication) @@ -30,7 +30,7 @@ curl -X POST "http://localhost:12345/admin/login" \ ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "token": "xxx.xxx.xxx", "token_type": "Bearer" @@ -74,7 +74,7 @@ curl -X POST "http://localhost:12345/share/text/" \ ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "code": "abc123" } @@ -125,7 +125,7 @@ curl -X POST "http://localhost:12345/share/file/" \ ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "code": "abc123", "name": "example.txt" @@ -157,7 +157,7 @@ curl -L "http://localhost:12345/share/select/?code=abc123" -o downloaded_file ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "code": "abc123", "name": "example.txt", @@ -184,7 +184,7 @@ Select file by share code. ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "code": "abc123", "name": "example.txt", @@ -232,15 +232,15 @@ Get system dashboard data. ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { - "totalFiles": 100, - "storageUsed": "1.5GB", - "sysUptime": "10 days", - "yesterdayCount": 50, - "yesterdaySize": "500MB", - "todayCount": 30, - "todaySize": "300MB" + "total_files": 100, + "storage_used": "1610612736", + "sys_uptime": 1725489600.0, + "yesterday_count": 50, + "yesterday_size": "524288000", + "today_count": 30, + "today_size": "314572800", } } ``` @@ -258,13 +258,15 @@ Get system file list. | page | integer | No | 1 | Current page | | size | integer | No | 10 | Page size | | keyword | string | No | "" | Search keyword | +| sort_by | string | No | "created_at" | Sort field(created_at/expired_at/name/size/used_count/code) | +| sort_order | string | No | "desc" | Sort direction(asc/desc) | **Response Example:** ```json { "code": 200, - "msg": "success", + "message": "success", "detail": { "page": 1, "size": 10, diff --git a/docs/en/api/presign-upload.md b/docs/en/api/presign-upload.md new file mode 100644 index 0000000..d4d0054 --- /dev/null +++ b/docs/en/api/presign-upload.md @@ -0,0 +1,389 @@ +# Presigned Upload API + +## Overview + +The presigned upload feature provides a unified file upload interface that automatically picks the optimal upload method based on the configured storage backend: + +- **S3 storage**: returns a presigned URL so the client uploads directly to S3 (reduces server bandwidth usage) +- **Other storage**: returns a proxy upload URL and the file is transferred through the server + +## Upload Flow + +### Flow Diagram + +``` +┌─────────┐ 1. Initialize upload ┌─────────┐ +│ Client │ ───────────────────────▶ │ Server │ +└─────────┘ └─────────┘ + │ │ + │◀──── upload_url + mode ──────────┤ + │ │ + │ ┌─────────────────────────────────────────┐ + │ │ if mode == "direct" (S3 storage) │ + │ │ 2a. PUT file to upload_url (S3) │ + │ │ 3a. POST /confirm to finish │ + │ │ │ + │ │ if mode == "proxy" (other storage) │ + │ │ 2b. PUT file to upload_url (server) │ + │ │ (share code returned automatically, │ + │ │ no confirmation needed) │ + │ └─────────────────────────────────────────┘ + │ + ▼ + Get share code +``` + +--- + +## API Endpoints + +### 1. Initialize Upload + +Initialize a presigned upload session and get the upload URL and mode. + +**Request** + +``` +POST /presign/upload/init +Content-Type: application/json +``` + +**Request Body** + +| Field | Type | Required | Default | Description | +| ------------ | ------- | -------- | ------- | --------------------------------------- | +| file_name | string | ✅ | - | File name (with extension) | +| file_size | integer | ✅ | - | File size in bytes | +| expire_value | integer | ❌ | 1 | Expiration time value | +| expire_style | string | ❌ | "day" | Expiration type: day/hour/minute/forever/count | + +**Request Example** + +```json +{ + "file_name": "document.pdf", + "file_size": 1048576, + "expire_value": 7, + "expire_style": "day" +} +``` + +**Response** + +```json +{ + "code": 200, + "detail": { + "upload_id": "a1b2c3d4e5f6...", + "upload_url": "https://bucket.s3.amazonaws.com/path?X-Amz-Signature=...", + "mode": "direct", + "expires_in": 900 + } +} +``` + +**Response Fields** + +| Field | Type | Description | +| ---------- | ------- | --------------------------------------------------------------- | +| upload_id | string | Upload session ID, required by all subsequent operations | +| upload_url | string | Target upload URL | +| mode | string | Upload mode: `direct` (direct to S3) or `proxy` (via server) | +| expires_in | integer | URL validity in seconds, default 900 seconds (15 minutes) | + +In `proxy` mode the response also includes `proxy_upload_url` (and `legacy_proxy_upload_url`); `upload_url` equals the proxy URL. + +**Error Responses** + +| Status | Description | +| ------ | -------------------------------------------------- | +| 400 | Invalid expiration type | +| 403 | File size exceeds limit / IP rate limit exceeded | + +--- + +### 2a. Direct Mode - Upload File to S3 + +When `mode == "direct"`, the client PUTs the file directly to the returned presigned URL. + +**Request** + +``` +PUT {upload_url} +Content-Type: application/octet-stream + +[binary file content] +``` + +**Notes** + +- Use the returned `upload_url` as-is, do not modify it +- `application/octet-stream` is the recommended Content-Type +- The request goes directly to S3, it does not pass through the server + +**JavaScript Example** + +```javascript +const response = await fetch(uploadUrl, { + method: 'PUT', + body: file, + headers: { + 'Content-Type': 'application/octet-stream', + }, +}) + +if (response.ok) { + // Upload succeeded, call the confirm endpoint +} +``` + +--- + +### 2b. Proxy Mode - Upload File to Server + +When `mode == "proxy"`, the client PUTs the file to the server proxy endpoint. + +**Request** + +``` +PUT /presign/upload/proxy/{upload_id} +Content-Type: multipart/form-data + +file: [file] +``` + +**Path Parameters** + +| Parameter | Description | +| --------- | ----------------------------------------------- | +| upload_id | Upload session ID returned by the init endpoint | + +**Response** + +```json +{ + "code": 200, + "detail": { + "code": "123456", + "name": "document.pdf" + } +} +``` + +**Note**: In proxy mode the share code is returned immediately after upload; no confirmation call is needed. + +**Error Responses** + +| Status | Description | +| ------ | ------------------------------------------------------------------ | +| 400 | File size mismatches declared size / session does not support proxy | +| 404 | Upload session does not exist or has expired | +| 500 | Failed to save file | + +--- + +### 3. Confirm Upload (direct mode only) + +In direct mode, call this endpoint after the S3 upload completes to obtain the share code. + +**Request** + +``` +POST /presign/upload/confirm/{upload_id} +Content-Type: application/json +``` + +**Path Parameters** + +| Parameter | Description | +| --------- | ----------------------------------------------- | +| upload_id | Upload session ID returned by the init endpoint | + +**Response** + +```json +{ + "code": 200, + "detail": { + "code": "123456", + "name": "document.pdf" + } +} +``` + +**Error Responses** + +| Status | Description | +| ------ | ------------------------------------------------------------------------------- | +| 400 | Session does not support direct confirmation | +| 404 | Upload session does not exist or has expired / file not uploaded or upload failed | + +--- + +### 4. Query Upload Status + +Query the current state of an upload session. + +**Request** + +``` +GET /presign/upload/status/{upload_id} +``` + +**Response** + +```json +{ + "code": 200, + "detail": { + "upload_id": "a1b2c3d4e5f6...", + "file_name": "document.pdf", + "file_size": 1048576, + "mode": "direct", + "created_at": "2024-01-01T12:00:00", + "expires_at": "2024-01-01T12:15:00", + "is_expired": false + } +} +``` + +**Error Responses** + +| Status | Description | +| ------ | ---------------------------------- | +| 404 | Upload session does not exist | + +--- + +### 5. Cancel Upload + +Cancel an upload session and clean up related resources. + +**Request** + +``` +DELETE /presign/upload/{upload_id} +``` + +**Response** + +```json +{ + "code": 200, + "detail": { + "message": "Upload session cancelled" + } +} +``` + +**Error Responses** + +| Status | Description | +| ------ | ---------------------------------- | +| 404 | Upload session does not exist | + +--- + +## Frontend Integration Example + +### Complete Upload Flow (JavaScript/TypeScript) + +```typescript +interface PresignInitResponse { + upload_id: string + upload_url: string + mode: 'direct' | 'proxy' + expires_in: number +} + +interface UploadResult { + code: string + name: string +} + +async function uploadFile( + file: File, + expireValue: number = 1, + expireStyle: string = 'day' +): Promise { + // 1. Initialize upload + const initResponse = await fetch('/presign/upload/init', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + file_name: file.name, + file_size: file.size, + expire_value: expireValue, + expire_style: expireStyle, + }), + }) + + const initData = await initResponse.json() + if (initData.code !== 200) { + throw new Error(initData.detail) + } + + const { upload_id, upload_url, mode } = initData.detail as PresignInitResponse + + // 2. Upload file based on mode + if (mode === 'direct') { + // Direct mode: upload to S3 + const uploadResponse = await fetch(upload_url, { + method: 'PUT', + body: file, + headers: { 'Content-Type': 'application/octet-stream' }, + }) + + if (!uploadResponse.ok) { + throw new Error('S3 upload failed') + } + + // 3. Confirm upload + const confirmResponse = await fetch( + `/presign/upload/confirm/${upload_id}`, + { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + } + ) + + const confirmData = await confirmResponse.json() + if (confirmData.code !== 200) { + throw new Error(confirmData.detail) + } + + return confirmData.detail + } else { + // Proxy mode: upload through the server + const formData = new FormData() + formData.append('file', file) + + const uploadResponse = await fetch(upload_url, { + method: 'PUT', + body: formData, + }) + + const uploadData = await uploadResponse.json() + if (uploadData.code !== 200) { + throw new Error(uploadData.detail) + } + + return uploadData.detail + } +} + +// Usage example +const file = document.querySelector('input[type="file"]').files[0] +const result = await uploadFile(file, 7, 'day') +console.log('Share code:', result.code) +``` + +--- + +## Notes + +1. **Session validity**: upload sessions expire after 15 minutes by default; complete the upload within the window +2. **File size limit**: bounded by the `upload_size` system setting +3. **Expiration types**: `day`, `hour`, `minute`, `forever`, `count` +4. **CORS**: in direct mode, S3 must have a proper CORS policy configured +5. **Retry**: implement upload retry logic on the client side for failed uploads