style: auto-fix linting issues
This commit is contained in:
parent
2a05de8b34
commit
5fe3a7068a
1 changed files with 77 additions and 78 deletions
|
|
@ -1,83 +1,82 @@
|
||||||
const BASE = "https://temp.imgur.gg";
|
const BASE = 'https://temp.imgur.gg';
|
||||||
|
|
||||||
export async function onRequest(context) {
|
export async function onRequest(context) {
|
||||||
const { request } = context;
|
const { request } = context;
|
||||||
|
|
||||||
if (request.method !== "POST") {
|
if (request.method !== 'POST') {
|
||||||
return new Response("Method not allowed", { status: 405 });
|
return new Response('Method not allowed', { status: 405 });
|
||||||
}
|
|
||||||
|
|
||||||
const formData = await request.formData();
|
|
||||||
const file = formData.get("file");
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
return new Response("No file", { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const buffer = await file.arrayBuffer();
|
|
||||||
// 1. GET site to grab cookie
|
|
||||||
const getRes = await fetch(BASE, {
|
|
||||||
method: "GET",
|
|
||||||
});
|
|
||||||
|
|
||||||
const setCookie = getRes.headers.get("set-cookie") || "";
|
|
||||||
const cookie = setCookie.split(";")[0]; // _s=xxxxx
|
|
||||||
|
|
||||||
// 2. Request metadata WITH cookie
|
|
||||||
|
|
||||||
const metadataRes = await fetch(`${BASE}/api/upload`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Cookie: cookie,
|
|
||||||
Origin: BASE,
|
|
||||||
Referer: BASE + "/",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
fileName: file.name,
|
|
||||||
fileType: file.type,
|
|
||||||
fileSize: file.size,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!metadataRes.ok) {
|
|
||||||
const text = await metadataRes.text();
|
|
||||||
return new Response("Metadata failed: " + text, { status: 500 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const metadata = await metadataRes.json();
|
|
||||||
const uploadInfo = metadata.files[0];
|
|
||||||
|
|
||||||
// 3. Upload to signed URL
|
|
||||||
|
|
||||||
const uploadRes = await fetch(uploadInfo.uploadUrl, {
|
|
||||||
method: "PUT",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": file.type,
|
|
||||||
},
|
|
||||||
body: buffer,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!uploadRes.ok) {
|
|
||||||
return new Response("Upload failed", { status: 500 });
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. Return public URL
|
|
||||||
|
|
||||||
const publicUrl =
|
|
||||||
`https://i.imgur.gg/${uploadInfo.fileId}-${uploadInfo.fileName}`;
|
|
||||||
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
success: true,
|
|
||||||
url: publicUrl,
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
const formData = await request.formData();
|
||||||
|
const file = formData.get('file');
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
return new Response('No file', { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = await file.arrayBuffer();
|
||||||
|
// 1. GET site to grab cookie
|
||||||
|
const getRes = await fetch(BASE, {
|
||||||
|
method: 'GET',
|
||||||
|
});
|
||||||
|
|
||||||
|
const setCookie = getRes.headers.get('set-cookie') || '';
|
||||||
|
const cookie = setCookie.split(';')[0]; // _s=xxxxx
|
||||||
|
|
||||||
|
// 2. Request metadata WITH cookie
|
||||||
|
|
||||||
|
const metadataRes = await fetch(`${BASE}/api/upload`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Cookie: cookie,
|
||||||
|
Origin: BASE,
|
||||||
|
Referer: BASE + '/',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
fileName: file.name,
|
||||||
|
fileType: file.type,
|
||||||
|
fileSize: file.size,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!metadataRes.ok) {
|
||||||
|
const text = await metadataRes.text();
|
||||||
|
return new Response('Metadata failed: ' + text, { status: 500 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const metadata = await metadataRes.json();
|
||||||
|
const uploadInfo = metadata.files[0];
|
||||||
|
|
||||||
|
// 3. Upload to signed URL
|
||||||
|
|
||||||
|
const uploadRes = await fetch(uploadInfo.uploadUrl, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': file.type,
|
||||||
|
},
|
||||||
|
body: buffer,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!uploadRes.ok) {
|
||||||
|
return new Response('Upload failed', { status: 500 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Return public URL
|
||||||
|
|
||||||
|
const publicUrl = `https://i.imgur.gg/${uploadInfo.fileId}-${uploadInfo.fileName}`;
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: true,
|
||||||
|
url: publicUrl,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue