Equicord/src/equicordplugins/fixFileExtensions/index.tsx

56 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-10-27 02:07:41 -04:00
/*
* 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 { tarExtMatcher } from "@plugins/anonymiseFileNames";
2024-10-27 01:27:27 -06:00
import { EquicordDevs } from "@utils/constants";
2024-10-28 21:59:06 -04:00
import definePlugin, { ReporterTestable } from "@utils/types";
2024-10-27 02:07:41 -04:00
2025-02-14 02:46:00 -05:00
const extensionMap = {
"ogg": [".ogv", ".oga", ".ogx", ".ogm", ".spx", ".opus"],
"jpg": [".jpg", ".jpeg", ".jfif", ".jpe", ".jif", ".jfi", ".pjpeg", ".pjp"],
"svg": [".svgz"],
"mp4": [".m4v", ".m4r", ".m4p"],
"m4a": [".m4b"],
"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>);
2024-10-27 19:17:25 -04:00
2024-10-27 02:07:41 -04:00
export default definePlugin({
name: "FixFileExtensions",
authors: [EquicordDevs.thororen],
description: "Fixes file extensions by renaming them to a compatible supported format if possible",
2024-10-28 21:59:06 -04:00
reporterTestable: ReporterTestable.None,
2024-10-27 02:07:41 -04:00
patches: [
2024-10-27 02:16:11 -04:00
// Taken from AnonymiseFileNames
2024-10-27 02:07:41 -04:00
{
2025-06-17 00:04:19 -04:00
find: "async uploadFilesSimple(",
2025-06-04 20:46:38 -04:00
replacement: [
{
match: /async uploadFilesSimple\((\i)\){/,
2025-06-04 22:09:30 -04:00
replace: "$&$1.forEach($self.fixExt);"
2025-06-04 20:46:38 -04:00
}
],
2025-04-08 09:09:21 -04:00
predicate: () => !Settings.plugins.AnonymiseFileNames.enabled,
2024-10-27 02:07:41 -04:00
},
],
2025-06-05 13:45:28 -04:00
fixExt(upload: Upload) {
2024-10-27 02:07:41 -04:00
const file = upload.filename;
const tarMatch = tarExtMatcher.exec(file);
const extIdx = tarMatch?.index ?? file.lastIndexOf(".");
2025-04-08 09:09:21 -04:00
const fileName = extIdx !== -1 ? file.substring(0, extIdx) : "";
2024-10-27 02:07:41 -04:00
const ext = extIdx !== -1 ? file.slice(extIdx) : "";
const newExt = reverseExtensionMap[ext] || ext;
return fileName + newExt;
},
});