fix FileUpload + userhash (#762)
This commit is contained in:
parent
f02545d710
commit
d869df150e
3 changed files with 113 additions and 33 deletions
|
|
@ -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(
|
export async function fetchFile(
|
||||||
|
|
||||||
_: IpcMainInvokeEvent,
|
_: IpcMainInvokeEvent,
|
||||||
|
|
||||||
url: string
|
url: string
|
||||||
|
|
||||||
): Promise<{ success: boolean; data?: ArrayBuffer; contentType?: string; error?: string }> {
|
): Promise<{ success: boolean; data?: ArrayBuffer; contentType?: string; error?: string; }> {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,12 @@ export const settings = definePluginSettings({
|
||||||
default: "24h",
|
default: "24h",
|
||||||
hidden: true
|
hidden: true
|
||||||
},
|
},
|
||||||
|
catboxUserhash: {
|
||||||
|
type: OptionType.STRING,
|
||||||
|
description: "Catbox userhash for account binding",
|
||||||
|
default: "",
|
||||||
|
hidden: true
|
||||||
|
},
|
||||||
stripQueryParams: {
|
stripQueryParams: {
|
||||||
type: OptionType.BOOLEAN,
|
type: OptionType.BOOLEAN,
|
||||||
description: "Strip query params from uploaded URLs",
|
description: "Strip query params from uploaded URLs",
|
||||||
|
|
@ -180,6 +186,7 @@ export function SettingsComponent() {
|
||||||
const isEzHost = store.serviceType === ServiceType.EZHOST;
|
const isEzHost = store.serviceType === ServiceType.EZHOST;
|
||||||
const isS3 = store.serviceType === ServiceType.S3;
|
const isS3 = store.serviceType === ServiceType.S3;
|
||||||
const isZipline = store.serviceType === ServiceType.ZIPLINE;
|
const isZipline = store.serviceType === ServiceType.ZIPLINE;
|
||||||
|
const isCatbox = store.serviceType === ServiceType.CATBOX;
|
||||||
const isLitterbox = store.serviceType === ServiceType.LITTERBOX;
|
const isLitterbox = store.serviceType === ServiceType.LITTERBOX;
|
||||||
|
|
||||||
return (
|
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 && (
|
{isLitterbox && (
|
||||||
<SettingsSection name="Litterbox Expiry" description="How long uploads are retained">
|
<SettingsSection name="Litterbox Expiry" description="How long uploads are retained">
|
||||||
<Select
|
<Select
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ async function uploadToNest(fileBlob: Blob, filename: string): Promise<string> {
|
||||||
throw new Error(`Upload failed: ${response.status} ${errorText}`);
|
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) {
|
if (data.fileURL) {
|
||||||
return data.fileURL;
|
return data.fileURL;
|
||||||
|
|
@ -132,7 +132,7 @@ export function isConfigured(): boolean {
|
||||||
case ServiceType.NEST:
|
case ServiceType.NEST:
|
||||||
return Boolean(nestToken);
|
return Boolean(nestToken);
|
||||||
case ServiceType.EZHOST:
|
case ServiceType.EZHOST:
|
||||||
return Boolean((settings.store as { ezHostKey?: string }).ezHostKey);
|
return Boolean((settings.store as { ezHostKey?: string; }).ezHostKey);
|
||||||
case ServiceType.S3:
|
case ServiceType.S3:
|
||||||
return isS3Configured();
|
return isS3Configured();
|
||||||
case ServiceType.CATBOX:
|
case ServiceType.CATBOX:
|
||||||
|
|
@ -148,7 +148,7 @@ export function isConfigured(): boolean {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadToEzHost(fileBlob: Blob, filename: string): Promise<string> {
|
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");
|
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> {
|
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();
|
const formData = new FormData();
|
||||||
formData.append("reqtype", "fileupload");
|
formData.append("reqtype", "fileupload");
|
||||||
|
if (catboxUserhash) formData.append("userhash", catboxUserhash);
|
||||||
formData.append("fileToUpload", fileBlob, filename);
|
formData.append("fileToUpload", fileBlob, filename);
|
||||||
|
|
||||||
const uploadUrl = Native
|
const response = await fetch(`${CORS_PROXY}?url=${encodeURIComponent("https://catbox.moe/user/api.php")}`, {
|
||||||
? "https://catbox.moe/user/api.php"
|
|
||||||
: `${CORS_PROXY}?url=${encodeURIComponent("https://catbox.moe/user/api.php")}`;
|
|
||||||
|
|
||||||
const response = await fetch(uploadUrl, {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: formData
|
body: formData
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) throw new Error(`Upload failed: ${response.status} ${await response.text()}`);
|
||||||
const text = await response.text();
|
|
||||||
throw new Error(`Upload failed: ${response.status} ${text}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const text = (await response.text()).trim();
|
const text = (await response.text()).trim();
|
||||||
if (!text) {
|
if (!text) throw new Error("No URL returned from upload");
|
||||||
throw new Error("No URL returned from upload");
|
|
||||||
}
|
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -239,30 +237,27 @@ async function uploadTo0x0(fileBlob: Blob, filename: string): Promise<string> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadToLitterbox(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();
|
const formData = new FormData();
|
||||||
formData.append("reqtype", "fileupload");
|
formData.append("reqtype", "fileupload");
|
||||||
formData.append("time", settings.store.litterboxExpiry || "24h");
|
formData.append("time", expiry);
|
||||||
formData.append("fileToUpload", fileBlob, filename);
|
formData.append("fileToUpload", fileBlob, filename);
|
||||||
|
|
||||||
const uploadUrl = Native
|
const response = await fetch(`${CORS_PROXY}?url=${encodeURIComponent("https://litterbox.catbox.moe/resources/internals/api.php")}`, {
|
||||||
? "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, {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: formData
|
body: formData
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) throw new Error(`Upload failed: ${response.status} ${await response.text()}`);
|
||||||
const text = await response.text();
|
|
||||||
throw new Error(`Upload failed: ${response.status} ${text}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const text = (await response.text()).trim();
|
const text = (await response.text()).trim();
|
||||||
if (!text) {
|
if (!text) throw new Error("No URL returned from upload");
|
||||||
throw new Error("No URL returned from upload");
|
|
||||||
}
|
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue