Update upload.js

This commit is contained in:
Eduard Prigoana 2026-02-21 18:48:51 +02:00 committed by GitHub
parent 3528b49d64
commit 8c77b18073
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,3 @@
// functions/upload.js
// 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'; const PING_URL = 'https://temp.imgur.gg/api/ping';
@ -29,10 +26,37 @@ export async function onRequest(context) {
} }
try { try {
const formData = await request.formData(); const contentType = request.headers.get('content-type') || '';
const file = formData.get('file'); let file;
let fileName;
let fileType;
if (!file) { if (contentType.includes('application/json')) {
const body = await request.json();
if (!body.fileUrl) {
return new Response(JSON.stringify({ error: 'No fileUrl provided' }), {
status: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
});
}
const fileResponse = await fetch(body.fileUrl);
if (!fileResponse.ok) {
throw new Error('Failed to fetch remote file');
}
const buffer = await fileResponse.arrayBuffer();
file = buffer;
fileName = body.fileName || body.fileUrl.split('/').pop() || 'file';
fileType = fileResponse.headers.get('content-type') || 'application/octet-stream';
} else {
const formData = await request.formData();
const uploadedFile = formData.get('file');
if (!uploadedFile) {
return new Response(JSON.stringify({ error: 'No file provided' }), { return new Response(JSON.stringify({ error: 'No file provided' }), {
status: 400, status: 400,
headers: { headers: {
@ -42,25 +66,23 @@ export async function onRequest(context) {
}); });
} }
const maxSize = 500 * 1024 * 1024; if (uploadedFile.size > 500 * 1024 * 1024) {
return new Response(JSON.stringify({
if (file.size > maxSize) {
return new Response(
JSON.stringify({
error: 'File size exceeds 500MB limit', error: 'File size exceeds 500MB limit',
size: file.size, size: uploadedFile.size,
}), }), {
{
status: 400, status: 400,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
}, },
} });
);
} }
const fileBytes = await file.arrayBuffer(); file = await uploadedFile.arrayBuffer();
fileName = uploadedFile.name;
fileType = uploadedFile.type || 'application/octet-stream';
}
const pingResponse = await fetch(PING_URL, { const pingResponse = await fetch(PING_URL, {
method: 'GET', method: 'GET',
@ -76,9 +98,9 @@ export async function onRequest(context) {
const metadataPayload = { const metadataPayload = {
files: [ files: [
{ {
fileName: file.name, fileName,
fileType: file.type || 'application/octet-stream', fileType,
fileSize: file.size, fileSize: file.byteLength || 0,
}, },
], ],
}; };
@ -112,12 +134,7 @@ export async function onRequest(context) {
throw new Error(`Metadata request failed: ${metadataResponse.status} - ${metadataText}`); throw new Error(`Metadata request failed: ${metadataResponse.status} - ${metadataText}`);
} }
let metadata; const metadata = JSON.parse(metadataText);
try {
metadata = JSON.parse(metadataText);
} catch {
throw new Error('Metadata response not valid JSON');
}
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');
@ -132,9 +149,9 @@ export async function onRequest(context) {
const uploadResponse = await fetch(uploadUrl, { const uploadResponse = await fetch(uploadUrl, {
method: 'PUT', method: 'PUT',
body: fileBytes, body: file,
headers: { headers: {
'Content-Type': file.type || 'application/octet-stream', 'Content-Type': fileType,
}, },
}); });
@ -145,36 +162,28 @@ export async function onRequest(context) {
const publicUrl = `https://i.imgur.gg/${fileInfo.fileId}-${fileInfo.fileName}`; const publicUrl = `https://i.imgur.gg/${fileInfo.fileId}-${fileInfo.fileName}`;
return new Response( return new Response(JSON.stringify({
JSON.stringify({
success: true, success: true,
url: publicUrl, url: publicUrl,
fileId: fileInfo.fileId, fileId: fileInfo.fileId,
fileName: fileInfo.fileName, fileName: fileInfo.fileName,
}), }), {
{
status: 200, status: 200,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
}, },
} });
);
} catch (error) { } catch (error) {
console.error('Upload failed:', error); return new Response(JSON.stringify({
return new Response(
JSON.stringify({
error: 'Upload failed', error: 'Upload failed',
message: error.message, message: error.message,
}), }), {
{
status: 500, status: 500,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
}, },
} });
);
} }
} }