Skip to content

Commit

Permalink
fix:tempalte detail api add cdn (#4428)
Browse files Browse the repository at this point in the history
Signed-off-by: jingyang <3161362058@qq.com>
  • Loading branch information
zjy365 authored Dec 17, 2023
1 parent 0af9b05 commit b9a7ed7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'fs';
import yaml from 'js-yaml';
import type { NextApiRequest, NextApiResponse } from 'next';
import path from 'path';
import { replaceRawWithCDN } from './listTemplate';

export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
Expand Down Expand Up @@ -60,6 +61,7 @@ export async function GetTemplateByName({
namespace: string;
templateName: string;
}) {
const cdnUrl = process.env.CDN_URL;
const TemplateEnvs = {
SEALOS_CLOUD_DOMAIN: process.env.SEALOS_CLOUD_DOMAIN || 'cloud.sealos.io',
SEALOS_CERT_SECRET_NAME: process.env.SEALOS_CERT_SECRET_NAME || 'wildcard-cert',
Expand All @@ -83,6 +85,11 @@ export async function GetTemplateByName({
message: 'Lack of kind template'
};
}
if (cdnUrl) {
templateYaml.spec.readme = replaceRawWithCDN(templateYaml.spec.readme, cdnUrl);
templateYaml.spec.icon = replaceRawWithCDN(templateYaml.spec.icon, cdnUrl);
}

const yamlList = yamlData.filter((item: any) => item.kind !== 'Template');
const dataSource = getTemplateDataSource(templateYaml, TemplateEnvs);

Expand Down
4 changes: 1 addition & 3 deletions frontend/providers/template/src/pages/api/listTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import fs from 'fs';
import type { NextApiRequest, NextApiResponse } from 'next';
import path from 'path';

function replaceRawWithCDN(url: string, cdnUrl: string) {
export function replaceRawWithCDN(url: string, cdnUrl: string) {
let parsedUrl = parseGithubUrl(url);
if (!parsedUrl || !cdnUrl) return url;

if (parsedUrl.hostname === 'raw.githubusercontent.com') {
const newUrl = `https://${cdnUrl}/gh/${parsedUrl.organization}/${parsedUrl.repository}@${parsedUrl.branch}/${parsedUrl.remainingPath}`;
return newUrl;
}

return url;
}

Expand Down

0 comments on commit b9a7ed7

Please sign in to comment.