feat(plugin): FixFileExtensions

This commit is contained in:
thororen1234 2024-10-27 02:07:41 -04:00
parent 2c773ec7d1
commit 0d4cfd5e3b
3 changed files with 67 additions and 20 deletions

View file

@ -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;
},
});

View file

@ -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) {

View file

@ -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<string, string>);
/**
* 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,
},