Make file uploads work on any device or network
Update the file upload URL generation to use same-origin paths, ensuring compatibility across different network environments and devices by allowing the browser to automatically resolve the correct origin. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 77cfe984-2c65-4152-bb7a-0df28274fe66 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 5c4f1b40-c7ae-41a2-b281-0b1785f97ceb Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/c3c252e4-c83d-40ca-9fff-99a3ea60701e/77cfe984-2c65-4152-bb7a-0df28274fe66/IO8TMSC Replit-Helium-Checkpoint-Created: true
This commit is contained in:
@@ -160,13 +160,21 @@ class LocalDriver implements StorageDriver {
|
||||
).toString("base64url");
|
||||
const sig = createHmac("sha256", LOCAL_HMAC_SECRET).update(payload).digest("base64url");
|
||||
const token = `${payload}.${sig}`;
|
||||
const base = (process.env.PUBLIC_BASE_URL ?? `http://localhost:${process.env.PORT ?? "8080"}`).replace(/\/+$/, "");
|
||||
return `${base}/api/storage/_local/upload?token=${token}`;
|
||||
// Return a SAME-ORIGIN path (not an absolute URL). The browser will
|
||||
// resolve it against `window.location.origin`, so the upload always
|
||||
// targets the public host the SPA is being served from — works for
|
||||
// localhost, Tailscale (tx.local), LAN IPs, and remote phones alike
|
||||
// without depending on PUBLIC_BASE_URL being set correctly.
|
||||
return `/api/storage/_local/upload?token=${token}`;
|
||||
}
|
||||
|
||||
parseUploadUrl(rawUrl: string): { bucketName: string; objectName: string } | null {
|
||||
try {
|
||||
const u = new URL(rawUrl);
|
||||
// Accept BOTH legacy absolute URLs (older signed URLs still in the
|
||||
// wild) and the new same-origin paths. URL() needs a base for the
|
||||
// relative form — the base is irrelevant since we only read
|
||||
// pathname/searchParams.
|
||||
const u = new URL(rawUrl, "http://placeholder.local");
|
||||
if (!u.pathname.endsWith("/api/storage/_local/upload")) return null;
|
||||
const token = u.searchParams.get("token");
|
||||
if (!token) return null;
|
||||
|
||||
Reference in New Issue
Block a user