this gotta work bro

This commit is contained in:
Eduard Prigoana 2026-02-21 13:14:29 +00:00
parent 8c1b009705
commit b62f4144ac

View file

@ -2,15 +2,11 @@
// Handles cover image uploads via imgur.gg API // Handles cover image uploads via imgur.gg API
const API_BASE = 'https://temp.imgur.gg/api/upload'; const API_BASE = 'https://temp.imgur.gg/api/upload';
const PING_URL = 'https://temp.imgur.gg/api/ping';
export async function onRequest(context) { export async function onRequest(context) {
const { request } = context; const { request } = context;
console.log('Incoming request:', request.method);
// -------------------------
// CORS Preflight
// -------------------------
if (request.method === 'OPTIONS') { if (request.method === 'OPTIONS') {
return new Response(null, { return new Response(null, {
status: 204, status: 204,
@ -23,37 +19,25 @@ export async function onRequest(context) {
} }
if (request.method !== 'POST') { if (request.method !== 'POST') {
return new Response( return new Response(JSON.stringify({ error: 'Method not allowed' }), {
JSON.stringify({ error: 'Method not allowed' }), status: 405,
{ headers: {
status: 405, 'Content-Type': 'application/json',
headers: { 'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json', },
'Access-Control-Allow-Origin': '*', });
},
}
);
} }
try { try {
// -------------------------
// Parse form data
// -------------------------
const formData = await request.formData(); const formData = await request.formData();
const file = formData.get('file'); const file = formData.get('file');
if (!file) { if (!file) {
return new Response( return new Response(JSON.stringify({ error: 'No file provided' }), { status: 400 });
JSON.stringify({ error: 'No file provided' }),
{ status: 400 }
);
} }
if (!file.type || !file.type.startsWith('image/')) { if (!file.type || !file.type.startsWith('image/')) {
return new Response( return new Response(JSON.stringify({ error: 'File must be an image' }), { status: 400 });
JSON.stringify({ error: 'File must be an image' }),
{ status: 400 }
);
} }
const maxSize = 10 * 1024 * 1024; const maxSize = 10 * 1024 * 1024;
@ -70,11 +54,16 @@ export async function onRequest(context) {
const fileBytes = await file.arrayBuffer(); const fileBytes = await file.arrayBuffer();
// ------------------------- const pingResponse = await fetch(PING_URL, {
// STEP 1 — Metadata Request method: 'GET',
// ------------------------- headers: {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
},
});
const cookies = request.headers.get('cookie'); const setCookie = pingResponse.headers.get('set-cookie') || '';
const sessionCookie = setCookie.split(';').find((c) => c.trim().startsWith('_s=')) || '';
const metadataPayload = { const metadataPayload = {
files: [ files: [
@ -86,17 +75,25 @@ export async function onRequest(context) {
], ],
}; };
console.log('Sending metadata request...');
const metadataResponse = await fetch(API_BASE, { const metadataResponse = await fetch(API_BASE, {
method: 'POST', method: 'POST',
headers: { headers: {
Accept: '*/*',
'Accept-Language': 'en-US,en;q=0.9',
'Cache-Control': 'no-cache',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Accept': 'application/json', Origin: 'https://temp.imgur.gg',
'User-Agent': 'Mozilla/5.0', Pragma: 'no-cache',
'Referer': 'https://temp.imgur.gg/', Referer: 'https://temp.imgur.gg/',
'Origin': 'https://temp.imgur.gg', 'Sec-Ch-Ua': '"Not A(Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
...(cookies ? { Cookie: cookies } : {}), 'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
Cookie: sessionCookie,
}, },
body: JSON.stringify(metadataPayload), body: JSON.stringify(metadataPayload),
}); });
@ -104,9 +101,7 @@ export async function onRequest(context) {
const metadataText = await metadataResponse.text(); const metadataText = await metadataResponse.text();
if (!metadataResponse.ok) { if (!metadataResponse.ok) {
throw new Error( throw new Error(`Metadata request failed: ${metadataResponse.status} - ${metadataText}`);
`Metadata request failed: ${metadataResponse.status} - ${metadataText}`
);
} }
let metadata; let metadata;
@ -127,10 +122,6 @@ export async function onRequest(context) {
throw new Error('No uploadUrl returned from metadata'); throw new Error('No uploadUrl returned from metadata');
} }
// -------------------------
// STEP 2 — Upload File
// -------------------------
const uploadResponse = await fetch(uploadUrl, { const uploadResponse = await fetch(uploadUrl, {
method: 'PUT', method: 'PUT',
body: fileBytes, body: fileBytes,
@ -139,12 +130,9 @@ export async function onRequest(context) {
}, },
}); });
const uploadText = await uploadResponse.text();
if (!uploadResponse.ok) { if (!uploadResponse.ok) {
throw new Error( const uploadText = await uploadResponse.text();
`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}`;