FileUpload: upload to discord if size does not exceed allowed size (#924)
* FileUpload: upload via discord if file size does not exceed allowed file size * Change default file upload limit to 8 MB * :) * t * Update index.tsx * FileUpload: get fallback file size from getUserMaxFileSize instead of hardcoded values * FileUpload: move findbyProps * Update index.tsx * Update index.tsx --------- Co-authored-by: thororen1234 <78185467+thororen1234@users.noreply.github.com> Co-authored-by: creations <creations@creations.works> Co-authored-by: Marvin Blank <blank@dontsp.am>
This commit is contained in:
parent
242966a803
commit
dbc2bcfb7e
2 changed files with 31 additions and 2 deletions
|
|
@ -12,14 +12,15 @@ import { OpenExternalIcon } from "@components/Icons";
|
||||||
import { Devs, EquicordDevs } from "@utils/constants";
|
import { Devs, EquicordDevs } from "@utils/constants";
|
||||||
import { classNameFactory } from "@utils/css";
|
import { classNameFactory } from "@utils/css";
|
||||||
import definePlugin from "@utils/types";
|
import definePlugin from "@utils/types";
|
||||||
import { DraftType, FluxDispatcher, Menu, PermissionsBits, PermissionStore, React, useEffect, useState } from "@webpack/common";
|
import { findByPropsLazy } from "@webpack";
|
||||||
|
import { DraftType, FluxDispatcher, Menu, PermissionsBits, PermissionStore, React, useEffect, UserStore, useState } from "@webpack/common";
|
||||||
|
|
||||||
import { settings } from "./settings";
|
import { settings } from "./settings";
|
||||||
import { serviceLabels, ServiceType } from "./types";
|
import { serviceLabels, ServiceType } from "./types";
|
||||||
import { getMediaUrl } from "./utils/getMediaUrl";
|
import { getMediaUrl } from "./utils/getMediaUrl";
|
||||||
import { cancelCurrentUpload, getUploadState, isConfigured, subscribeUploadState, uploadFile, uploadPickedFile, uploadProvidedFiles } from "./utils/upload";
|
import { cancelCurrentUpload, getUploadState, isConfigured, subscribeUploadState, uploadFile, uploadPickedFile, uploadProvidedFiles } from "./utils/upload";
|
||||||
|
|
||||||
const cl = classNameFactory("vc-file-upload-");
|
const cl = classNameFactory("vc-file-upload-");
|
||||||
|
const { getUserMaxFileSize } = findByPropsLazy("getUserMaxFileSize");
|
||||||
let uploadAddFilesInterceptor: ((event: unknown) => void) | null = null;
|
let uploadAddFilesInterceptor: ((event: unknown) => void) | null = null;
|
||||||
|
|
||||||
type UploadAddFilesEvent = {
|
type UploadAddFilesEvent = {
|
||||||
|
|
@ -28,8 +29,22 @@ type UploadAddFilesEvent = {
|
||||||
uploads?: unknown;
|
uploads?: unknown;
|
||||||
items?: unknown;
|
items?: unknown;
|
||||||
draftType?: unknown;
|
draftType?: unknown;
|
||||||
|
maxFileSize?: unknown;
|
||||||
|
fileSizeLimit?: unknown;
|
||||||
|
limits?: {
|
||||||
|
fileSize?: unknown;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function shouldInterceptUploadFiles(files: readonly File[], payload: UploadAddFilesEvent): boolean {
|
||||||
|
if (!settings.store.interceptDiscordUploadOnlyOverLimit) return true;
|
||||||
|
|
||||||
|
const directLimit = [payload.maxFileSize, payload.fileSizeLimit, payload.limits?.fileSize].find(limit => Number.isFinite(limit)) as number | undefined;
|
||||||
|
const fallbackLimit = getUserMaxFileSize(UserStore.getCurrentUser());
|
||||||
|
const discordLimit = Math.max(0, directLimit ?? fallbackLimit);
|
||||||
|
|
||||||
|
return files.some(file => file.size > discordLimit);
|
||||||
|
}
|
||||||
function extractFilesFromValue(value: unknown): File[] {
|
function extractFilesFromValue(value: unknown): File[] {
|
||||||
if (value instanceof File) return [value];
|
if (value instanceof File) return [value];
|
||||||
|
|
||||||
|
|
@ -68,6 +83,7 @@ function interceptUploadAddFiles(event: unknown): void {
|
||||||
const uniqueFiles = Array.from(new Set(files));
|
const uniqueFiles = Array.from(new Set(files));
|
||||||
|
|
||||||
if (!uniqueFiles.length) return;
|
if (!uniqueFiles.length) return;
|
||||||
|
if (!shouldInterceptUploadFiles(uniqueFiles, payload)) return;
|
||||||
|
|
||||||
payload.files = [];
|
payload.files = [];
|
||||||
payload.uploads = [];
|
payload.uploads = [];
|
||||||
|
|
|
||||||
|
|
@ -172,6 +172,12 @@ export const settings = definePluginSettings({
|
||||||
default: false,
|
default: false,
|
||||||
hidden: true
|
hidden: true
|
||||||
},
|
},
|
||||||
|
interceptDiscordUploadOnlyOverLimit: {
|
||||||
|
type: OptionType.BOOLEAN,
|
||||||
|
description: "Only intercept uploads that exceed Discord file size limit.",
|
||||||
|
default: true,
|
||||||
|
hidden: true
|
||||||
|
},
|
||||||
gofileToken: {
|
gofileToken: {
|
||||||
type: OptionType.STRING,
|
type: OptionType.STRING,
|
||||||
description: "Optional GoFile API token",
|
description: "Optional GoFile API token",
|
||||||
|
|
@ -518,6 +524,13 @@ export function SettingsComponent() {
|
||||||
/>
|
/>
|
||||||
</SettingsSection>
|
</SettingsSection>
|
||||||
|
|
||||||
|
<SettingsSection tag="label" name="Only Intercept Over Discord File Size Limit" description="Use FileUpload only for files larger than your current Discord upload limit" inlineSetting>
|
||||||
|
<Switch
|
||||||
|
checked={store.interceptDiscordUploadOnlyOverLimit}
|
||||||
|
onChange={v => store.interceptDiscordUploadOnlyOverLimit = v}
|
||||||
|
/>
|
||||||
|
</SettingsSection>
|
||||||
|
|
||||||
<SettingsSection name="Upload Timeout" description="Maximum time to wait per upload attempt before switching to fallback">
|
<SettingsSection name="Upload Timeout" description="Maximum time to wait per upload attempt before switching to fallback">
|
||||||
<Select
|
<Select
|
||||||
options={[
|
options={[
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue