1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| const HTML = ` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Air1临时文件</title> <link rel="icon" type="image/png" href="https://air1.cn/favicon.png" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body { font-family: -apple-system, BlinkMacSystemFont, sans-serif; max-width: 500px; margin: 40px auto; padding: 20px; } h1 { text-align: center; } input, button { width: 100%; padding: 12px; margin: 10px 0; box-sizing: border-box; border: 1px solid #ccc; border-radius: 6px; } button { background: #007bff; color: white; border: none; cursor: pointer; } button:hover { background: #0069d9; } #result { margin-top: 15px; padding: 12px; background: #e8f4ff; border-radius: 6px; word-break: break-all; } a { color: #007bff; text-decoration: none; } a:hover { text-decoration: underline; } </style> </head> <body> <h1>📁 临时文件保存</h1> <input type="file" id="fileInput" /> <button onclick="upload()">上传(≤25MB)</button> <div id="result"></div> <p style="text-align:center;color:#666;font-size:14px;">文件 12 小时后自动删除</p>
<script> async function upload() { const file = document.getElementById('fileInput').files[0]; if (!file) return alert("请选择文件"); if (file.size > 26112000) return alert("文件不能超过 25MB");
const formData = new FormData(); formData.append("file", file); const btn = document.querySelector('button'); btn.disabled = true; btn.textContent = "上传中…";
try { const res = await fetch("/api/upload-public", { method: "POST", body: formData }); const data = await res.json(); const el = document.getElementById('result'); if (res.ok && data.downloadUrl) { el.innerHTML = '<strong>✅ 分享链接:</strong><br><a href="' + data.downloadUrl + '" target="_blank">' + data.downloadUrl + '</a>'; } else { el.innerText = "❌ " + (data.error || "上传失败"); } } catch (e) { document.getElementById('result').innerText = "网络错误:" + e.message; } finally { btn.disabled = false; btn.textContent = "上传(≤25MB)"; } } </script> </body> </html> `;
function generateFileId() { return Math.random().toString(36).substring(2, 6); }
async function handleFileUpload(file, env) { const MAX_SIZE = 26112000; if (file.size > MAX_SIZE) { return new Response(JSON.stringify({ error: "文件不能超过 25MB" }), { status: 400, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }); }
const fileId = generateFileId(); const arrayBuffer = await file.arrayBuffer();
await env.TEMP_STORE.put(fileId, arrayBuffer, { metadata: { filename: file.name || "file", contentType: file.type || "application/octet-stream" }, expirationTtl: 43200 });
const downloadUrl = `https://tmp.air1.cn/${fileId}`;
return new Response(JSON.stringify({ downloadUrl }), { headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }); }
async function handleProtectedUpload(file, env, request) { const authHeader = request.headers.get("Authorization"); const expectedToken = env.UPLOAD_TOKEN;
if (!expectedToken) { return new Response(JSON.stringify({ error: "服务器未配置 UPLOAD_TOKEN" }), { status: 500, headers: { "Content-Type": "application/json" } }); }
if (authHeader !== `Bearer ${expectedToken}`) { return new Response(JSON.stringify({ error: "无效或缺失 Token" }), { status: 401, headers: { "Content-Type": "application/json" } }); }
const MAX_SIZE = 26112000; if (file.size > MAX_SIZE) { return new Response(JSON.stringify({ error: "文件不能超过 25MB" }), { status: 400, headers: { "Content-Type": "application/json" } }); }
const fileId = generateFileId(); const arrayBuffer = await file.arrayBuffer();
await env.TEMP_STORE.put(fileId, arrayBuffer, { metadata: { filename: file.name || "file", contentType: file.type || "application/octet-stream" }, expirationTtl: 43200 });
const downloadUrl = `https://tmp.air1.cn/${fileId}`; return new Response(JSON.stringify({ downloadUrl }), { headers: { "Content-Type": "application/json" } }); }
export default { async fetch(request, env) { const url = new URL(request.url); const { pathname } = url;
if (pathname === "/") { return new Response(HTML, { headers: { "Content-Type": "text/html; charset=utf-8" } }); }
if (request.method === "OPTIONS") { return new Response(null, { headers: { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type" } }); }
if (pathname === "/api/upload-public" && request.method === "POST") { try { const formData = await request.formData(); const file = formData.get("file"); if (!file || !(file instanceof File)) { return new Response(JSON.stringify({ error: "未提供有效文件" }), { status: 400, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }); } return await handleFileUpload(file, env); } catch (e) { console.error("公开上传出错:", e); return new Response(JSON.stringify({ error: "服务器内部错误" }), { status: 500, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }); } }
if (pathname === "/api/upload" && request.method === "POST") { try { const formData = await request.formData(); const file = formData.get("file"); if (!file || !(file instanceof File)) { return new Response(JSON.stringify({ error: "未提供有效文件" }), { status: 400, headers: { "Content-Type": "application/json" } }); } return await handleProtectedUpload(file, env, request); } catch (e) { console.error("受保护上传出错:", e); return new Response(JSON.stringify({ error: "服务器内部错误" }), { status: 500, headers: { "Content-Type": "application/json" } }); } }
const segments = pathname.split('/').filter(Boolean); if (segments.length === 1 && segments[0].length >= 4) { const id = segments[0]; const reservedPaths = new Set([ 'api', 'upload', 'f', 'favicon.ico', 'robots.txt', 'about', 's' ]); if (!reservedPaths.has(id)) { const entry = await env.TEMP_STORE.getWithMetadata(id, "arrayBuffer"); if (entry.value) { return new Response(entry.value, { headers: { "Content-Type": entry.metadata?.contentType || "application/octet-stream", "Content-Disposition": "attachment; filename=\"" + encodeURIComponent(entry.metadata?.filename || 'file') + "\"", "Cache-Control": "no-store" } }); } } }
return new Response("Not Found", { status: 404 }); } };
|