109 lines
2.7 KiB
TypeScript
109 lines
2.7 KiB
TypeScript
import "dotenv/config";
|
|
|
|
import {
|
|
GetBucketCorsCommand,
|
|
PutBucketCorsCommand,
|
|
type CORSRule,
|
|
} from "@aws-sdk/client-s3";
|
|
|
|
import {
|
|
createS3Client,
|
|
resolveS3UploadTarget,
|
|
} from "@/lib/storage/service";
|
|
|
|
const DEFAULT_ORIGINS = [
|
|
"https://ai.yydd.ai",
|
|
"https://yydd.ai",
|
|
"https://bbyy.ai",
|
|
"https://goyingup.chat",
|
|
"http://localhost:3000",
|
|
"http://localhost:3001",
|
|
];
|
|
|
|
function readOrigins() {
|
|
const raw = process.env.DIRECT_UPLOAD_CORS_ORIGINS || "";
|
|
const origins = raw
|
|
.split(",")
|
|
.map((origin) => origin.trim())
|
|
.filter(Boolean);
|
|
|
|
return origins.length > 0 ? origins : DEFAULT_ORIGINS;
|
|
}
|
|
|
|
function normalizeRule(rule: CORSRule): CORSRule {
|
|
return {
|
|
AllowedHeaders: Array.from(new Set(rule.AllowedHeaders || [])),
|
|
AllowedMethods: Array.from(new Set(rule.AllowedMethods || [])),
|
|
AllowedOrigins: Array.from(new Set(rule.AllowedOrigins || [])),
|
|
ExposeHeaders: Array.from(new Set(rule.ExposeHeaders || [])),
|
|
MaxAgeSeconds: rule.MaxAgeSeconds,
|
|
};
|
|
}
|
|
|
|
function isDirectUploadRule(rule: CORSRule, origins: string[]) {
|
|
const methods = new Set(rule.AllowedMethods || []);
|
|
const allowedOrigins = new Set(rule.AllowedOrigins || []);
|
|
return (
|
|
methods.has("PUT") &&
|
|
methods.has("POST") &&
|
|
origins.some((origin) => allowedOrigins.has(origin))
|
|
);
|
|
}
|
|
|
|
async function main() {
|
|
const target = await resolveS3UploadTarget();
|
|
if (!target) {
|
|
console.log("[direct-upload-cors] skip: no S3-compatible upload target");
|
|
return;
|
|
}
|
|
|
|
const origins = readOrigins();
|
|
const client = createS3Client(target.config);
|
|
const bucket = target.config.bucket;
|
|
const directRule: CORSRule = {
|
|
AllowedOrigins: origins,
|
|
AllowedMethods: ["GET", "PUT", "POST", "HEAD"],
|
|
AllowedHeaders: ["*"],
|
|
ExposeHeaders: ["ETag", "x-amz-request-id", "x-amz-id-2"],
|
|
MaxAgeSeconds: 3600,
|
|
};
|
|
|
|
let existingRules: CORSRule[] = [];
|
|
try {
|
|
const current = await client.send(new GetBucketCorsCommand({ Bucket: bucket }));
|
|
existingRules = current.CORSRules || [];
|
|
} catch (error) {
|
|
console.warn(
|
|
"[direct-upload-cors] existing CORS not found or unreadable, replacing with direct upload rule:",
|
|
error instanceof Error ? error.message : String(error)
|
|
);
|
|
}
|
|
|
|
const mergedRules = [
|
|
directRule,
|
|
...existingRules.filter((rule) => !isDirectUploadRule(rule, origins)),
|
|
]
|
|
.slice(0, 100)
|
|
.map(normalizeRule);
|
|
|
|
await client.send(
|
|
new PutBucketCorsCommand({
|
|
Bucket: bucket,
|
|
CORSConfiguration: {
|
|
CORSRules: mergedRules,
|
|
},
|
|
})
|
|
);
|
|
|
|
console.log("[direct-upload-cors] configured", {
|
|
bucket,
|
|
origins,
|
|
ruleCount: mergedRules.length,
|
|
});
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error("[direct-upload-cors] failed:", error);
|
|
process.exitCode = 1;
|
|
});
|