From 0d4cfd5e3ba073642b4a4d8064d5fc29d290bd61 Mon Sep 17 00:00:00 2001 From: thororen1234 <78185467+thororen1234@users.noreply.github.com> Date: Sun, 27 Oct 2024 02:07:41 -0400 Subject: [PATCH] feat(plugin): FixFileExtensions --- .../fixFileExtensions/index.tsx | 46 +++++++++++++++++++ src/plugins/anonymiseFileNames/index.tsx | 24 ++-------- src/utils/constants.ts | 17 ++++++- 3 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 src/equicordplugins/fixFileExtensions/index.tsx diff --git a/src/equicordplugins/fixFileExtensions/index.tsx b/src/equicordplugins/fixFileExtensions/index.tsx new file mode 100644 index 00000000..8bd89ef9 --- /dev/null +++ b/src/equicordplugins/fixFileExtensions/index.tsx @@ -0,0 +1,46 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2024 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { Upload } from "@api/MessageEvents"; +import { Settings } from "@api/Settings"; +import { EquicordDevs, reverseExtensionMap } from "@utils/constants"; +import definePlugin from "@utils/types"; + +type ExtUpload = Upload & { fixExtension?: boolean; }; + +export default definePlugin({ + name: "FixFileExtensions", + authors: [EquicordDevs.thororen], + description: "Fixes file extensions by renaming them to a compatible supported format if possible", + patches: [ + { + find: "instantBatchUpload:", + predicate: () => !Settings.plugins.AnonymiseFileNames.enabled, + replacement: { + match: /uploadFiles:(\i),/, + replace: + "uploadFiles:(...args)=>(args[0].uploads.forEach(f=>f.filename=$self.fixExt(f)),$1(...args)),", + }, + }, + { + find: 'addFilesTo:"message.attachments"', + predicate: () => !Settings.plugins.AnonymiseFileNames.enabled, + replacement: { + match: /(\i.uploadFiles\((\i),)/, + replace: "$2.forEach(f=>f.filename=$self.fixExt(f)),$1", + }, + } + ], + fixExt(upload: ExtUpload) { + const file = upload.filename; + const extIdx = file.lastIndexOf("."); + const fileName = extIdx !== -1 ? file.substring(0, extIdx) : ""; + const ext = extIdx !== -1 ? file.slice(extIdx) : ""; + const newExt = reverseExtensionMap[ext] || ext; + + return fileName + newExt; + }, +}); diff --git a/src/plugins/anonymiseFileNames/index.tsx b/src/plugins/anonymiseFileNames/index.tsx index 0a36248d..fbc99848 100644 --- a/src/plugins/anonymiseFileNames/index.tsx +++ b/src/plugins/anonymiseFileNames/index.tsx @@ -17,9 +17,9 @@ */ import { Upload } from "@api/MessageEvents"; -import { definePluginSettings } from "@api/Settings"; +import { definePluginSettings, Settings } from "@api/Settings"; import ErrorBoundary from "@components/ErrorBoundary"; -import { Devs, EquicordDevs } from "@utils/constants"; +import { Devs, reverseExtensionMap } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; import { findByCodeLazy, findByPropsLazy } from "@webpack"; @@ -42,11 +42,6 @@ const settings = definePluginSettings({ type: OptionType.BOOLEAN, default: true, }, - fixOpusExtensions: { - description: "Whether to fix file extensions by default", - type: OptionType.BOOLEAN, - default: true, - }, method: { description: "Anonymising method", type: OptionType.SELECT, @@ -72,7 +67,7 @@ const settings = definePluginSettings({ export default definePlugin({ name: "AnonymiseFileNames", - authors: [Devs.fawn, EquicordDevs.thororen], + authors: [Devs.fawn], description: "Anonymise uploaded file names", patches: [ { @@ -125,17 +120,8 @@ export default definePlugin({ const tarMatch = tarExtMatcher.exec(file); const extIdx = tarMatch?.index ?? file.lastIndexOf("."); let ext = extIdx !== -1 ? file.slice(extIdx) : ""; - const matchList = [ - ".ogv", - ".oga", - ".ogx", - ".ogm", - ".spx", - ".opus" - ]; - - if (settings.store.fixOpusExtensions && ext !== "ogg" && matchList.includes(ext)) { - ext = ".ogg"; + if (Settings.plugins.FixFileExtensions.enabled) { + ext = reverseExtensionMap[ext]; } switch (settings.store.method) { diff --git a/src/utils/constants.ts b/src/utils/constants.ts index bb2dc56f..f0840390 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -31,6 +31,21 @@ export interface Dev { badge?: boolean; } +// No clue where to put these +const extensionMap = { + "ogg": [".ogv", ".oga", ".ogx", ".ogm", ".spx", ".opus"], + "jpg": [".jpg", ".jpeg", ".jfif", ".jpe", ".jif", ".jfi", ".pjpeg", ".pjp"], + "svg": [".svgz"], + "mp4": [".m4v", ".m4a", ".m4r", ".m4b", ".m4p"], + "mov": [".movie", ".qt"], +}; + +export const reverseExtensionMap = Object.entries(extensionMap).reduce((acc, [target, exts]) => { + exts.forEach(ext => acc[ext] = `.${target}`); + return acc; +}, {} as Record); + + /** * If you made a plugin or substantial contribution, add yourself here. * This object is used for the plugin author list, as well as to add a contributor badge to your profile. @@ -928,7 +943,7 @@ export const EquicordDevs = Object.freeze({ name: "Leko", id: 108153734541942784n }, - SomeAspy: { + SomeAspy: { name: "SomeAspy", id: 516750892372852754n, },