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;
}
}

View file

@ -1910,6 +1910,30 @@ class JobHandler(BaseHTTPRequestHandler):
self._handle_rmd_preview()
return
if self.path == "/jobs/presign":
# Synchronous presign — used by API routes
content_length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(content_length)
try:
payload = json.loads(body)
result = handle_presign(payload)
self._respond(200, result)
except Exception as exc:
self._respond(500, {"error": str(exc)})
return
if self.path == "/jobs/minio-upload":
# Synchronous MinIO upload — used by id-upload
content_length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(content_length)
try:
payload = json.loads(body)
result = handle_minio_upload(payload)
self._respond(200, result)
except Exception as exc:
self._respond(500, {"error": str(exc)})
return
if self.path != "/jobs":
self._respond(404, {"error": "Not found"})
return