mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-19 19:37:01 -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;
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue