feat: add server file attachment pipeline
This commit is contained in:
73
scripts/verify-attachment-upload-download.mjs
Normal file
73
scripts/verify-attachment-upload-download.mjs
Normal file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env node
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
const baseUrl = process.env.BOSS_TEST_BASE_URL || "http://127.0.0.1:3000";
|
||||
const repoRoot = process.cwd();
|
||||
const readmePath = path.join(repoRoot, "README.md");
|
||||
const readmeBytes = await readFile(readmePath);
|
||||
|
||||
const loginResponse = await fetch(`${baseUrl}/api/auth/login`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
|
||||
if (!loginResponse.ok) {
|
||||
throw new Error(`LOGIN_FAILED:${loginResponse.status}`);
|
||||
}
|
||||
|
||||
const setCookie = loginResponse.headers.get("set-cookie") || "";
|
||||
const cookie = setCookie.split(";")[0];
|
||||
if (!cookie) {
|
||||
throw new Error("COOKIE_MISSING");
|
||||
}
|
||||
|
||||
const uploadForm = new FormData();
|
||||
uploadForm.append("file", new File([readmeBytes], "README.md", { type: "text/markdown" }));
|
||||
|
||||
const uploadResponse = await fetch(`${baseUrl}/api/v1/projects/boss-console/attachments`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
cookie,
|
||||
},
|
||||
body: uploadForm,
|
||||
});
|
||||
|
||||
if (!uploadResponse.ok) {
|
||||
throw new Error(`UPLOAD_FAILED:${uploadResponse.status}`);
|
||||
}
|
||||
|
||||
const uploadJson = await uploadResponse.json();
|
||||
if (!uploadJson.ok || !uploadJson.attachment?.attachmentId || !uploadJson.downloadUrl) {
|
||||
throw new Error("UPLOAD_RESPONSE_INVALID");
|
||||
}
|
||||
if (uploadJson.message?.kind !== "attachment") {
|
||||
throw new Error("ATTACHMENT_MESSAGE_KIND_INVALID");
|
||||
}
|
||||
if (!Array.isArray(uploadJson.message?.attachments) || uploadJson.message.attachments.length !== 1) {
|
||||
throw new Error("ATTACHMENT_PAYLOAD_INVALID");
|
||||
}
|
||||
|
||||
const downloadResponse = await fetch(`${baseUrl}${uploadJson.downloadUrl}`, {
|
||||
headers: {
|
||||
cookie,
|
||||
},
|
||||
});
|
||||
|
||||
if (!downloadResponse.ok) {
|
||||
throw new Error(`DOWNLOAD_FAILED:${downloadResponse.status}`);
|
||||
}
|
||||
|
||||
const downloadedBytes = Buffer.from(await downloadResponse.arrayBuffer());
|
||||
if (Buffer.compare(downloadedBytes, readmeBytes) !== 0) {
|
||||
throw new Error("DOWNLOADED_CONTENT_MISMATCH");
|
||||
}
|
||||
|
||||
if ((downloadResponse.headers.get("content-disposition") || "").indexOf("README.md") === -1) {
|
||||
throw new Error("DOWNLOAD_HEADERS_INVALID");
|
||||
}
|
||||
|
||||
console.log("OK");
|
||||
Reference in New Issue
Block a user