2025-10-24 18:05:51 +02:00
|
|
|
import { type NextRequest, NextResponse } from "next/server";
|
|
|
|
|
import { env } from "~/env";
|
|
|
|
|
import { TOKEN_CONFIG } from "~/server/auth/configs";
|
|
|
|
|
import { verifyToken } from "~/server/auth/jwt";
|
|
|
|
|
|
|
|
|
|
export const apisMiddleware = async (req: NextRequest) => {
|
|
|
|
|
const { pathname, searchParams } = req.nextUrl;
|
2025-11-12 17:28:59 +01:00
|
|
|
//console.log("APIs Middleware triggered for:", pathname);
|
2025-10-27 17:58:42 +01:00
|
|
|
|
|
|
|
|
// Only handle storage API routes
|
|
|
|
|
if (!pathname.startsWith("/storage-api/")) {
|
|
|
|
|
return null; // Pass to next middleware
|
2025-10-24 18:05:51 +02:00
|
|
|
}
|
2025-10-27 17:58:42 +01:00
|
|
|
|
|
|
|
|
const params = new URLSearchParams(searchParams);
|
|
|
|
|
const isImageRequest = params.get("image") === "true";
|
|
|
|
|
const isVideoRequest = params.get("video") === "true";
|
|
|
|
|
|
|
|
|
|
// Check if this is a Next.js Image Optimization request
|
|
|
|
|
const purpose = req.headers.get("purpose");
|
|
|
|
|
const isNextImageRequest =
|
|
|
|
|
purpose === "prefetch" || req.headers.get("x-vercel-id");
|
|
|
|
|
|
|
|
|
|
// For regular requests (not from Next Image Optimization), check auth
|
|
|
|
|
if (!isNextImageRequest && !isImageRequest && !isVideoRequest) {
|
|
|
|
|
const accessToken = req.cookies.get(
|
|
|
|
|
TOKEN_CONFIG.ACCESS_TOKEN_COOKIE_NAME,
|
|
|
|
|
)?.value;
|
|
|
|
|
|
|
|
|
|
if (!accessToken) {
|
|
|
|
|
console.log("No access token found");
|
|
|
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const payload = await verifyToken(accessToken);
|
|
|
|
|
if (!payload) {
|
|
|
|
|
console.log("Invalid token");
|
|
|
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
|
|
|
|
}
|
2025-10-24 18:05:51 +02:00
|
|
|
}
|
2025-10-27 17:58:42 +01:00
|
|
|
|
2025-10-24 18:05:51 +02:00
|
|
|
// Build the storage API URL correctly
|
|
|
|
|
const slug = pathname.replace("/storage-api/", "");
|
|
|
|
|
|
|
|
|
|
params.set("token", env.STORAGE_TOKEN);
|
|
|
|
|
|
|
|
|
|
const storageUrl = `${env.STORAGE_URL}/${slug}?${params.toString()}`;
|
|
|
|
|
|
2025-11-12 17:28:59 +01:00
|
|
|
//console.log("Proxying to:", storageUrl);
|
2025-10-27 17:58:42 +01:00
|
|
|
|
2025-10-24 18:05:51 +02:00
|
|
|
try {
|
|
|
|
|
// Forward the request to storage API
|
|
|
|
|
const headers = new Headers(req.headers);
|
|
|
|
|
headers.delete("host");
|
|
|
|
|
headers.delete("cookie");
|
|
|
|
|
|
|
|
|
|
const response = await fetch(storageUrl, {
|
|
|
|
|
method: req.method,
|
|
|
|
|
headers: headers,
|
|
|
|
|
body:
|
|
|
|
|
req.method !== "GET" && req.method !== "HEAD" ? req.body : undefined,
|
|
|
|
|
// @ts-expect-error - duplex needed for request streaming
|
|
|
|
|
duplex: "half",
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-27 17:58:42 +01:00
|
|
|
// Handle failed responses
|
2025-10-24 18:05:51 +02:00
|
|
|
if (!response.ok) {
|
2025-10-27 17:58:42 +01:00
|
|
|
console.error(`Storage API error: ${response.status} for ${storageUrl}`);
|
|
|
|
|
|
|
|
|
|
// Return fallback for image requests
|
|
|
|
|
if (isImageRequest || isNextImageRequest) {
|
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
|
url.pathname = "/fallback-image.png";
|
|
|
|
|
url.search = "";
|
|
|
|
|
return NextResponse.rewrite(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return fallback for video requests
|
|
|
|
|
if (isVideoRequest) {
|
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
|
url.pathname = "/fallback-video.png";
|
|
|
|
|
url.search = "";
|
|
|
|
|
return NextResponse.rewrite(url);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 18:05:51 +02:00
|
|
|
return new NextResponse("Storage API error", { status: response.status });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get response data
|
|
|
|
|
const data = await response.arrayBuffer();
|
|
|
|
|
|
2025-10-27 17:58:42 +01:00
|
|
|
// Check if we got valid data
|
|
|
|
|
if (!data || data.byteLength === 0) {
|
|
|
|
|
console.error("Received empty response from storage API");
|
|
|
|
|
|
|
|
|
|
if (isImageRequest || isNextImageRequest) {
|
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
|
url.pathname = "/fallback-image.png";
|
|
|
|
|
url.search = "";
|
|
|
|
|
return NextResponse.rewrite(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isVideoRequest) {
|
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
|
url.pathname = "/fallback-video.png";
|
|
|
|
|
url.search = "";
|
|
|
|
|
return NextResponse.rewrite(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new NextResponse("Empty response", { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 18:05:51 +02:00
|
|
|
// Create response with proper headers
|
|
|
|
|
const proxyResponse = new NextResponse(data, {
|
|
|
|
|
status: response.status,
|
|
|
|
|
statusText: response.statusText,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Copy relevant headers
|
|
|
|
|
const contentType = response.headers.get("Content-Type");
|
|
|
|
|
const contentDisposition = response.headers.get("Content-Disposition");
|
|
|
|
|
const contentLength = response.headers.get("Content-Length");
|
|
|
|
|
|
2025-10-27 17:58:42 +01:00
|
|
|
if (contentType) {
|
|
|
|
|
proxyResponse.headers.set("Content-Type", contentType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (contentDisposition) {
|
2025-10-24 18:05:51 +02:00
|
|
|
proxyResponse.headers.set("Content-Disposition", contentDisposition);
|
2025-10-27 17:58:42 +01:00
|
|
|
}
|
|
|
|
|
if (contentLength) {
|
2025-10-24 18:05:51 +02:00
|
|
|
proxyResponse.headers.set("Content-Length", contentLength);
|
2025-10-27 17:58:42 +01:00
|
|
|
}
|
2025-10-24 18:05:51 +02:00
|
|
|
|
2025-10-27 17:58:42 +01:00
|
|
|
// Set CORS and caching headers
|
|
|
|
|
proxyResponse.headers.set("Access-Control-Allow-Origin", "*");
|
|
|
|
|
proxyResponse.headers.set(
|
|
|
|
|
"Cache-Control",
|
|
|
|
|
"public, max-age=31536000, immutable",
|
|
|
|
|
);
|
|
|
|
|
proxyResponse.headers.delete("X-Frame-Options");
|
2025-10-24 18:05:51 +02:00
|
|
|
proxyResponse.headers.set(
|
|
|
|
|
"Content-Security-Policy",
|
|
|
|
|
"frame-ancestors 'self'",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return proxyResponse;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Storage proxy error:", error);
|
2025-10-27 17:58:42 +01:00
|
|
|
|
|
|
|
|
// Return fallback on error
|
|
|
|
|
if (isImageRequest || isNextImageRequest) {
|
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
|
url.pathname = "/fallback-image.png";
|
|
|
|
|
url.search = "";
|
|
|
|
|
return NextResponse.rewrite(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isVideoRequest) {
|
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
|
url.pathname = "/fallback-video.png";
|
|
|
|
|
url.search = "";
|
|
|
|
|
return NextResponse.rewrite(url);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 18:05:51 +02:00
|
|
|
return new NextResponse("Failed to proxy request", { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
};
|