style: auto-fix linting issues
This commit is contained in:
parent
497f2cfab6
commit
286a3e36d9
1 changed files with 38 additions and 40 deletions
|
|
@ -6,11 +6,11 @@ const API_BASE = 'https://temp.imgur.gg/api/upload';
|
|||
export async function onRequest(context) {
|
||||
const { request } = context;
|
||||
|
||||
console.log("Incoming request:", request.method);
|
||||
console.log('Incoming request:', request.method);
|
||||
|
||||
// Handle CORS preflight
|
||||
if (request.method === 'OPTIONS') {
|
||||
console.log("Handling OPTIONS preflight");
|
||||
console.log('Handling OPTIONS preflight');
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
|
|
@ -23,7 +23,7 @@ export async function onRequest(context) {
|
|||
}
|
||||
|
||||
if (request.method !== 'POST') {
|
||||
console.log("Method not allowed:", request.method);
|
||||
console.log('Method not allowed:', request.method);
|
||||
|
||||
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
|
||||
status: 405,
|
||||
|
|
@ -35,14 +35,14 @@ export async function onRequest(context) {
|
|||
}
|
||||
|
||||
try {
|
||||
console.log("Parsing form data...");
|
||||
console.log('Parsing form data...');
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('file');
|
||||
|
||||
console.log("File received:", file);
|
||||
console.log('File received:', file);
|
||||
|
||||
if (!file) {
|
||||
console.error("No file provided");
|
||||
console.error('No file provided');
|
||||
|
||||
return new Response(JSON.stringify({ error: 'No file provided' }), {
|
||||
status: 400,
|
||||
|
|
@ -50,7 +50,7 @@ export async function onRequest(context) {
|
|||
}
|
||||
|
||||
if (!file.type || !file.type.startsWith('image/')) {
|
||||
console.error("Invalid file type:", file?.type);
|
||||
console.error('Invalid file type:', file?.type);
|
||||
|
||||
return new Response(JSON.stringify({ error: 'File must be an image' }), {
|
||||
status: 400,
|
||||
|
|
@ -58,21 +58,24 @@ export async function onRequest(context) {
|
|||
}
|
||||
|
||||
const maxSize = 10 * 1024 * 1024;
|
||||
console.log("File size:", file.size);
|
||||
console.log('File size:', file.size);
|
||||
|
||||
if (file.size > maxSize) {
|
||||
console.error("File too large");
|
||||
console.error('File too large');
|
||||
|
||||
return new Response(JSON.stringify({
|
||||
error: 'File size exceeds 10MB limit',
|
||||
size: file.size
|
||||
}), {
|
||||
status: 400,
|
||||
});
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error: 'File size exceeds 10MB limit',
|
||||
size: file.size,
|
||||
}),
|
||||
{
|
||||
status: 400,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const fileBytes = await file.arrayBuffer();
|
||||
console.log("File bytes length:", fileBytes.byteLength);
|
||||
console.log('File bytes length:', fileBytes.byteLength);
|
||||
|
||||
// -------------------------
|
||||
// STEP 1 — Metadata Request
|
||||
|
|
@ -88,60 +91,58 @@ export async function onRequest(context) {
|
|||
],
|
||||
};
|
||||
|
||||
console.log("Sending metadata request...");
|
||||
console.log("Metadata payload:", JSON.stringify(metadataPayload));
|
||||
console.log('Sending metadata request...');
|
||||
console.log('Metadata payload:', JSON.stringify(metadataPayload));
|
||||
|
||||
const metadataResponse = await fetch(API_BASE, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': 'Mozilla/5.0',
|
||||
'Origin': 'https://your-domain.com',
|
||||
Origin: 'https://your-domain.com',
|
||||
},
|
||||
body: JSON.stringify(metadataPayload),
|
||||
});
|
||||
|
||||
console.log("Metadata status:", metadataResponse.status);
|
||||
console.log('Metadata status:', metadataResponse.status);
|
||||
|
||||
const metadataText = await metadataResponse.text();
|
||||
console.log("Metadata raw response:", metadataText);
|
||||
console.log('Metadata raw response:', metadataText);
|
||||
|
||||
if (!metadataResponse.ok) {
|
||||
throw new Error(
|
||||
`Metadata request failed: ${metadataResponse.status} - ${metadataText}`
|
||||
);
|
||||
throw new Error(`Metadata request failed: ${metadataResponse.status} - ${metadataText}`);
|
||||
}
|
||||
|
||||
let metadata;
|
||||
try {
|
||||
metadata = JSON.parse(metadataText);
|
||||
} catch (err) {
|
||||
console.error("Failed to parse metadata JSON");
|
||||
throw new Error("Metadata response not valid JSON");
|
||||
console.error('Failed to parse metadata JSON');
|
||||
throw new Error('Metadata response not valid JSON');
|
||||
}
|
||||
|
||||
console.log("Metadata parsed:", metadata);
|
||||
console.log('Metadata parsed:', metadata);
|
||||
|
||||
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];
|
||||
console.log("File info:", fileInfo);
|
||||
console.log('File info:', fileInfo);
|
||||
|
||||
const uploadUrl = fileInfo.uploadUrl;
|
||||
|
||||
if (!uploadUrl) {
|
||||
throw new Error("No uploadUrl returned from metadata");
|
||||
throw new Error('No uploadUrl returned from metadata');
|
||||
}
|
||||
|
||||
console.log("Upload URL:", uploadUrl);
|
||||
console.log('Upload URL:', uploadUrl);
|
||||
|
||||
// -------------------------
|
||||
// STEP 2 — Upload File
|
||||
// -------------------------
|
||||
|
||||
console.log("Uploading file...");
|
||||
console.log('Uploading file...');
|
||||
|
||||
const uploadResponse = await fetch(uploadUrl, {
|
||||
method: 'PUT',
|
||||
|
|
@ -151,20 +152,18 @@ export async function onRequest(context) {
|
|||
},
|
||||
});
|
||||
|
||||
console.log("Upload status:", uploadResponse.status);
|
||||
console.log('Upload status:', uploadResponse.status);
|
||||
|
||||
const uploadText = await uploadResponse.text();
|
||||
console.log("Upload raw response:", uploadText);
|
||||
console.log('Upload raw response:', uploadText);
|
||||
|
||||
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}`;
|
||||
|
||||
console.log("Upload successful:", publicUrl);
|
||||
console.log('Upload successful:', publicUrl);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
|
|
@ -181,9 +180,8 @@ export async function onRequest(context) {
|
|||
},
|
||||
}
|
||||
);
|
||||
|
||||
} catch (error) {
|
||||
console.error("Upload failed:", error);
|
||||
console.error('Upload failed:', error);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
|
|
|
|||
Loading…
Reference in a new issue