Update upload.js
This commit is contained in:
parent
3528b49d64
commit
8c77b18073
1 changed files with 78 additions and 69 deletions
|
|
@ -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,38 +26,63 @@ 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')) {
|
||||||
return new Response(JSON.stringify({ error: 'No file provided' }), {
|
const body = await request.json();
|
||||||
status: 400,
|
if (!body.fileUrl) {
|
||||||
headers: {
|
return new Response(JSON.stringify({ error: 'No fileUrl provided' }), {
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const maxSize = 500 * 1024 * 1024;
|
|
||||||
|
|
||||||
if (file.size > maxSize) {
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
error: 'File size exceeds 500MB limit',
|
|
||||||
size: file.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();
|
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' }), {
|
||||||
|
status: 400,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uploadedFile.size > 500 * 1024 * 1024) {
|
||||||
|
return new Response(JSON.stringify({
|
||||||
|
error: 'File size exceeds 500MB limit',
|
||||||
|
size: uploadedFile.size,
|
||||||
|
}), {
|
||||||
|
status: 400,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
{
|
headers: {
|
||||||
status: 200,
|
'Content-Type': 'application/json',
|
||||||
headers: {
|
'Access-Control-Allow-Origin': '*',
|
||||||
'Content-Type': 'application/json',
|
},
|
||||||
'Access-Control-Allow-Origin': '*',
|
});
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Upload failed:', error);
|
return new Response(JSON.stringify({
|
||||||
|
error: 'Upload failed',
|
||||||
return new Response(
|
message: error.message,
|
||||||
JSON.stringify({
|
}), {
|
||||||
error: 'Upload failed',
|
status: 500,
|
||||||
message: error.message,
|
headers: {
|
||||||
}),
|
'Content-Type': 'application/json',
|
||||||
{
|
'Access-Control-Allow-Origin': '*',
|
||||||
status: 500,
|
},
|
||||||
headers: {
|
});
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue