Update upload.js to allow chunked uploads
This commit is contained in:
parent
812178e3f3
commit
545fa80734
1 changed files with 200 additions and 149 deletions
|
|
@ -1,198 +1,249 @@
|
||||||
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 COMPLETE_URL = "https://temp.imgur.gg/api/upload/complete";
|
||||||
|
const PING_URL = "https://temp.imgur.gg/api/ping";
|
||||||
|
|
||||||
export async function onRequest(context) {
|
export async function onRequest(context) {
|
||||||
const { request } = context;
|
const { request } = context;
|
||||||
|
|
||||||
if (request.method === 'OPTIONS') {
|
if (request.method === "OPTIONS") {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 204,
|
status: 204,
|
||||||
headers: {
|
headers: corsHeaders()
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
|
||||||
'Access-Control-Allow-Headers': '*',
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.method !== 'POST') {
|
if (request.method !== "POST") {
|
||||||
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
|
return jsonError("Method not allowed", 405);
|
||||||
status: 405,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const contentType = request.headers.get('content-type') || '';
|
const contentType = request.headers.get("content-type") || "";
|
||||||
let file;
|
let file;
|
||||||
let fileName;
|
let fileName;
|
||||||
let fileType;
|
let fileType;
|
||||||
|
|
||||||
if (contentType.includes('application/json')) {
|
/* ==========================
|
||||||
|
HANDLE FILE INPUT
|
||||||
|
========================== */
|
||||||
|
|
||||||
|
if (contentType.includes("application/json")) {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
|
|
||||||
if (!body.fileUrl) {
|
if (!body.fileUrl) {
|
||||||
return new Response(JSON.stringify({ error: 'No fileUrl provided' }), {
|
return jsonError("No fileUrl provided", 400);
|
||||||
status: 400,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileResponse = await fetch(body.fileUrl);
|
const res = await fetch(body.fileUrl);
|
||||||
if (!fileResponse.ok) {
|
if (!res.ok) throw new Error("Failed to fetch remote file");
|
||||||
throw new Error('Failed to fetch remote file');
|
|
||||||
}
|
|
||||||
|
|
||||||
const buffer = await fileResponse.arrayBuffer();
|
file = await res.arrayBuffer();
|
||||||
file = buffer;
|
fileName = body.fileName || body.fileUrl.split("/").pop();
|
||||||
fileName = body.fileName || body.fileUrl.split('/').pop() || 'file';
|
fileType = res.headers.get("content-type") || "application/octet-stream";
|
||||||
fileType = fileResponse.headers.get('content-type') || 'application/octet-stream';
|
|
||||||
} else {
|
} else {
|
||||||
const formData = await request.formData();
|
const form = await request.formData();
|
||||||
const uploadedFile = formData.get('file');
|
const uploaded = form.get("file");
|
||||||
|
|
||||||
if (!uploadedFile) {
|
if (!uploaded) return jsonError("No file provided", 400);
|
||||||
return new Response(JSON.stringify({ error: 'No file provided' }), {
|
|
||||||
status: 400,
|
if (uploaded.size > 500 * 1024 * 1024) {
|
||||||
headers: {
|
return jsonError("File exceeds 500MB", 400);
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uploadedFile.size > 500 * 1024 * 1024) {
|
file = await uploaded.arrayBuffer();
|
||||||
return new Response(
|
fileName = uploaded.name;
|
||||||
JSON.stringify({
|
fileType = uploaded.type || "application/octet-stream";
|
||||||
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, {
|
/* ==========================
|
||||||
method: 'GET',
|
GET UPLOAD METADATA
|
||||||
headers: {
|
========================== */
|
||||||
'User-Agent':
|
|
||||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
const ping = await fetch(PING_URL, {
|
||||||
},
|
method: "GET",
|
||||||
|
headers: userAgentHeaders()
|
||||||
});
|
});
|
||||||
|
|
||||||
const setCookie = pingResponse.headers.get('set-cookie') || '';
|
const cookieHeader = ping.headers.get("set-cookie") || "";
|
||||||
const sessionCookie = setCookie.split(';').find((c) => c.trim().startsWith('_s=')) || '';
|
const sessionCookie =
|
||||||
|
cookieHeader.split(";").find(c => c.trim().startsWith("_s=")) || "";
|
||||||
|
|
||||||
const metadataPayload = {
|
const metadataResp = await fetch(API_BASE, {
|
||||||
files: [
|
method: "POST",
|
||||||
{
|
|
||||||
fileName,
|
|
||||||
fileType,
|
|
||||||
fileSize: file.byteLength || 0,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
const metadataResponse = await fetch(API_BASE, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
headers: {
|
||||||
Accept: '*/*',
|
...userAgentHeaders(),
|
||||||
'Accept-Language': 'en-US,en;q=0.9',
|
"Content-Type": "application/json",
|
||||||
'Cache-Control': 'no-cache',
|
Cookie: sessionCookie
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Origin: 'https://temp.imgur.gg',
|
|
||||||
Pragma: 'no-cache',
|
|
||||||
Referer: 'https://temp.imgur.gg/',
|
|
||||||
'Sec-Ch-Ua': '"Not A(Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
|
|
||||||
'Sec-Ch-Ua-Mobile': '?0',
|
|
||||||
'Sec-Ch-Ua-Platform': '"Windows"',
|
|
||||||
'Sec-Fetch-Dest': 'empty',
|
|
||||||
'Sec-Fetch-Mode': 'cors',
|
|
||||||
'Sec-Fetch-Site': 'same-origin',
|
|
||||||
'User-Agent':
|
|
||||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
||||||
Cookie: sessionCookie,
|
|
||||||
},
|
},
|
||||||
body: JSON.stringify(metadataPayload),
|
body: JSON.stringify({
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
fileName,
|
||||||
|
fileType,
|
||||||
|
fileSize: file.byteLength
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
const metadataText = await metadataResponse.text();
|
const metadataText = await metadataResp.text();
|
||||||
|
|
||||||
if (!metadataResponse.ok) {
|
if (!metadataResp.ok) {
|
||||||
throw new Error(`Metadata request failed: ${metadataResponse.status} - ${metadataText}`);
|
throw new Error(`Metadata failed: ${metadataText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const metadata = JSON.parse(metadataText);
|
const metadata = JSON.parse(metadataText);
|
||||||
|
const fileInfo = metadata.files?.[0];
|
||||||
|
|
||||||
if (!metadata.success || !metadata.files || !metadata.files[0]) {
|
if (!fileInfo || !fileInfo.success) {
|
||||||
throw new Error('Metadata missing required fields');
|
throw new Error("Invalid metadata response");
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileInfo = metadata.files[0];
|
/* ==========================
|
||||||
const uploadUrl = fileInfo.uploadUrl;
|
HANDLE UPLOAD TYPE
|
||||||
|
========================== */
|
||||||
|
|
||||||
if (!uploadUrl) {
|
let finalData;
|
||||||
throw new Error('No uploadUrl returned from metadata');
|
|
||||||
}
|
|
||||||
|
|
||||||
const uploadResponse = await fetch(uploadUrl, {
|
if (fileInfo.isMultipart) {
|
||||||
method: 'PUT',
|
finalData = await handleMultipart(fileInfo, file);
|
||||||
body: file,
|
} else {
|
||||||
headers: {
|
finalData = await handleSingle(fileInfo.uploadUrl, file, fileType);
|
||||||
'Content-Type': fileType,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!uploadResponse.ok) {
|
|
||||||
const uploadText = await uploadResponse.text();
|
|
||||||
throw new Error(`File upload failed: ${uploadResponse.status} - ${uploadText}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const publicUrl = `https://i.imgur.gg/${fileInfo.fileId}-${fileInfo.fileName}`;
|
const publicUrl = `https://i.imgur.gg/${fileInfo.fileId}-${fileInfo.fileName}`;
|
||||||
|
|
||||||
return new Response(
|
return jsonResponse({
|
||||||
JSON.stringify({
|
success: true,
|
||||||
success: true,
|
url: publicUrl,
|
||||||
url: publicUrl,
|
fileId: fileInfo.fileId,
|
||||||
fileId: fileInfo.fileId,
|
fileName: fileInfo.fileName
|
||||||
fileName: fileInfo.fileName,
|
});
|
||||||
}),
|
|
||||||
{
|
} catch (err) {
|
||||||
status: 200,
|
return jsonError(err.message, 500);
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
error: 'Upload failed',
|
|
||||||
message: error.message,
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 500,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ===================================================== */
|
||||||
|
/* ================= SINGLE UPLOAD ===================== */
|
||||||
|
/* ===================================================== */
|
||||||
|
|
||||||
|
async function handleSingle(uploadUrl, fileBuffer, fileType) {
|
||||||
|
if (!uploadUrl) {
|
||||||
|
throw new Error("Missing uploadUrl for single upload");
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await fetch(uploadUrl, {
|
||||||
|
method: "PUT",
|
||||||
|
body: fileBuffer,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": fileType
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const txt = await res.text();
|
||||||
|
throw new Error(`Single upload failed: ${txt}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ===================================================== */
|
||||||
|
/* ================= MULTIPART UPLOAD =================== */
|
||||||
|
/* ===================================================== */
|
||||||
|
|
||||||
|
async function handleMultipart(fileInfo, fileBuffer) {
|
||||||
|
const { partUrls, partSize, uploadId, fileId } = fileInfo;
|
||||||
|
|
||||||
|
if (!partUrls || !uploadId) {
|
||||||
|
throw new Error("Invalid multipart metadata");
|
||||||
|
}
|
||||||
|
|
||||||
|
const parts = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < partUrls.length; i++) {
|
||||||
|
const start = i * partSize;
|
||||||
|
const end = start + partSize;
|
||||||
|
const chunk = fileBuffer.slice(start, end);
|
||||||
|
|
||||||
|
const uploadRes = await fetch(partUrls[i].url, {
|
||||||
|
method: "PUT",
|
||||||
|
body: chunk
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!uploadRes.ok) {
|
||||||
|
const txt = await uploadRes.text();
|
||||||
|
throw new Error(`Multipart part ${i + 1} failed: ${txt}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let etag = uploadRes.headers.get("etag") || "";
|
||||||
|
etag = etag.replace(/"/g, ""); // clean quotes
|
||||||
|
|
||||||
|
parts.push({
|
||||||
|
PartNumber: i + 1,
|
||||||
|
ETag: `"${etag}"`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ===== FINALIZE MULTIPART ===== */
|
||||||
|
|
||||||
|
const complete = await fetch(COMPLETE_URL, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
fileId,
|
||||||
|
uploadId,
|
||||||
|
parts
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
const completeText = await complete.text();
|
||||||
|
|
||||||
|
if (!complete.ok) {
|
||||||
|
throw new Error(`Multipart complete failed: ${completeText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const completeData = JSON.parse(completeText);
|
||||||
|
|
||||||
|
if (!completeData.success) {
|
||||||
|
throw new Error("Multipart finalize returned failure");
|
||||||
|
}
|
||||||
|
|
||||||
|
return completeData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ===================================================== */
|
||||||
|
/* ================= UTIL FUNCTIONS ===================== */
|
||||||
|
/* ===================================================== */
|
||||||
|
|
||||||
|
function jsonResponse(obj, status = 200) {
|
||||||
|
return new Response(JSON.stringify(obj), {
|
||||||
|
status,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
...corsHeaders()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function jsonError(message, status) {
|
||||||
|
return jsonResponse({ success: false, error: message }, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
function corsHeaders() {
|
||||||
|
return {
|
||||||
|
"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"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue