Equicord/src/equicordplugins/fixFileExtensions/index.tsx

57 lines
2.1 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
*/
2025-12-06 00:49:53 -05:00
import { isPluginEnabled } from "@api/PluginManager";
import anonymiseFileNames, { tarExtMatcher } from "@plugins/anonymiseFileNames";
2025-09-08 21:48:40 -04:00
import { Devs } from "@utils/constants";
2024-10-28 21:59:06 -04:00
import definePlugin, { ReporterTestable } from "@utils/types";
2025-09-08 22:01:55 -04:00
import { CloudUpload } from "@vencord/discord-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", ".flac", ".aac", ".wma"],
"jpg": [".jpeg", ".jfif", ".jpe", ".jif", ".jfi", ".pjpeg", ".pjp", ".bmp", ".tiff", ".tif", ".webp"],
"svg": [".svgz", ".ai", ".eps"],
"mp4": [".m4v", ".m4r", ".m4p", ".avi", ".mkv", ".wmv", ".flv", ".3gp", ".webm"],
"m4a": [".m4b", ".aiff", ".wav"],
"mov": [".movie", ".qt", ".asf", ".rm", ".rmvb"],
"png": [".ico", ".cur"],
2025-02-14 02:46:00 -05:00
};
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",
2025-09-08 21:48:40 -04:00
authors: [Devs.thororen],
2024-10-27 02:07:41 -04:00
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 09:08:35 -04:00
find: "async uploadFiles(",
2025-06-04 20:46:38 -04:00
replacement: [
{
2025-06-17 09:08:35 -04:00
match: /async uploadFiles\((\i)\){/,
2025-06-04 22:09:30 -04:00
replace: "$&$1.forEach($self.fixExt);"
2025-06-04 20:46:38 -04:00
}
],
2025-12-06 00:49:53 -05:00
predicate: () => !isPluginEnabled(anonymiseFileNames.name),
2024-10-27 02:07:41 -04:00
},
],
2025-09-08 22:01:55 -04:00
fixExt(upload: CloudUpload) {
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;
upload.filename = fileName + newExt;
2024-10-27 02:07:41 -04:00
},
});