This commit is contained in:
Eduard Prigoana 2026-02-21 14:10:24 +02:00 committed by GitHub
parent cd7db67d44
commit 8c1b009705
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,10 +8,10 @@ export async function onRequest(context) {
console.log('Incoming request:', request.method); console.log('Incoming request:', request.method);
// Handle CORS preflight // -------------------------
// CORS Preflight
// -------------------------
if (request.method === 'OPTIONS') { if (request.method === 'OPTIONS') {
console.log('Handling OPTIONS preflight');
return new Response(null, { return new Response(null, {
status: 204, status: 204,
headers: { headers: {
@ -23,64 +23,59 @@ export async function onRequest(context) {
} }
if (request.method !== 'POST') { if (request.method !== 'POST') {
console.log('Method not allowed:', request.method); return new Response(
JSON.stringify({ error: 'Method not allowed' }),
return new Response(JSON.stringify({ error: 'Method not allowed' }), { {
status: 405, status: 405,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
}, },
}); }
);
} }
try { try {
console.log('Parsing form data...'); // -------------------------
// Parse form data
// -------------------------
const formData = await request.formData(); const formData = await request.formData();
const file = formData.get('file'); const file = formData.get('file');
console.log('File received:', file);
if (!file) { if (!file) {
console.error('No file provided'); return new Response(
JSON.stringify({ error: 'No file provided' }),
return new Response(JSON.stringify({ error: 'No file provided' }), { { status: 400 }
status: 400, );
});
} }
if (!file.type || !file.type.startsWith('image/')) { if (!file.type || !file.type.startsWith('image/')) {
console.error('Invalid file type:', file?.type); return new Response(
JSON.stringify({ error: 'File must be an image' }),
return new Response(JSON.stringify({ error: 'File must be an image' }), { { status: 400 }
status: 400, );
});
} }
const maxSize = 10 * 1024 * 1024; const maxSize = 10 * 1024 * 1024;
console.log('File size:', file.size);
if (file.size > maxSize) { if (file.size > maxSize) {
console.error('File too large');
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
error: 'File size exceeds 10MB limit', error: 'File size exceeds 10MB limit',
size: file.size, size: file.size,
}), }),
{ { status: 400 }
status: 400,
}
); );
} }
const fileBytes = await file.arrayBuffer(); const fileBytes = await file.arrayBuffer();
console.log('File bytes length:', fileBytes.byteLength);
// ------------------------- // -------------------------
// STEP 1 — Metadata Request // STEP 1 — Metadata Request
// ------------------------- // -------------------------
const cookies = request.headers.get('cookie');
const metadataPayload = { const metadataPayload = {
files: [ files: [
{ {
@ -92,58 +87,50 @@ export async function onRequest(context) {
}; };
console.log('Sending metadata request...'); console.log('Sending metadata request...');
console.log('Metadata payload:', JSON.stringify(metadataPayload));
const metadataResponse = await fetch(API_BASE, { const metadataResponse = await fetch(API_BASE, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0', 'User-Agent': 'Mozilla/5.0',
Origin: 'https://imgur.gg', 'Referer': 'https://temp.imgur.gg/',
'Origin': 'https://temp.imgur.gg',
...(cookies ? { Cookie: cookies } : {}),
}, },
body: JSON.stringify(metadataPayload), body: JSON.stringify(metadataPayload),
}); });
console.log('Metadata status:', metadataResponse.status);
const metadataText = await metadataResponse.text(); const metadataText = await metadataResponse.text();
console.log('Metadata raw response:', metadataText);
if (!metadataResponse.ok) { if (!metadataResponse.ok) {
throw new Error(`Metadata request failed: ${metadataResponse.status} - ${metadataText}`); throw new Error(
`Metadata request failed: ${metadataResponse.status} - ${metadataText}`
);
} }
let metadata; let metadata;
try { try {
metadata = JSON.parse(metadataText); metadata = JSON.parse(metadataText);
} catch (err) { } catch {
console.error('Failed to parse metadata JSON');
throw new Error('Metadata response not valid JSON'); throw new Error('Metadata response not valid JSON');
} }
console.log('Metadata parsed:', metadata);
if (!metadata.success || !metadata.files || !metadata.files[0]) { if (!metadata.success || !metadata.files || !metadata.files[0]) {
throw new Error('Metadata missing required fields'); throw new Error('Metadata missing required fields');
} }
const fileInfo = metadata.files[0]; const fileInfo = metadata.files[0];
console.log('File info:', fileInfo);
const uploadUrl = fileInfo.uploadUrl; const uploadUrl = fileInfo.uploadUrl;
if (!uploadUrl) { if (!uploadUrl) {
throw new Error('No uploadUrl returned from metadata'); throw new Error('No uploadUrl returned from metadata');
} }
console.log('Upload URL:', uploadUrl);
// ------------------------- // -------------------------
// STEP 2 — Upload File // STEP 2 — Upload File
// ------------------------- // -------------------------
console.log('Uploading file...');
const uploadResponse = await fetch(uploadUrl, { const uploadResponse = await fetch(uploadUrl, {
method: 'PUT', method: 'PUT',
body: fileBytes, body: fileBytes,
@ -152,19 +139,16 @@ export async function onRequest(context) {
}, },
}); });
console.log('Upload status:', uploadResponse.status);
const uploadText = await uploadResponse.text(); const uploadText = await uploadResponse.text();
console.log('Upload raw response:', uploadText);
if (!uploadResponse.ok) { if (!uploadResponse.ok) {
throw new Error(`File upload failed: ${uploadResponse.status} - ${uploadText}`); throw new Error(
`File upload failed: ${uploadResponse.status} - ${uploadText}`
);
} }
const publicUrl = `https://i.imgur.gg/${fileInfo.fileId}-${fileInfo.fileName}`; const publicUrl = `https://i.imgur.gg/${fileInfo.fileId}-${fileInfo.fileName}`;
console.log('Upload successful:', publicUrl);
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
success: true, success: true,