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) {
|
export async function onRequest(context) {
|
||||||
const { request } = context;
|
const { request } = context;
|
||||||
|
|
||||||
console.log("Incoming request:", request.method);
|
console.log('Incoming request:', request.method);
|
||||||
|
|
||||||
// Handle CORS preflight
|
// Handle CORS preflight
|
||||||
if (request.method === 'OPTIONS') {
|
if (request.method === 'OPTIONS') {
|
||||||
console.log("Handling OPTIONS preflight");
|
console.log('Handling OPTIONS preflight');
|
||||||
|
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 204,
|
status: 204,
|
||||||
|
|
@ -23,7 +23,7 @@ export async function onRequest(context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.method !== 'POST') {
|
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' }), {
|
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
|
||||||
status: 405,
|
status: 405,
|
||||||
|
|
@ -35,14 +35,14 @@ export async function onRequest(context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("Parsing form data...");
|
console.log('Parsing 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);
|
console.log('File received:', file);
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
console.error("No file provided");
|
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,
|
||||||
|
|
@ -50,7 +50,7 @@ export async function onRequest(context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file.type || !file.type.startsWith('image/')) {
|
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' }), {
|
return new Response(JSON.stringify({ error: 'File must be an image' }), {
|
||||||
status: 400,
|
status: 400,
|
||||||
|
|
@ -58,21 +58,24 @@ export async function onRequest(context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxSize = 10 * 1024 * 1024;
|
const maxSize = 10 * 1024 * 1024;
|
||||||
console.log("File size:", file.size);
|
console.log('File size:', file.size);
|
||||||
|
|
||||||
if (file.size > maxSize) {
|
if (file.size > maxSize) {
|
||||||
console.error("File too large");
|
console.error('File too large');
|
||||||
|
|
||||||
return new Response(JSON.stringify({
|
return new Response(
|
||||||
error: 'File size exceeds 10MB limit',
|
JSON.stringify({
|
||||||
size: file.size
|
error: 'File size exceeds 10MB limit',
|
||||||
}), {
|
size: file.size,
|
||||||
status: 400,
|
}),
|
||||||
});
|
{
|
||||||
|
status: 400,
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileBytes = await file.arrayBuffer();
|
const fileBytes = await file.arrayBuffer();
|
||||||
console.log("File bytes length:", fileBytes.byteLength);
|
console.log('File bytes length:', fileBytes.byteLength);
|
||||||
|
|
||||||
// -------------------------
|
// -------------------------
|
||||||
// STEP 1 — Metadata Request
|
// STEP 1 — Metadata Request
|
||||||
|
|
@ -88,60 +91,58 @@ export async function onRequest(context) {
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log("Sending metadata request...");
|
console.log('Sending metadata request...');
|
||||||
console.log("Metadata payload:", JSON.stringify(metadataPayload));
|
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',
|
||||||
'User-Agent': 'Mozilla/5.0',
|
'User-Agent': 'Mozilla/5.0',
|
||||||
'Origin': 'https://your-domain.com',
|
Origin: 'https://your-domain.com',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(metadataPayload),
|
body: JSON.stringify(metadataPayload),
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Metadata status:", metadataResponse.status);
|
console.log('Metadata status:', metadataResponse.status);
|
||||||
|
|
||||||
const metadataText = await metadataResponse.text();
|
const metadataText = await metadataResponse.text();
|
||||||
console.log("Metadata raw response:", metadataText);
|
console.log('Metadata raw response:', metadataText);
|
||||||
|
|
||||||
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;
|
||||||
try {
|
try {
|
||||||
metadata = JSON.parse(metadataText);
|
metadata = JSON.parse(metadataText);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Failed to parse metadata JSON");
|
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);
|
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);
|
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);
|
console.log('Upload URL:', uploadUrl);
|
||||||
|
|
||||||
// -------------------------
|
// -------------------------
|
||||||
// STEP 2 — Upload File
|
// STEP 2 — Upload File
|
||||||
// -------------------------
|
// -------------------------
|
||||||
|
|
||||||
console.log("Uploading file...");
|
console.log('Uploading file...');
|
||||||
|
|
||||||
const uploadResponse = await fetch(uploadUrl, {
|
const uploadResponse = await fetch(uploadUrl, {
|
||||||
method: 'PUT',
|
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();
|
const uploadText = await uploadResponse.text();
|
||||||
console.log("Upload raw response:", uploadText);
|
console.log('Upload raw response:', uploadText);
|
||||||
|
|
||||||
if (!uploadResponse.ok) {
|
if (!uploadResponse.ok) {
|
||||||
throw new Error(
|
throw new Error(`File upload failed: ${uploadResponse.status} - ${uploadText}`);
|
||||||
`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);
|
console.log('Upload successful:', publicUrl);
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
|
@ -181,9 +180,8 @@ export async function onRequest(context) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Upload failed:", error);
|
console.error('Upload failed:', error);
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue