ext/web W5: multipart upload receive and download
Add app.upload/download routes over # storage, offline media validate/save helpers, form type=file + enctype, and curl live gold coverage. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
034f52b7e7
commit
a900fd550f
18 files changed
+1214
-15
No files matched your search
@@ -0,0 +1 @@
|
||||
not-allowed
|
||||
@@ -0,0 +1 @@
|
||||
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
@@ -0,0 +1 @@
|
||||
hello-upload
|
||||
@@ -0,0 +1,2 @@
|
||||
‰PNG
|
||||
|
||||
|
After Width: | Height: | Size: 8 B |
@@ -0,0 +1,31 @@
|
||||
---
|
||||
title: web upload live server (W5)
|
||||
description: Listen with upload + download routes for curl gold.
|
||||
import web:ext/web/web.mq.md
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*p = > plugin.native_path name="web"*
|
||||
1. `p`
|
||||
> plugin.load path=`p`
|
||||
2. *
|
||||
> sys.exit code=1
|
||||
|
||||
*blob = > web.storage url="file:tests/ext/web-fixtures/data/upload-live-blobs"*
|
||||
|
||||
`types` =
|
||||
|
||||
| 类型 | 扩展名 |
|
||||
|------|--------|
|
||||
| text/plain | txt |
|
||||
| image/png | png |
|
||||
|
||||
*pg = > web.page title="upload-live"*
|
||||
*app = > web.app page=`pg` port=18101*
|
||||
*app = > `app`.configure body_limit=1048576*
|
||||
*app = > `app`.upload path="/_upload" field="file" storage=`blob` prefix="live/" max_bytes=1024 types=`types`*
|
||||
*app = > `app`.download path="/_media/{*key}" storage=`blob` disposition="attachment"*
|
||||
> `app`.listen
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
title: web upload smoke (W5)
|
||||
description: Offline validate + save into file: storage.
|
||||
import web:ext/web/web.mq.md
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*p = > plugin.native_path name="web"*
|
||||
1. `p`
|
||||
> plugin.load path=`p`
|
||||
2. *
|
||||
> sys.exit code=1
|
||||
|
||||
`types` =
|
||||
|
||||
| 类型 | 扩展名 |
|
||||
|------|--------|
|
||||
| image/png | png |
|
||||
| text/plain | txt |
|
||||
|
||||
*m = > web.media*
|
||||
*ok = > `m`.validate filename="a.png" content_type="image/png" size=100 max_bytes=1000 types=`types`*
|
||||
*ook = ok[^ok]*
|
||||
1. `ook`
|
||||
> print text=validate-ok
|
||||
2. *
|
||||
> print text=validate-fail
|
||||
|
||||
*bad = > `m`.validate filename="a.exe" content_type="application/octet-stream" size=100 max_bytes=1000 types=`types`*
|
||||
*bok = bad[^ok]*
|
||||
1. `bok`
|
||||
> print text=reject-type-fail
|
||||
2. *
|
||||
> print text=reject-type-ok
|
||||
|
||||
*big = > `m`.validate filename="a.png" content_type="image/png" size=9000 max_bytes=1000 types=`types`*
|
||||
*gok = big[^ok]*
|
||||
1. `gok`
|
||||
> print text=reject-size-fail
|
||||
2. *
|
||||
> print text=reject-size-ok
|
||||
|
||||
*blob = > web.storage url="file:tests/ext/web-fixtures/data/upload-blobs"*
|
||||
*m = > web.media storage=`blob`*
|
||||
*saved = > `m`.save path="tests/ext/web-fixtures/upload/sample.txt" content_type="text/plain" prefix="smoke/"*
|
||||
*sok = saved[^ok]*
|
||||
*skey = saved[^key]*
|
||||
1. `sok`
|
||||
> print text=save-ok
|
||||
2. *
|
||||
> print text=save-fail
|
||||
|
||||
*got = > `blob`.get key=`skey`*
|
||||
*body = got[^body]*
|
||||
1. `body` == "hello-upload"
|
||||
> print text=save-roundtrip-ok
|
||||
2. *
|
||||
> print text=save-roundtrip-fail
|
||||
|
||||
<!-- form file field renders with multipart enctype -->
|
||||
|
||||
`fields` =
|
||||
|
||||
| name | label | type |
|
||||
|------|-------|------|
|
||||
| title | Title | text |
|
||||
| file | File | file |
|
||||
|
||||
*frm = > web.form table="x" fields=`fields`*
|
||||
*html = > `frm`.render id="up"*
|
||||
1. `html`
|
||||
> print text=form-file-ok
|
||||
2. *
|
||||
> print text=form-file-fail
|
||||
+172
@@ -1765,6 +1765,178 @@ s3-open-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_upload_smoke() {
|
||||
let status = Command::new("cargo")
|
||||
.args(["build", "-p", "marqdo_plugin_web"])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.status()
|
||||
.expect("build web plugin");
|
||||
assert!(status.success(), "failed to build marqdo_plugin_web");
|
||||
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/ext/web-fixtures/data/upload-blobs");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
|
||||
assert_out(
|
||||
"tests/ext/web-upload-smoke.mq.md",
|
||||
"validate-ok
|
||||
reject-type-ok
|
||||
reject-size-ok
|
||||
save-ok
|
||||
save-roundtrip-ok
|
||||
form-file-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_upload_live() {
|
||||
let status = Command::new("cargo")
|
||||
.args(["build", "-p", "marqdo_plugin_web"])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.status()
|
||||
.expect("build web plugin");
|
||||
assert!(status.success(), "failed to build marqdo_plugin_web");
|
||||
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/ext/web-fixtures/data/upload-live-blobs");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
|
||||
let script = "tests/ext/web-upload-live-server.mq.md";
|
||||
let bin = env!("CARGO_BIN_EXE_marqdo");
|
||||
let mut child = Command::new(bin)
|
||||
.args(["run", script])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("spawn upload live server");
|
||||
|
||||
let base = "http://127.0.0.1:18101";
|
||||
let ready = std::time::Instant::now();
|
||||
loop {
|
||||
if ready.elapsed().as_secs() > 10 {
|
||||
let _ = child.kill();
|
||||
panic!("upload live server did not start in time");
|
||||
}
|
||||
let out = Command::new("curl")
|
||||
.args([
|
||||
"-s",
|
||||
"-o",
|
||||
"/dev/null",
|
||||
"-w",
|
||||
"%{http_code}",
|
||||
&format!("{base}/"),
|
||||
])
|
||||
.output()
|
||||
.expect("curl probe");
|
||||
if String::from_utf8_lossy(&out.stdout) == "200" {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(300));
|
||||
}
|
||||
|
||||
let fixture = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/ext/web-fixtures/upload/sample.txt");
|
||||
|
||||
// Happy path multipart upload.
|
||||
let out = Command::new("curl")
|
||||
.args([
|
||||
"-s",
|
||||
"-w",
|
||||
"\n%{http_code}",
|
||||
"-F",
|
||||
&format!("file=@{};type=text/plain", fixture.display()),
|
||||
&format!("{base}/_upload"),
|
||||
])
|
||||
.output()
|
||||
.expect("curl upload");
|
||||
let body = String::from_utf8_lossy(&out.stdout);
|
||||
let (json_part, code) = body.rsplit_once('\n').unwrap_or((&body, ""));
|
||||
assert_eq!(code.trim(), "200", "upload body={body}");
|
||||
assert!(
|
||||
json_part.contains("\"ok\":true") && json_part.contains("\"key\""),
|
||||
"upload json={json_part}"
|
||||
);
|
||||
let key = {
|
||||
let v: serde_json::Value =
|
||||
serde_json::from_str(json_part).expect("upload json parse");
|
||||
v.get("key")
|
||||
.and_then(|k| k.as_str())
|
||||
.expect("key")
|
||||
.to_string()
|
||||
};
|
||||
|
||||
// Download with Content-Disposition.
|
||||
let out = Command::new("curl")
|
||||
.args([
|
||||
"-s",
|
||||
"-D",
|
||||
"-",
|
||||
"-o",
|
||||
"/tmp/marqdo-upload-dl.txt",
|
||||
&format!("{base}/_media/{key}"),
|
||||
])
|
||||
.output()
|
||||
.expect("curl download");
|
||||
let headers = String::from_utf8_lossy(&out.stdout).to_ascii_lowercase();
|
||||
assert!(
|
||||
headers.contains("content-disposition: attachment"),
|
||||
"download headers={headers}"
|
||||
);
|
||||
let dl = std::fs::read_to_string("/tmp/marqdo-upload-dl.txt").expect("dl body");
|
||||
assert_eq!(dl, "hello-upload");
|
||||
|
||||
// Reject wrong type.
|
||||
let bad = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/ext/web-fixtures/upload/bad.bin");
|
||||
let _ = std::fs::write(&bad, b"not-allowed");
|
||||
let out = Command::new("curl")
|
||||
.args([
|
||||
"-s",
|
||||
"-o",
|
||||
"/dev/null",
|
||||
"-w",
|
||||
"%{http_code}",
|
||||
"-F",
|
||||
&format!("file=@{};type=application/octet-stream", bad.display()),
|
||||
&format!("{base}/_upload"),
|
||||
])
|
||||
.output()
|
||||
.expect("curl bad type");
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&out.stdout).trim(),
|
||||
"400",
|
||||
"bad type should 400"
|
||||
);
|
||||
|
||||
// Oversize (> max_bytes=1024).
|
||||
let big = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/ext/web-fixtures/upload/big.txt");
|
||||
let _ = std::fs::write(&big, vec![b'x'; 2048]);
|
||||
let out = Command::new("curl")
|
||||
.args([
|
||||
"-s",
|
||||
"-o",
|
||||
"/dev/null",
|
||||
"-w",
|
||||
"%{http_code}",
|
||||
"-F",
|
||||
&format!("file=@{};type=text/plain", big.display()),
|
||||
&format!("{base}/_upload"),
|
||||
])
|
||||
.output()
|
||||
.expect("curl oversize");
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&out.stdout).trim(),
|
||||
"400",
|
||||
"oversize should 400"
|
||||
);
|
||||
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_db_w2_smoke() {
|
||||
// W2 data layer: transactions, connection pooling, pagination, query
|
||||
|
||||
Reference in new issue
Block a user