session cookies for chunked uploads
This commit is contained in:
parent
bb427facf4
commit
19b5cc5b1d
1 changed files with 82 additions and 80 deletions
|
|
@ -1,87 +1,85 @@
|
|||
const API_BASE = 'https://temp.imgur.gg/api/upload';
|
||||
const COMPLETE_URL = 'https://temp.imgur.gg/api/upload/complete';
|
||||
const PING_URL = 'https://temp.imgur.gg/api/ping';
|
||||
const API_BASE = "https://temp.imgur.gg/api/upload";
|
||||
const COMPLETE_URL = "https://temp.imgur.gg/api/upload/complete";
|
||||
const PING_URL = "https://temp.imgur.gg/api/ping";
|
||||
|
||||
export async function onRequest(context) {
|
||||
const { request } = context;
|
||||
|
||||
if (request.method === 'OPTIONS') {
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
headers: corsHeaders(),
|
||||
});
|
||||
if (request.method === "OPTIONS") {
|
||||
return new Response(null, { status: 204, headers: corsHeaders() });
|
||||
}
|
||||
|
||||
if (request.method !== 'POST') {
|
||||
return jsonError('Method not allowed', 405);
|
||||
if (request.method !== "POST") {
|
||||
return jsonError("Method not allowed", 405);
|
||||
}
|
||||
|
||||
try {
|
||||
const contentType = request.headers.get('content-type') || '';
|
||||
const contentType = request.headers.get("content-type") || "";
|
||||
let file;
|
||||
let fileName;
|
||||
let fileType;
|
||||
|
||||
/* ==========================
|
||||
HANDLE FILE INPUT
|
||||
========================== */
|
||||
/* ========================= */
|
||||
/* GET FILE */
|
||||
/* ========================= */
|
||||
|
||||
if (contentType.includes('application/json')) {
|
||||
if (contentType.includes("application/json")) {
|
||||
const body = await request.json();
|
||||
|
||||
if (!body.fileUrl) {
|
||||
return jsonError('No fileUrl provided', 400);
|
||||
}
|
||||
if (!body.fileUrl) return jsonError("No fileUrl provided", 400);
|
||||
|
||||
const res = await fetch(body.fileUrl);
|
||||
if (!res.ok) throw new Error('Failed to fetch remote file');
|
||||
if (!res.ok) throw new Error("Failed to fetch remote file");
|
||||
|
||||
file = await res.arrayBuffer();
|
||||
fileName = body.fileName || body.fileUrl.split('/').pop();
|
||||
fileType = res.headers.get('content-type') || 'application/octet-stream';
|
||||
fileName = body.fileName || body.fileUrl.split("/").pop();
|
||||
fileType = res.headers.get("content-type") || "application/octet-stream";
|
||||
} else {
|
||||
const form = await request.formData();
|
||||
const uploaded = form.get('file');
|
||||
|
||||
if (!uploaded) return jsonError('No file provided', 400);
|
||||
const uploaded = form.get("file");
|
||||
if (!uploaded) return jsonError("No file provided", 400);
|
||||
|
||||
if (uploaded.size > 500 * 1024 * 1024) {
|
||||
return jsonError('File exceeds 500MB', 400);
|
||||
return jsonError("File exceeds 500MB", 400);
|
||||
}
|
||||
|
||||
file = await uploaded.arrayBuffer();
|
||||
fileName = uploaded.name;
|
||||
fileType = uploaded.type || 'application/octet-stream';
|
||||
fileType = uploaded.type || "application/octet-stream";
|
||||
}
|
||||
|
||||
/* ==========================
|
||||
GET UPLOAD METADATA
|
||||
========================== */
|
||||
/* ========================= */
|
||||
/* GET SESSION */
|
||||
/* ========================= */
|
||||
|
||||
const ping = await fetch(PING_URL, {
|
||||
method: 'GET',
|
||||
headers: userAgentHeaders(),
|
||||
method: "GET",
|
||||
headers: userAgentHeaders()
|
||||
});
|
||||
|
||||
const cookieHeader = ping.headers.get('set-cookie') || '';
|
||||
const sessionCookie = cookieHeader.split(';').find((c) => c.trim().startsWith('_s=')) || '';
|
||||
const setCookie = ping.headers.get("set-cookie") || "";
|
||||
const sessionCookie =
|
||||
setCookie.split(";").find(c => c.trim().startsWith("_s=")) || "";
|
||||
|
||||
/* ========================= */
|
||||
/* GET METADATA */
|
||||
/* ========================= */
|
||||
|
||||
const metadataResp = await fetch(API_BASE, {
|
||||
method: 'POST',
|
||||
method: "POST",
|
||||
headers: {
|
||||
...userAgentHeaders(),
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: sessionCookie,
|
||||
"Content-Type": "application/json",
|
||||
"Cookie": sessionCookie,
|
||||
...userAgentHeaders()
|
||||
},
|
||||
body: JSON.stringify({
|
||||
files: [
|
||||
{
|
||||
fileName,
|
||||
fileType,
|
||||
fileSize: file.byteLength,
|
||||
},
|
||||
],
|
||||
}),
|
||||
fileSize: file.byteLength
|
||||
}
|
||||
]
|
||||
})
|
||||
});
|
||||
|
||||
const metadataText = await metadataResp.text();
|
||||
|
|
@ -94,19 +92,19 @@ export async function onRequest(context) {
|
|||
const fileInfo = metadata.files?.[0];
|
||||
|
||||
if (!fileInfo || !fileInfo.success) {
|
||||
throw new Error('Invalid metadata response');
|
||||
throw new Error("Invalid metadata response");
|
||||
}
|
||||
|
||||
/* ==========================
|
||||
HANDLE UPLOAD TYPE
|
||||
========================== */
|
||||
/* ========================= */
|
||||
/* HANDLE UPLOAD */
|
||||
/* ========================= */
|
||||
|
||||
let finalData;
|
||||
let result;
|
||||
|
||||
if (fileInfo.isMultipart) {
|
||||
finalData = await handleMultipart(fileInfo, file);
|
||||
result = await handleMultipart(fileInfo, file, sessionCookie);
|
||||
} else {
|
||||
finalData = await handleSingle(fileInfo.uploadUrl, file, fileType);
|
||||
result = await handleSingle(fileInfo.uploadUrl, file, fileType);
|
||||
}
|
||||
|
||||
const publicUrl = `https://i.imgur.gg/${fileInfo.fileId}-${fileInfo.fileName}`;
|
||||
|
|
@ -115,8 +113,9 @@ export async function onRequest(context) {
|
|||
success: true,
|
||||
url: publicUrl,
|
||||
fileId: fileInfo.fileId,
|
||||
fileName: fileInfo.fileName,
|
||||
fileName: fileInfo.fileName
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
return jsonError(err.message, 500);
|
||||
}
|
||||
|
|
@ -127,16 +126,14 @@ export async function onRequest(context) {
|
|||
/* ===================================================== */
|
||||
|
||||
async function handleSingle(uploadUrl, fileBuffer, fileType) {
|
||||
if (!uploadUrl) {
|
||||
throw new Error('Missing uploadUrl for single upload');
|
||||
}
|
||||
if (!uploadUrl) throw new Error("Missing uploadUrl");
|
||||
|
||||
const res = await fetch(uploadUrl, {
|
||||
method: 'PUT',
|
||||
method: "PUT",
|
||||
body: fileBuffer,
|
||||
headers: {
|
||||
'Content-Type': fileType,
|
||||
},
|
||||
"Content-Type": fileType
|
||||
}
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
|
|
@ -151,11 +148,11 @@ async function handleSingle(uploadUrl, fileBuffer, fileType) {
|
|||
/* ================= MULTIPART UPLOAD =================== */
|
||||
/* ===================================================== */
|
||||
|
||||
async function handleMultipart(fileInfo, fileBuffer) {
|
||||
async function handleMultipart(fileInfo, fileBuffer, sessionCookie) {
|
||||
const { partUrls, partSize, uploadId, fileId } = fileInfo;
|
||||
|
||||
if (!partUrls || !uploadId) {
|
||||
throw new Error('Invalid multipart metadata');
|
||||
throw new Error("Invalid multipart metadata");
|
||||
}
|
||||
|
||||
const parts = [];
|
||||
|
|
@ -166,8 +163,8 @@ async function handleMultipart(fileInfo, fileBuffer) {
|
|||
const chunk = fileBuffer.slice(start, end);
|
||||
|
||||
const uploadRes = await fetch(partUrls[i].url, {
|
||||
method: 'PUT',
|
||||
body: chunk,
|
||||
method: "PUT",
|
||||
body: chunk
|
||||
});
|
||||
|
||||
if (!uploadRes.ok) {
|
||||
|
|
@ -175,55 +172,59 @@ async function handleMultipart(fileInfo, fileBuffer) {
|
|||
throw new Error(`Multipart part ${i + 1} failed: ${txt}`);
|
||||
}
|
||||
|
||||
let etag = uploadRes.headers.get('etag') || '';
|
||||
etag = etag.replace(/"/g, ''); // clean quotes
|
||||
let etag = uploadRes.headers.get("etag") || "";
|
||||
etag = etag.replace(/"/g, "");
|
||||
|
||||
parts.push({
|
||||
PartNumber: i + 1,
|
||||
ETag: `"${etag}"`,
|
||||
ETag: `"${etag}"`
|
||||
});
|
||||
}
|
||||
|
||||
/* ===== FINALIZE MULTIPART ===== */
|
||||
/* ========================= */
|
||||
/* FINALIZE UPLOAD */
|
||||
/* ========================= */
|
||||
|
||||
const complete = await fetch(COMPLETE_URL, {
|
||||
method: 'PUT',
|
||||
const completeResp = await fetch(COMPLETE_URL, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
"Cookie": sessionCookie,
|
||||
...userAgentHeaders()
|
||||
},
|
||||
body: JSON.stringify({
|
||||
fileId,
|
||||
uploadId,
|
||||
parts,
|
||||
}),
|
||||
parts
|
||||
})
|
||||
});
|
||||
|
||||
const completeText = await complete.text();
|
||||
const completeText = await completeResp.text();
|
||||
|
||||
if (!complete.ok) {
|
||||
if (!completeResp.ok) {
|
||||
throw new Error(`Multipart complete failed: ${completeText}`);
|
||||
}
|
||||
|
||||
const completeData = JSON.parse(completeText);
|
||||
|
||||
if (!completeData.success) {
|
||||
throw new Error('Multipart finalize returned failure');
|
||||
throw new Error("Multipart finalize returned failure");
|
||||
}
|
||||
|
||||
return completeData;
|
||||
}
|
||||
|
||||
/* ===================================================== */
|
||||
/* ================= UTIL FUNCTIONS ===================== */
|
||||
/* ================= UTILITIES ========================= */
|
||||
/* ===================================================== */
|
||||
|
||||
function jsonResponse(obj, status = 200) {
|
||||
return new Response(JSON.stringify(obj), {
|
||||
status,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...corsHeaders(),
|
||||
},
|
||||
"Content-Type": "application/json",
|
||||
...corsHeaders()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -233,14 +234,15 @@ function jsonError(message, status) {
|
|||
|
||||
function corsHeaders() {
|
||||
return {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
||||
'Access-Control-Allow-Headers': '*',
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "*"
|
||||
};
|
||||
}
|
||||
|
||||
function userAgentHeaders() {
|
||||
return {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0',
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0"
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue