From 74dc26c083446d24e8515d0605ed2552ede80a88 Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Thu, 6 Jun 2024 15:28:08 +0200 Subject: [PATCH] Serve plugin icons locally (#3768) Co-authored-by: Anbraten <6918444+anbraten@users.noreply.github.com> --- docs/plugins/woodpecker-plugins/src/index.ts | 22 ++++++++++++++++--- .../src/theme/WoodpeckerPlugin.tsx | 4 +++- .../src/theme/WoodpeckerPluginList.tsx | 4 +++- docs/plugins/woodpecker-plugins/src/types.ts | 1 + 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/docs/plugins/woodpecker-plugins/src/index.ts b/docs/plugins/woodpecker-plugins/src/index.ts index f8fd00c55..d39a90071 100644 --- a/docs/plugins/woodpecker-plugins/src/index.ts +++ b/docs/plugins/woodpecker-plugins/src/index.ts @@ -12,7 +12,7 @@ async function loadContent(): Promise { const plugins = ( await Promise.all( - pluginsIndex.plugins.map(async (i) => { + pluginsIndex.plugins.map(async (i): Promise => { let docsContent: string; try { const response = await axios(i.docs); @@ -29,7 +29,22 @@ async function loadContent(): Promise { return undefined; } - return { + let pluginIconDataUrl: string | undefined; + if (docsHeader.icon) { + try { + const response = await axios(docsHeader.icon, { + responseType: 'arraybuffer', + }); + pluginIconDataUrl = `data:${response.headers['content-type'].toString()};base64,${Buffer.from( + response.data, + 'binary', + ).toString('base64')}`; + } catch (e) { + console.error("Can't fetch plugin icon", docsHeader.icon, (e as AxiosError).message); + } + } + + return { name: docsHeader.name, url: docsHeader.url, icon: docsHeader.icon, @@ -40,7 +55,8 @@ async function loadContent(): Promise { containerImage: docsHeader.containerImage, containerImageUrl: docsHeader.containerImageUrl, verified: i.verified || false, - }; + iconDataUrl: pluginIconDataUrl, + } satisfies WoodpeckerPlugin; }), ) ).filter((plugin): plugin is WoodpeckerPlugin => plugin !== undefined); diff --git a/docs/plugins/woodpecker-plugins/src/theme/WoodpeckerPlugin.tsx b/docs/plugins/woodpecker-plugins/src/theme/WoodpeckerPlugin.tsx index d827458b7..a80cab9f0 100644 --- a/docs/plugins/woodpecker-plugins/src/theme/WoodpeckerPlugin.tsx +++ b/docs/plugins/woodpecker-plugins/src/theme/WoodpeckerPlugin.tsx @@ -60,7 +60,9 @@ export function WoodpeckerPlugin({ plugin }: { plugin: WoodpeckerPluginType }) {

{plugin.description}

-
{plugin.icon ? : IconPlugin(150)}
+
+ {plugin.iconDataUrl ? : IconPlugin(150)} +

diff --git a/docs/plugins/woodpecker-plugins/src/theme/WoodpeckerPluginList.tsx b/docs/plugins/woodpecker-plugins/src/theme/WoodpeckerPluginList.tsx index 403dce984..3fd3e37b3 100644 --- a/docs/plugins/woodpecker-plugins/src/theme/WoodpeckerPluginList.tsx +++ b/docs/plugins/woodpecker-plugins/src/theme/WoodpeckerPluginList.tsx @@ -11,7 +11,9 @@ function PluginPanel({ plugin }: { plugin: WoodpeckerPlugin }) { return (
-
{plugin.icon ? : IconPlugin()}
+
+ {plugin.iconDataUrl ? : IconPlugin()} +

{plugin.name}

{plugin.description}

diff --git a/docs/plugins/woodpecker-plugins/src/types.ts b/docs/plugins/woodpecker-plugins/src/types.ts index 1786427be..668fe186e 100644 --- a/docs/plugins/woodpecker-plugins/src/types.ts +++ b/docs/plugins/woodpecker-plugins/src/types.ts @@ -19,6 +19,7 @@ export type WoodpeckerPlugin = WoodpeckerPluginHeader & { name: string; docs: string; // body of the docs .md file verified: boolean; // we set verified to false when not explicitly set + iconDataUrl?: string; }; export type Content = {