diff --git a/Cargo.lock b/Cargo.lock index b256364..6ef0986 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,6 +191,7 @@ dependencies = [ "matchit", "memchr", "mime", + "multer", "percent-encoding", "pin-project-lite", "serde_core", @@ -537,6 +538,15 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34" +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + [[package]] name = "errno" version = "0.3.14" @@ -1132,6 +1142,23 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "multer" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83e87776546dc87511aa5ee218730c92b666d7264ab6ed41f9d215af9cd5224b" +dependencies = [ + "bytes", + "encoding_rs", + "futures-util", + "http", + "httparse", + "memchr", + "mime", + "spin", + "version_check", +] + [[package]] name = "normalize-line-endings" version = "0.3.0" @@ -1777,6 +1804,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "spin" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" + [[package]] name = "stringprep" version = "0.1.5" diff --git a/doc/design/web-net-capabilities.md b/doc/design/web-net-capabilities.md index c02af19..c03b8b6 100644 --- a/doc/design/web-net-capabilities.md +++ b/doc/design/web-net-capabilities.md @@ -117,7 +117,7 @@ | A2 | **中间件管道**(`app.configure` / `应用.装配`:CORS、安全头、压缩、请求体限制统一挂载) | ✅ **已实现(W1)**:`middleware.rs` 装配 GFM 表并 `app.layer` 挂到 axum | `plugins/web` | **P0 ✅** | | A3 | **CORS 支持**(`Access-Control-Allow-Origin` 可配) | ✅ **已实现(W1)**:`跨域` 表 `|允许来源|方法|头|暴露头|凭证|` | `plugins/web` | P1 ✅ | | A4 | **安全响应头**(CSP / X-Frame-Options / X-Content-Type-Options / HSTS / Referrer-Policy / 去 `X-Powered-By`) | ✅ **已实现(W1)**:`安全` 表 `|头|值|` | `plugins/web` | P1 ✅ | -| A5 | **文件上传接收**(multipart extractor → 校验类型/大小 → 落盘)与**下载**(`Content-Disposition`) | 头像/附件/图片上传是博客/CMS 刚需 | `plugins/web` | P1 | +| A5 | **文件上传接收**(multipart extractor → 校验类型/大小 → 落盘)与**下载**(`Content-Disposition`) | ✅ **已实现(W5)**:`app.upload` / `app.download`;`file:` storage;离线 + curl live 金样 | `plugins/web` | P1 ✅ | | A6 | **HTTPS / TLS**(rustls 终止或反代提示) | 生产部署安全 | `plugins/web`(或文档指引反代) | P2 | | A7 | **gzip / br 压缩** + **ETag / Cache-Control 缓存头** | gzip ✅ **已实现(W1,`压缩=真`)**;ETag/Cache-Control 待补 | `plugins/web`(tower-http 现成) | P2 | | A8 | **访问日志**(请求方法/路径/状态/耗时) | 排障、审计 | `plugins/web`(tower-http TraceLayer) | P2 | @@ -179,7 +179,7 @@ | **W2** | 数据层深化 | 连接池/busy_timeout/WAL + 事务 API + 结果集裸查询 + 分页(offset) + OR/IN/BETWEEN + 聚合 count | B1 B2 B3 B4 B5 B8 ✅ **已完成** | | **W3** | 安全硬化 | 密码哈希 + CSRF + 会话持久化 + CSPRNG session id + cookie 增强 + 登录限速/锁定 | C1–C7 | | **W4** | 内容站点标配 | SEO 元数据 + sitemap/robots + RSS + Markdown 渲染 + 分页导航 UI + 标签/分类 | D1–D6 | -| **W5** | 上传与媒体 | multipart 文件上传接收 + 落盘 + 下载 + 图片库 | A5 D9 | +| **W5** | 上传与媒体 | multipart 文件上传接收 + 落盘 + 下载 + 图片库 | A5 D9 ✅ **A5 done**(D9 相册 UI 后续) | | **W6** | 进阶 | 全文搜索(FTS5) + 迁移机制 + 评论系统 + 草稿/发布 + WS 广播 | B6 B9 D7 D8 + WS 增强 | ### 4.2 边界判定要点(严格遵守既有约束) diff --git a/doc/roadmap/ext-web.md b/doc/roadmap/ext-web.md index e1e38ad..7d48b8e 100644 --- a/doc/roadmap/ext-web.md +++ b/doc/roadmap/ext-web.md @@ -2,7 +2,7 @@ | | | |---|---| -| 状态 | **W0–W4 + 安全硬化 + 内容标配 W4c 已落地** | +| 状态 | **W0–W5 + 安全硬化 + 内容标配 W4c 已落地** | | 日期 | 2026-08-28 | | **锁定设计** | [design/ext-web.md](../design/ext-web.md) | | 相关 | [ext-cli.md](../design/ext-cli.md) · [ext-abi.md](../design/ext-abi.md) · [ext-llm.md](../design/ext-llm.md) | @@ -30,8 +30,9 @@ | **W3.5** | 人写面首版:`web.assemble` + `` `表`.`字段` `` + `examples/man-write-site` | **done**(编排袋仍可再瘦) | | **W4** | Postgres;Redis / S3 驱动(见 [ext-web-drivers.md](../design/ext-web-drivers.md)) | **done** | | **W4c** | 内容标配:SEO / Markdown / RSS / 分页 UI(见 [web-net-capabilities.md](../design/web-net-capabilities.md)) | **done** | +| **W5** | 上传与媒体:multipart 接收 + 落盘 + 下载(见 [web-net-capabilities.md](../design/web-net-capabilities.md) A5) | **done** | -验收金样:`tests/ext/web-smoke.mq.md`、`tests/ext/web-security-smoke.mq.md`、`tests/ext/web-drivers-smoke.mq.md`、`ext_cli_add_web`。 +验收金样:`tests/ext/web-smoke.mq.md`、`tests/ext/web-security-smoke.mq.md`、`tests/ext/web-drivers-smoke.mq.md`、`tests/ext/web-upload-smoke.mq.md`、`ext_cli_add_web`。 --- diff --git a/ext/web/web.mq.md b/ext/web/web.mq.md index d738155..2ea5d94 100644 --- a/ext/web/web.mq.md +++ b/ext/web/web.mq.md @@ -417,7 +417,7 @@ Tables stay as data; `configure` assembles them. 配置即数据、装配即函 ## listen -Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes), `/_form/{id}` (from mounts + page embeds), optional `/static` (or custom mount), and optional `/admin`. +Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes), `/_form/{id}` (from mounts + page embeds), optional `/static` (or custom mount), optional `/admin`, and any `upload` / `download` routes. **> web_listen app=`self`** @@ -450,6 +450,27 @@ Register an RSS 2.0 feed at `path` (e.g. `/feed.xml`) backed by a DB table. **> web_app_route_rss app=`self` path=`path` table=`table` limit=`limit` order=`order` title=`title` link=`link` description=`description`** +## upload + + `path`=/_upload + + `field`=file + + `storage` + + `prefix`=uploads/ + + `max_bytes`=5242880 + + `types`=None + +Mount `POST path` for multipart file upload. `storage` is a `# storage` handle or `file:…` / `s3://…` URL. Optional `types` is a `|type|ext|` / `|类型|扩展名|` allowlist (or a MIME CSV string). Success returns JSON `{ok,key,size,content_type}`. + +**> web_app_upload app=`self` path=`path` field=`field` storage=`storage` prefix=`prefix` max_bytes=`max_bytes` types=`types`** + +## download + + `path`=/_media/{*key} + + `storage` + + `disposition`=attachment + +Mount `GET path` to stream an object. Path must capture `key` (use `{*key}` for nested keys). `disposition` is `attachment` or `inline`. + +**> web_app_download app=`self` path=`path` storage=`storage` disposition=`disposition`** + # auth + `users` + `session_ttl`=3600 @@ -569,6 +590,37 @@ Provide either `body` (text) or `path` (local file to upload). *url = self[^url]* **> web_storage_list url=`url` prefix=`prefix`** +# media + + `storage`=None + +Offline helpers for upload validation and saving into `# storage` (also used by HTTP `app.upload`). + +> ensure_plugin +**> web_media_new storage=`storage`** + +## validate + + `filename` + + `content_type`="application/octet-stream" + + `size` + + `max_bytes`=5242880 + + `types`=None + +**> web_upload_validate filename=`filename` content_type=`content_type` size=`size` max_bytes=`max_bytes` types=`types`** + +## save + + `path` + + `key`=None + + `content_type`="application/octet-stream" + + `prefix`=uploads/ + + `storage`=None + +*st = storage* +1. `st` == None + *st = self[^storage]* +2. * + +**> web_upload_save storage=`st` path=`path` key=`key` content_type=`content_type` prefix=`prefix`** + # ws + `timeout_sec`=30 diff --git a/ext/web/网页.mq.md b/ext/web/网页.mq.md index 12c97e2..571cd0c 100644 --- a/ext/web/网页.mq.md +++ b/ext/web/网页.mq.md @@ -399,7 +399,7 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c ## 监听 -提供 `/`、路由页、首页 `/_part/{id}`、路由 `{路径}/_part/{id}`、`/_form/{id}`、可选静态目录,以及可选 `/admin`。 +提供 `/`、路由页、首页 `/_part/{id}`、路由 `{路径}/_part/{id}`、`/_form/{id}`、可选静态目录、可选 `/admin`,以及已挂载的 `上传` / `下载` 路由。 **> web_listen app=`self`** @@ -432,6 +432,27 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c **> web_app_route_rss app=`self` path=`路径` table=`表` limit=`上限` order=`排序` title=`标题` link=`链接` description=`描述`** +## 上传 + + `路径`=/_upload + + `字段`=file + + `存储` + + `前缀`=uploads/ + + `最大字节`=5242880 + + `类型`=None + +挂载 `POST 路径` 接收 multipart 文件。`存储` 为 `# 存储` 句柄或 `file:…` / `s3://…` URL。可选 `类型` 为 `|类型|扩展名|` 允许表(或 MIME CSV)。成功返回 JSON `{ok,key,size,content_type}`。 + +**> web_app_upload app=`self` path=`路径` field=`字段` storage=`存储` prefix=`前缀` max_bytes=`最大字节` types=`类型`** + +## 下载 + + `路径`=/_media/{*key} + + `存储` + + `处置`=attachment + +挂载 `GET 路径` 流式返回对象。路径须捕获 `key`(嵌套键用 `{*key}`)。`处置` 为 `attachment` 或 `inline`。 + +**> web_app_download app=`self` path=`路径` storage=`存储` disposition=`处置`** + # 鉴权 + `用户表` + `会话时长`=3600 @@ -551,6 +572,37 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c *url = self[^url]* **> web_storage_list url=`url` prefix=`前缀`** +# 媒体 + + `存储`=None + +上传校验与落盘辅助(HTTP `应用.上传` 亦用同一套规则)。 + +> 确保插件 +**> web_media_new storage=`存储`** + +## 校验 + + `文件名` + + `类型`="application/octet-stream" + + `大小` + + `最大字节`=5242880 + + `允许`=None + +**> web_upload_validate filename=`文件名` content_type=`类型` size=`大小` max_bytes=`最大字节` types=`允许`** + +## 保存 + + `路径` + + `键`=None + + `类型`="application/octet-stream" + + `前缀`=uploads/ + + `存储`=None + +*st = 存储* +1. `st` == None + *st = self[^storage]* +2. * + +**> web_upload_save storage=`st` path=`路径` key=`键` content_type=`类型` prefix=`前缀`** + # 实时 + `超时秒`=30 diff --git a/plugins/web/Cargo.toml b/plugins/web/Cargo.toml index 893b042..f3c4b84 100644 --- a/plugins/web/Cargo.toml +++ b/plugins/web/Cargo.toml @@ -14,7 +14,7 @@ crate-type = ["cdylib"] serde = { version = "1", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "sync", "time", "signal"] } -axum = { version = "0.8", features = ["ws", "json", "query"] } +axum = { version = "0.8", features = ["ws", "json", "query", "multipart"] } tower-http = { version = "0.6", features = ["fs", "cors", "compression-gzip", "limit", "set-header", "trace", "propagate-header"] } rusqlite = { version = "0.32", features = ["bundled"] } tokio-tungstenite = "0.24" diff --git a/plugins/web/src/form.rs b/plugins/web/src/form.rs index b94adca..7f896a5 100644 --- a/plugins/web/src/form.rs +++ b/plugins/web/src/form.rs @@ -488,8 +488,17 @@ pub fn render_body( )); } body.push_str(&format!( - "