fix photo upload: add synchronous /jobs/presign and /jobs/minio-upload endpoints to workers

This commit is contained in:
justin 2026-05-30 19:13:51 -05:00
parent ca7af40ceb
commit e40f359693
2 changed files with 29 additions and 17 deletions

View file

@ -27,7 +27,6 @@ async function presign(key: string, method: "GET" | "PUT", expires = 3600): Prom
if (!r.ok) return null;
const data = (await r.json()) as { url?: string };
let url = data.url || null;
// Rewrite internal minio:9000 to public HTTPS endpoint
if (url) {
url = url.replace(/^http:\/\/minio:9000\//, "https://minio.performancewest.net/");
}
@ -37,12 +36,9 @@ async function presign(key: string, method: "GET" | "PUT", expires = 3600): Prom
}
}
/** Upload a buffer to MinIO using a presigned PUT URL. */
/** Upload a buffer to MinIO via the workers. */
async function uploadToMinio(key: string, buffer: Buffer, contentType: string): Promise<boolean> {
const putUrl = await presign(key, "PUT", 300);
if (!putUrl) return false;
try {
// The presigned PUT URL may have internal hostname — use the workers endpoint directly
const r = await fetch(`${WORKER_URL}/jobs/minio-upload`, {
method: "POST",
headers: { "Content-Type": "application/json" },
@ -53,19 +49,11 @@ async function uploadToMinio(key: string, buffer: Buffer, contentType: string):
bucket: MINIO_BUCKET,
}),
});
return r.ok;
if (!r.ok) return false;
const data = (await r.json()) as { success?: boolean };
return !!data.success;
} catch {
// Fallback: try presigned PUT
try {
const resp = await fetch(putUrl.replace("https://minio.performancewest.net/", "http://minio:9000/"), {
method: "PUT",
headers: { "Content-Type": contentType },
body: buffer,
});
return resp.ok;
} catch {
return false;
}
return false;
}
}