Update upload.js
This commit is contained in:
parent
384dd867cd
commit
2a05de8b34
1 changed files with 78 additions and 146 deletions
|
|
@ -1,151 +1,83 @@
|
||||||
// functions/upload.js
|
const BASE = "https://temp.imgur.gg";
|
||||||
// Handles cover image uploads via imgur.gg API
|
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
// Handle CORS preflight
|
if (request.method !== "POST") {
|
||||||
if (request.method === 'OPTIONS') {
|
return new Response("Method not allowed", { status: 405 });
|
||||||
return new Response(null, {
|
}
|
||||||
status: 204,
|
|
||||||
headers: {
|
const formData = await request.formData();
|
||||||
'Access-Control-Allow-Origin': '*',
|
const file = formData.get("file");
|
||||||
'Access-Control-Allow-Methods': 'POST',
|
|
||||||
'Access-Control-Allow-Headers': 'Content-Type',
|
if (!file) {
|
||||||
},
|
return new Response("No file", { status: 400 });
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
const buffer = await file.arrayBuffer();
|
||||||
if (request.method !== 'POST') {
|
// 1. GET site to grab cookie
|
||||||
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
|
const getRes = await fetch(BASE, {
|
||||||
status: 405,
|
method: "GET",
|
||||||
headers: {
|
});
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
const setCookie = getRes.headers.get("set-cookie") || "";
|
||||||
},
|
const cookie = setCookie.split(";")[0]; // _s=xxxxx
|
||||||
});
|
|
||||||
}
|
// 2. Request metadata WITH cookie
|
||||||
|
|
||||||
try {
|
const metadataRes = await fetch(`${BASE}/api/upload`, {
|
||||||
// Parse the multipart form data
|
method: "POST",
|
||||||
const formData = await request.formData();
|
headers: {
|
||||||
const file = formData.get('file');
|
"Content-Type": "application/json",
|
||||||
|
Cookie: cookie,
|
||||||
if (!file) {
|
Origin: BASE,
|
||||||
return new Response(JSON.stringify({ error: 'No file provided' }), {
|
Referer: BASE + "/",
|
||||||
status: 400,
|
},
|
||||||
headers: {
|
body: JSON.stringify({
|
||||||
'Content-Type': 'application/json',
|
files: [
|
||||||
'Access-Control-Allow-Origin': '*',
|
{
|
||||||
},
|
fileName: file.name,
|
||||||
});
|
fileType: file.type,
|
||||||
}
|
fileSize: file.size,
|
||||||
|
},
|
||||||
// Validate file type
|
],
|
||||||
if (!file.type.startsWith('image/')) {
|
}),
|
||||||
return new Response(JSON.stringify({ error: 'File must be an image' }), {
|
});
|
||||||
status: 400,
|
|
||||||
headers: {
|
if (!metadataRes.ok) {
|
||||||
'Content-Type': 'application/json',
|
const text = await metadataRes.text();
|
||||||
'Access-Control-Allow-Origin': '*',
|
return new Response("Metadata failed: " + text, { status: 500 });
|
||||||
},
|
}
|
||||||
});
|
|
||||||
}
|
const metadata = await metadataRes.json();
|
||||||
|
const uploadInfo = metadata.files[0];
|
||||||
// Validate file size (max 10MB)
|
|
||||||
const maxSize = 10 * 1024 * 1024; // 10MB
|
// 3. Upload to signed URL
|
||||||
if (file.size > maxSize) {
|
|
||||||
return new Response(JSON.stringify({ error: 'File size exceeds 10MB limit' }), {
|
const uploadRes = await fetch(uploadInfo.uploadUrl, {
|
||||||
status: 400,
|
method: "PUT",
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": file.type,
|
||||||
'Access-Control-Allow-Origin': '*',
|
},
|
||||||
},
|
body: buffer,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
if (!uploadRes.ok) {
|
||||||
// Get file bytes
|
return new Response("Upload failed", { status: 500 });
|
||||||
const fileBytes = await file.arrayBuffer();
|
}
|
||||||
|
|
||||||
// Step 1: Request upload metadata
|
// 4. Return public URL
|
||||||
const metadataPayload = {
|
|
||||||
files: [
|
const publicUrl =
|
||||||
{
|
`https://i.imgur.gg/${uploadInfo.fileId}-${uploadInfo.fileName}`;
|
||||||
fileName: file.name,
|
|
||||||
fileType: file.type,
|
return new Response(
|
||||||
fileSize: file.size,
|
JSON.stringify({
|
||||||
},
|
success: true,
|
||||||
],
|
url: publicUrl,
|
||||||
};
|
}),
|
||||||
|
{
|
||||||
const metadataResponse = await fetch(API_BASE, {
|
headers: { "Content-Type": "application/json" },
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(metadataPayload),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!metadataResponse.ok) {
|
|
||||||
throw new Error(`Metadata request failed: ${metadataResponse.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const metadata = await metadataResponse.json();
|
|
||||||
|
|
||||||
if (!metadata.success || !metadata.files || !metadata.files[0]) {
|
|
||||||
throw new Error('Failed to get upload URL from imgur.gg');
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileInfo = metadata.files[0];
|
|
||||||
const uploadUrl = fileInfo.uploadUrl;
|
|
||||||
|
|
||||||
// Step 2: Upload the file
|
|
||||||
const uploadResponse = await fetch(uploadUrl, {
|
|
||||||
method: 'PUT',
|
|
||||||
body: fileBytes,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': file.type,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!uploadResponse.ok) {
|
|
||||||
throw new Error(`File upload failed: ${uploadResponse.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 3: Return the public URL
|
|
||||||
const publicUrl = `https://i.imgur.gg/${fileInfo.fileId}-${fileInfo.fileName}`;
|
|
||||||
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
success: true,
|
|
||||||
url: publicUrl,
|
|
||||||
fileId: fileInfo.fileId,
|
|
||||||
fileName: fileInfo.fileName,
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 200,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Upload error:', error);
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
error: 'Upload failed',
|
|
||||||
message: error.message,
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 500,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue