Add placeholders for file embeds

This commit is contained in:
Lee 2022-11-17 11:26:02 +00:00
parent 51b542a8e9
commit 1d620e3ec0
No known key found for this signature in database
GPG Key ID: 6EA25896ECCB3121
2 changed files with 43 additions and 10 deletions

@ -1,6 +1,7 @@
import { NextSeo } from "next-seo"; import { NextSeo } from "next-seo";
import dynamic from "next/dynamic"; import dynamic from "next/dynamic";
import { getFileInfo } from "src/utils/helpers/fileHelpers"; import { getFileInfo } from "src/utils/helpers/fileHelpers";
import { replaceFilePlaceholders } from "src/utils/helpers/placeholderHelpers";
import { formatBytes } from "src/utils/helpers/stringHelpers"; import { formatBytes } from "src/utils/helpers/stringHelpers";
import { downloadURI } from "src/utils/helpers/webUtils"; import { downloadURI } from "src/utils/helpers/webUtils";
@ -51,26 +52,39 @@ export default function File({ isValidFile, fileData }) {
} }
} }
let openGraph = { let openGraph = {};
title: isValidFile ? `${fileId}.${ext}` : "Unknown file", let embedColor;
};
if (!isValidFile) { if (!isValidFile) {
openGraph.title = "Unknown file";
openGraph.description = "This file was not found, is this correct id?"; openGraph.description = "This file was not found, is this correct id?";
} }
if (!isVideo && !isImage) { if (isValidFile) {
openGraph.description = "Click to open and download this file"; if (!isVideo && !isImage) {
} openGraph.description = "Click to open and download this file";
if (imageOrVideo) { }
openGraph = Object.assign(openGraph, imageOrVideo); if (imageOrVideo) {
openGraph = Object.assign(openGraph, imageOrVideo);
}
const { embed, title, description, color } = uploader.discordEmbed;
if (!embed) {
openGraph.title = undefined;
openGraph.description = undefined;
} else {
openGraph.title = replaceFilePlaceholders(file, title);
openGraph.description = replaceFilePlaceholders(file, description || "");
embedColor = "#" + color;
}
} }
const metaData = ( const metaData = (
<NextSeo <NextSeo
title={isValidFile ? `${fileId}.${ext}` : "Invalid file"} title={openGraph.title}
noindex
openGraph={openGraph} openGraph={openGraph}
themeColor={embedColor ? embedColor : undefined}
twitter={{ twitter={{
cardType: isImage ? "summary_large_image" : isVideo ? "player" : null, cardType: isImage ? "summary_large_image" : isVideo ? "player" : null,
}} }}
noindex
/> />
); );

@ -0,0 +1,19 @@
import { formatBytes } from "./stringHelpers";
/**
* Formats the given string with the placeholders for the image
*
* @param {{}} file The file to replace the placeholders for
* @param {string} string The message to replace the placeholders for
* @returns The formatted string
*/
export function replaceFilePlaceholders(file, string) {
const { fileId, originalFileName, contentType, ext, size } = file;
return string
.replace("{id}", fileId)
.replace("{original-name}", originalFileName)
.replace("{content-type}", contentType)
.replace("{ext}", ext)
.replace("{size}", formatBytes(size));
}