FileUpload: allow configuration of cors proxy (#940)
This commit is contained in:
parent
af41040297
commit
726fdb86a2
4 changed files with 32 additions and 6 deletions
|
|
@ -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)}`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue