From 1d620e3ec0f0b548486d095473d2e346ad2b47b6 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 17 Nov 2022 11:26:02 +0000 Subject: [PATCH] Add placeholders for file embeds --- src/pages/files/[fileId].js | 34 +++++++++++++++++-------- src/utils/helpers/placeholderHelpers.js | 19 ++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 src/utils/helpers/placeholderHelpers.js diff --git a/src/pages/files/[fileId].js b/src/pages/files/[fileId].js index deb04a3..3fadbf3 100644 --- a/src/pages/files/[fileId].js +++ b/src/pages/files/[fileId].js @@ -1,6 +1,7 @@ import { NextSeo } from "next-seo"; import dynamic from "next/dynamic"; import { getFileInfo } from "src/utils/helpers/fileHelpers"; +import { replaceFilePlaceholders } from "src/utils/helpers/placeholderHelpers"; import { formatBytes } from "src/utils/helpers/stringHelpers"; import { downloadURI } from "src/utils/helpers/webUtils"; @@ -51,26 +52,39 @@ export default function File({ isValidFile, fileData }) { } } - let openGraph = { - title: isValidFile ? `${fileId}.${ext}` : "Unknown file", - }; + let openGraph = {}; + let embedColor; if (!isValidFile) { + openGraph.title = "Unknown file"; openGraph.description = "This file was not found, is this correct id?"; } - if (!isVideo && !isImage) { - openGraph.description = "Click to open and download this file"; - } - if (imageOrVideo) { - openGraph = Object.assign(openGraph, imageOrVideo); + if (isValidFile) { + if (!isVideo && !isImage) { + openGraph.description = "Click to open and download this file"; + } + 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 = ( ); diff --git a/src/utils/helpers/placeholderHelpers.js b/src/utils/helpers/placeholderHelpers.js new file mode 100644 index 0000000..70eb38d --- /dev/null +++ b/src/utils/helpers/placeholderHelpers.js @@ -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)); +}