|
| 1 | +import { detectContentType } from "./detectContentType"; |
| 2 | + |
| 3 | +export const dynamic = "force-dynamic"; |
| 4 | +export const runtime = "nodejs"; |
| 5 | + |
| 6 | +// The main handler with integrated error handling |
| 7 | +export async function GET(request: Request) { |
| 8 | + // Get the URL parameter from the request |
| 9 | + const { searchParams } = new URL(request.url); |
| 10 | + const url = searchParams.get("url"); |
| 11 | + |
| 12 | + // Check if URL parameter is provided |
| 13 | + if (!url) { |
| 14 | + return new Response( |
| 15 | + JSON.stringify({ error: "URL parameter is required" }), |
| 16 | + { |
| 17 | + status: 400, |
| 18 | + headers: { |
| 19 | + "Content-Type": "application/json", |
| 20 | + "Access-Control-Allow-Origin": "*", // Allow cross-origin access |
| 21 | + }, |
| 22 | + }, |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + // Fetch the content from the external URL |
| 27 | + const response = await fetch(url); |
| 28 | + |
| 29 | + if (!response.ok) { |
| 30 | + return new Response( |
| 31 | + JSON.stringify({ |
| 32 | + error: `Failed to fetch from URL: ${response.statusText}`, |
| 33 | + }), |
| 34 | + { |
| 35 | + status: response.status, |
| 36 | + headers: { |
| 37 | + "Content-Type": "application/json", |
| 38 | + "Access-Control-Allow-Origin": "*", // Allow cross-origin access |
| 39 | + }, |
| 40 | + }, |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + // Get the original content type and filename |
| 45 | + let contentType = response.headers.get("content-type") || ""; |
| 46 | + // Try to get filename from Content-Disposition header, fallback to URL |
| 47 | + let fileName = "file"; |
| 48 | + const contentDisposition = response.headers.get("content-disposition"); |
| 49 | + if (contentDisposition) { |
| 50 | + const match = contentDisposition.match( |
| 51 | + /filename\*?=(?:UTF-8'')?["']?([^;"']+)/i, |
| 52 | + ); |
| 53 | + if (match && match[1]) { |
| 54 | + fileName = decodeURIComponent(match[1]); |
| 55 | + } |
| 56 | + } |
| 57 | + if (fileName === "file") { |
| 58 | + const urlPath = new URL(url).pathname; |
| 59 | + const urlFileName = urlPath.split("/").pop() || "file"; |
| 60 | + // Only use the filename from URL if it includes an extension |
| 61 | + if (/\.[a-zA-Z0-9]+$/i.test(urlFileName)) { |
| 62 | + fileName = urlFileName; |
| 63 | + } |
| 64 | + // If the filename does not have an extension, guess from contentType |
| 65 | + else if (!/\.[a-z0-9]+$/i.test(fileName) && contentType) { |
| 66 | + const extMap: Record<string, string> = { |
| 67 | + "image/png": "png", |
| 68 | + "image/webp": "webp", |
| 69 | + "audio/flac": "flac", |
| 70 | + "video/mp4": "mp4", |
| 71 | + }; |
| 72 | + const guessedExt = extMap[contentType.split(";")[0].trim()]; |
| 73 | + if (guessedExt) { |
| 74 | + fileName += `.${guessedExt}`; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // If content type is not octet-stream, return the response directly to reduce latency |
| 80 | + if ( |
| 81 | + contentType && |
| 82 | + contentType !== "application/octet-stream" && |
| 83 | + contentType !== "binary/octet-stream" |
| 84 | + ) { |
| 85 | + return new Response(response.body, { |
| 86 | + status: 200, |
| 87 | + headers: { |
| 88 | + "Content-Type": contentType, |
| 89 | + "Content-Disposition": `inline; filename="${fileName}"`, |
| 90 | + "Access-Control-Allow-Origin": "*", // Allow cross-origin access |
| 91 | + "Cache-Control": "public, max-age=86400", // Cache for 24 hours |
| 92 | + }, |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + // For unknown or generic content types, process further |
| 97 | + const blob = await response.blob(); |
| 98 | + const arrayBuffer = await blob.arrayBuffer(); |
| 99 | + |
| 100 | + // Detect content type from file signature, especially if the content type is generic or missing |
| 101 | + if ( |
| 102 | + !contentType || |
| 103 | + contentType === "application/octet-stream" || |
| 104 | + contentType === "binary/octet-stream" |
| 105 | + ) { |
| 106 | + const detectedContentType = await detectContentType(arrayBuffer, fileName); |
| 107 | + if (detectedContentType) { |
| 108 | + contentType = detectedContentType; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Check if the file type is supported |
| 113 | + const extension = fileName.split(".").pop()?.toLowerCase() || ""; |
| 114 | + const isSupported = ["png", "webp", "flac", "mp4"].some( |
| 115 | + (ext) => contentType.includes(ext) || extension === ext, |
| 116 | + ); |
| 117 | + |
| 118 | + if (!isSupported) { |
| 119 | + return new Response( |
| 120 | + JSON.stringify({ |
| 121 | + error: `Unsupported file format: ${contentType || extension}`, |
| 122 | + }), |
| 123 | + { |
| 124 | + status: 415, // Unsupported Media Type |
| 125 | + headers: { |
| 126 | + "Content-Type": "application/json", |
| 127 | + "Access-Control-Allow-Origin": "*", // Allow cross-origin access |
| 128 | + }, |
| 129 | + }, |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + // Return the original content with appropriate headers |
| 134 | + return new Response(blob, { |
| 135 | + status: 200, |
| 136 | + headers: { |
| 137 | + "Content-Type": contentType, |
| 138 | + "Content-Disposition": `inline; filename="${fileName}"`, |
| 139 | + "Access-Control-Allow-Origin": "*", // Allow cross-origin access |
| 140 | + "Cache-Control": "public, max-age=86400", // Cache for 24 hours |
| 141 | + }, |
| 142 | + }); |
| 143 | +} |
0 commit comments