/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import { definePluginSettings } from "@api/Settings";
import ErrorBoundary from "@components/ErrorBoundary";
import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types";
import { CloudUpload } from "@vencord/discord-types";
import { findByCodeLazy } from "@webpack";
import { useState } from "@webpack/common";
const ActionBarIcon = findByCodeLazy("Children.map", "isValidElement", "dangerous:");
const enum Methods {
Random,
Consistent,
Timestamp,
}
const ANONYMISE_UPLOAD_SYMBOL = Symbol("vcAnonymise");
const tarExtMatcher = /\.tar\.\w+$/;
const settings = definePluginSettings({
anonymiseByDefault: {
description: "Whether to anonymise file names by default",
type: OptionType.BOOLEAN,
default: true,
},
method: {
description: "Anonymising method",
type: OptionType.SELECT,
options: [
{ label: "Random Characters", value: Methods.Random, default: true },
{ label: "Consistent", value: Methods.Consistent },
{ label: "Timestamp", value: Methods.Timestamp },
],
},
randomisedLength: {
description: "Random characters length",
type: OptionType.NUMBER,
default: 10,
disabled: () => settings.store.method !== Methods.Random,
},
consistent: {
description: "Consistent filename",
type: OptionType.STRING,
default: "image",
disabled: () => settings.store.method !== Methods.Consistent,
},
});
export default definePlugin({
name: "AnonymiseFileNames",
authors: [Devs.fawn],
description: "Anonymise uploaded file names",
settings,
patches: [
{
find: "async uploadFiles(",
replacement: [
{
match: /async uploadFiles\((\i)\){/,
replace: "$&$1.forEach($self.anonymise);"
}
],
},
{
find: "#{intl::ATTACHMENT_UTILITIES_SPOILER}",
replacement: {
match: /(?<=children:\[)(?=.{10,80}tooltip:.{0,100}#{intl::ATTACHMENT_UTILITIES_SPOILER})/,
replace: "arguments[0].canEdit!==false?$self.AnonymiseUploadButton(arguments[0]):null,"
},
},
],
AnonymiseUploadButton: ErrorBoundary.wrap(({ upload }: { upload: CloudUpload; }) => {
const [anonymise, setAnonymise] = useState(upload[ANONYMISE_UPLOAD_SYMBOL] ?? settings.store.anonymiseByDefault);
function onToggleAnonymise() {
upload[ANONYMISE_UPLOAD_SYMBOL] = !anonymise;
setAnonymise(!anonymise);
}
return (
{anonymise
?
:
}
);
}, { noop: true }),
anonymise(upload: CloudUpload) {
if ((upload[ANONYMISE_UPLOAD_SYMBOL] ?? settings.store.anonymiseByDefault) === false) {
return;
}
const originalFileName = upload.filename;
const tarMatch = tarExtMatcher.exec(originalFileName);
const extIdx = tarMatch?.index ?? originalFileName.lastIndexOf(".");
const ext = extIdx !== -1 ? originalFileName.slice(extIdx) : "";
const newFilename = (() => {
switch (settings.store.method) {
case Methods.Random:
const chars = "ABCDEFabcdef0123456789";
return Array.from(
{ length: settings.store.randomisedLength },
() => chars[Math.floor(Math.random() * chars.length)]
).join("") + ext;
case Methods.Consistent:
return settings.store.consistent + ext;
case Methods.Timestamp:
return Date.now() + ext;
}
})();
upload.filename = newFilename;
}
});