fix FileUpload + userhash (#762)

This commit is contained in:
9gvuwu 2026-02-12 15:04:58 +02:00 • committed by GitHub
parent f02545d710
commit d869df150e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 113 additions and 33 deletions

View file

@ -136,13 +136,81 @@ export async function uploadToS3(
}
}
export async function uploadToCatbox(
_: IpcMainInvokeEvent,
fileBuffer: ArrayBuffer,
filename: string,
userhash?: string
): Promise<NativeUploadResult> {
try {
const formData = new FormData();
formData.append("reqtype", "fileupload");
if (userhash) {
formData.append("userhash", userhash);
}
formData.append("fileToUpload", new Blob([fileBuffer]), filename);
const response = await fetch("https://catbox.moe/user/api.php", {
method: "POST",
body: formData
});
if (!response.ok) {
const errorText = await response.text();
return { success: false, error: `Upload failed: ${response.status} ${errorText}` };
}
const text = (await response.text()).trim();
if (!text) {
return { success: false, error: "No URL returned from upload" };
}
return { success: true, url: text };
} catch (e) {
return { success: false, error: e instanceof Error ? e.message : "Unknown error" };
}
}
export async function uploadToLitterbox(
_: IpcMainInvokeEvent,
fileBuffer: ArrayBuffer,
filename: string,
expiry: string
): Promise<NativeUploadResult> {
try {
const formData = new FormData();
formData.append("reqtype", "fileupload");
formData.append("time", expiry);
formData.append("fileToUpload", new Blob([fileBuffer]), filename);
const response = await fetch("https://litterbox.catbox.moe/resources/internals/api.php", {
method: "POST",
body: formData
});
if (!response.ok) {
const errorText = await response.text();
return { success: false, error: `Upload failed: ${response.status} ${errorText}` };
}
const text = (await response.text()).trim();
if (!text) {
return { success: false, error: "No URL returned from upload" };
}
return { success: true, url: text };
} catch (e) {
return { success: false, error: e instanceof Error ? e.message : "Unknown error" };
}
}
export async function fetchFile(
_: IpcMainInvokeEvent,
url: string
): Promise<{ success: boolean; data?: ArrayBuffer; contentType?: string; error?: string }> {
): Promise<{ success: boolean; data?: ArrayBuffer; contentType?: string; error?: string; }> {
try {

View file

@ -128,6 +128,12 @@ export const settings = definePluginSettings({
default: "24h",
hidden: true
},
catboxUserhash: {
type: OptionType.STRING,
description: "Catbox userhash for account binding",
default: "",
hidden: true
},
stripQueryParams: {
type: OptionType.BOOLEAN,
description: "Strip query params from uploaded URLs",
@ -180,6 +186,7 @@ export function SettingsComponent() {
const isEzHost = store.serviceType === ServiceType.EZHOST;
const isS3 = store.serviceType === ServiceType.S3;
const isZipline = store.serviceType === ServiceType.ZIPLINE;
const isCatbox = store.serviceType === ServiceType.CATBOX;
const isLitterbox = store.serviceType === ServiceType.LITTERBOX;
return (
@ -310,6 +317,16 @@ export function SettingsComponent() {
</>
)}
{isCatbox && (
<SettingTextInput
name="Catbox Userhash"
description="Your Catbox userhash for account binding (leave empty for anonymous uploads)"
value={store.catboxUserhash}
onChange={v => store.catboxUserhash = v}
placeholder="Your Catbox userhash"
/>
)}
{isLitterbox && (
<SettingsSection name="Litterbox Expiry" description="How long uploads are retained">
<Select

View file

@ -107,7 +107,7 @@ async function uploadToNest(fileBlob: Blob, filename: string): Promise<string> {
throw new Error(`Upload failed: ${response.status} ${errorText}`);
}
const data = await response.json() as { fileURL?: string };
const data = await response.json() as { fileURL?: string; };
if (data.fileURL) {
return data.fileURL;
@ -132,7 +132,7 @@ export function isConfigured(): boolean {
case ServiceType.NEST:
return Boolean(nestToken);
case ServiceType.EZHOST:
return Boolean((settings.store as { ezHostKey?: string }).ezHostKey);
return Boolean((settings.store as { ezHostKey?: string; }).ezHostKey);
case ServiceType.S3:
return isS3Configured();
case ServiceType.CATBOX:
@ -148,7 +148,7 @@ export function isConfigured(): boolean {
}
async function uploadToEzHost(fileBlob: Blob, filename: string): Promise<string> {
const { ezHostKey } = (settings.store as { ezHostKey?: string });
const { ezHostKey } = (settings.store as { ezHostKey?: string; });
if (!ezHostKey) throw new Error("E-Z Host API key is required");
@ -193,29 +193,27 @@ async function uploadToEzHost(fileBlob: Blob, filename: string): Promise<string>
}
async function uploadToCatbox(fileBlob: Blob, filename: string): Promise<string> {
const { catboxUserhash } = settings.store;
if (Native) {
const result = await Native.uploadToCatbox(await fileBlob.arrayBuffer(), filename, catboxUserhash || undefined);
if (!result.success || !result.url) throw new Error(result.error || "No URL returned from upload");
return result.url;
}
const formData = new FormData();
formData.append("reqtype", "fileupload");
if (catboxUserhash) formData.append("userhash", catboxUserhash);
formData.append("fileToUpload", fileBlob, filename);
const uploadUrl = Native
? "https://catbox.moe/user/api.php"
: `${CORS_PROXY}?url=${encodeURIComponent("https://catbox.moe/user/api.php")}`;
const response = await fetch(uploadUrl, {
const response = await fetch(`${CORS_PROXY}?url=${encodeURIComponent("https://catbox.moe/user/api.php")}`, {
method: "POST",
body: formData
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Upload failed: ${response.status} ${text}`);
}
if (!response.ok) throw new Error(`Upload failed: ${response.status} ${await response.text()}`);
const text = (await response.text()).trim();
if (!text) {
throw new Error("No URL returned from upload");
}
if (!text) throw new Error("No URL returned from upload");
return text;
}
@ -239,30 +237,27 @@ async function uploadTo0x0(fileBlob: Blob, filename: string): Promise<string> {
}
async function uploadToLitterbox(fileBlob: Blob, filename: string): Promise<string> {
const expiry = settings.store.litterboxExpiry || "24h";
if (Native) {
const result = await Native.uploadToLitterbox(await fileBlob.arrayBuffer(), filename, expiry);
if (!result.success || !result.url) throw new Error(result.error || "No URL returned from upload");
return result.url;
}
const formData = new FormData();
formData.append("reqtype", "fileupload");
formData.append("time", settings.store.litterboxExpiry || "24h");
formData.append("time", expiry);
formData.append("fileToUpload", fileBlob, filename);
const uploadUrl = Native
? "https://litterbox.catbox.moe/resources/internals/api.php"
: `${CORS_PROXY}?url=${encodeURIComponent("https://litterbox.catbox.moe/resources/internals/api.php")}`;
const response = await fetch(uploadUrl, {
const response = await fetch(`${CORS_PROXY}?url=${encodeURIComponent("https://litterbox.catbox.moe/resources/internals/api.php")}`, {
method: "POST",
body: formData
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Upload failed: ${response.status} ${text}`);
}
if (!response.ok) throw new Error(`Upload failed: ${response.status} ${await response.text()}`);
const text = (await response.text()).trim();
if (!text) {
throw new Error("No URL returned from upload");
}
if (!text) throw new Error("No URL returned from upload");
return text;
}