mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-09 22:53:02 -04:00
feat(plugin): FixFileExtensions
This commit is contained in:
parent
2c773ec7d1
commit
0d4cfd5e3b
3 changed files with 67 additions and 20 deletions
46
src/equicordplugins/fixFileExtensions/index.tsx
Normal file
46
src/equicordplugins/fixFileExtensions/index.tsx
Normal 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;
|
||||||
|
},
|
||||||
|
});
|
|
@ -17,9 +17,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Upload } from "@api/MessageEvents";
|
import { Upload } from "@api/MessageEvents";
|
||||||
import { definePluginSettings } from "@api/Settings";
|
import { definePluginSettings, Settings } from "@api/Settings";
|
||||||
import ErrorBoundary from "@components/ErrorBoundary";
|
import ErrorBoundary from "@components/ErrorBoundary";
|
||||||
import { Devs, EquicordDevs } from "@utils/constants";
|
import { Devs, reverseExtensionMap } from "@utils/constants";
|
||||||
import definePlugin, { OptionType } from "@utils/types";
|
import definePlugin, { OptionType } from "@utils/types";
|
||||||
import { findByCodeLazy, findByPropsLazy } from "@webpack";
|
import { findByCodeLazy, findByPropsLazy } from "@webpack";
|
||||||
|
|
||||||
|
@ -42,11 +42,6 @@ const settings = definePluginSettings({
|
||||||
type: OptionType.BOOLEAN,
|
type: OptionType.BOOLEAN,
|
||||||
default: true,
|
default: true,
|
||||||
},
|
},
|
||||||
fixOpusExtensions: {
|
|
||||||
description: "Whether to fix file extensions by default",
|
|
||||||
type: OptionType.BOOLEAN,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
method: {
|
method: {
|
||||||
description: "Anonymising method",
|
description: "Anonymising method",
|
||||||
type: OptionType.SELECT,
|
type: OptionType.SELECT,
|
||||||
|
@ -72,7 +67,7 @@ const settings = definePluginSettings({
|
||||||
|
|
||||||
export default definePlugin({
|
export default definePlugin({
|
||||||
name: "AnonymiseFileNames",
|
name: "AnonymiseFileNames",
|
||||||
authors: [Devs.fawn, EquicordDevs.thororen],
|
authors: [Devs.fawn],
|
||||||
description: "Anonymise uploaded file names",
|
description: "Anonymise uploaded file names",
|
||||||
patches: [
|
patches: [
|
||||||
{
|
{
|
||||||
|
@ -125,17 +120,8 @@ export default definePlugin({
|
||||||
const tarMatch = tarExtMatcher.exec(file);
|
const tarMatch = tarExtMatcher.exec(file);
|
||||||
const extIdx = tarMatch?.index ?? file.lastIndexOf(".");
|
const extIdx = tarMatch?.index ?? file.lastIndexOf(".");
|
||||||
let ext = extIdx !== -1 ? file.slice(extIdx) : "";
|
let ext = extIdx !== -1 ? file.slice(extIdx) : "";
|
||||||
const matchList = [
|
if (Settings.plugins.FixFileExtensions.enabled) {
|
||||||
".ogv",
|
ext = reverseExtensionMap[ext];
|
||||||
".oga",
|
|
||||||
".ogx",
|
|
||||||
".ogm",
|
|
||||||
".spx",
|
|
||||||
".opus"
|
|
||||||
];
|
|
||||||
|
|
||||||
if (settings.store.fixOpusExtensions && ext !== "ogg" && matchList.includes(ext)) {
|
|
||||||
ext = ".ogg";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (settings.store.method) {
|
switch (settings.store.method) {
|
||||||
|
|
|
@ -31,6 +31,21 @@ export interface Dev {
|
||||||
badge?: boolean;
|
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.
|
* 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.
|
* This object is used for the plugin author list, as well as to add a contributor badge to your profile.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue