FileUpload: allow configuration of cors proxy (#940)

This commit is contained in:
Key 2026-03-29 03:32:31 +01:00 • committed by GitHub
parent af41040297
commit 726fdb86a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 6 deletions

View file

@ -6,6 +6,10 @@
export const CORS_PROXY = "https://cors.keiran0.workers.dev";
export function toProxiedUrl(url: string): string {
return `${CORS_PROXY}?url=${encodeURIComponent(url)}`;
export function normalizeCorsProxyUrl(url?: string): string {
return (url?.trim() || CORS_PROXY).replace(/\/+$/, "");
}
export function toProxiedUrl(url: string, corsProxyUrl = CORS_PROXY): string {
return `${normalizeCorsProxyUrl(corsProxyUrl)}?url=${encodeURIComponent(url)}`;
}

View file

@ -12,6 +12,7 @@ import { useForceUpdater } from "@utils/react";
import { OptionType } from "@utils/types";
import { React, Select, showToast, TextArea, TextInput, Toasts } from "@webpack/common";
import { CORS_PROXY } from "./constants";
import { ServiceType } from "./types";
import { parseShareXConfig } from "./utils/sharex";
@ -196,6 +197,12 @@ export const settings = definePluginSettings({
default: false,
hidden: true
},
corsProxyUrl: {
type: OptionType.STRING,
description: "CORS proxy URL used for browser uploads",
default: CORS_PROXY,
hidden: true
},
apngToGif: {
type: OptionType.BOOLEAN,
description: "Convert APNG uploads to GIF",
@ -482,6 +489,18 @@ export function SettingsComponent() {
/>
</SettingsSection>
<SettingTextInput
name="CORS Proxy URL"
description="CORS proxy used for web uploads. Leave empty to use the default proxy"
value={store.corsProxyUrl || ""}
onChange={v => store.corsProxyUrl = v}
placeholder="https://your-cors-proxy.example.com"
/>
<SettingsSection name="Default CORS Proxy Source" description="Source code for the default CORS proxy">
<a href="https://codeberg.org/key/corsproxy" target="_blank" rel="noreferrer">codeberg.org/key/corsproxy</a>
</SettingsSection>
<SettingsSection tag="label" name="Convert APNG to GIF" description="Convert APNG files to GIF format" inlineSetting>
<Switch
checked={store.apngToGif}

View file

@ -18,6 +18,7 @@ type S3Store = {
s3PublicUrl?: string;
s3Prefix?: string;
s3ForcePathStyle?: boolean;
corsProxyUrl?: string;
};
const textEncoder = new TextEncoder();
@ -188,7 +189,7 @@ export async function uploadToS3(
throw new Error(result.error || "Upload failed");
}
} else {
const response = await fetch(toProxiedUrl(uploadUrl.toString()), {
const response = await fetch(toProxiedUrl(uploadUrl.toString(), settings.store.corsProxyUrl), {
method: "PUT",
headers: requestHeaders,
body: fileBlob

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { CORS_PROXY, toProxiedUrl } from "@equicordplugins/fileUpload/constants";
import { normalizeCorsProxyUrl, toProxiedUrl } from "@equicordplugins/fileUpload/constants";
import { settings } from "@equicordplugins/fileUpload/settings";
import { serviceLabels, ServiceType, ShareXUploaderConfig, UploadResponse } from "@equicordplugins/fileUpload/types";
import { copyToClipboard } from "@utils/clipboard";
@ -26,11 +26,13 @@ const Native = IS_DISCORD_DESKTOP
const logger = new Logger("FileUpload", "#7cb7ff");
function toProxyUrl(url: string): string {
if (Native || url.startsWith(`${CORS_PROXY}?url=`)) {
const corsProxyUrl = normalizeCorsProxyUrl((settings.store as { corsProxyUrl?: string; }).corsProxyUrl);
if (Native || url.startsWith(`${corsProxyUrl}?url=`)) {
return url;
}
return toProxiedUrl(url);
return toProxiedUrl(url, corsProxyUrl);
}
let isUploading = false;