Skip to content

Commit

Permalink
chore: support html cdn proxy rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamerblue committed May 11, 2024
1 parent bdee5d8 commit cf036d4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .umirc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ console.log('Using build target:', buildTarget);
console.log('Using COMPETITION_SIDE:', process.env.COMPETITION_SIDE === '1');
process.env.CDN_URL && console.log('Using CDN_URL:', process.env.CDN_URL);
process.env.MEDIA_URL && console.log('Using MEDIA_URL:', process.env.MEDIA_URL);
process.env.CDN_RAW_URL_BEFORE_PROXY && console.log('Using CDN_RAW_URL_BEFORE_PROXY:', process.env.CDN_RAW_URL_BEFORE_PROXY);
process.env.CDN_PROXY && console.log('Using CDN_PROXY:', process.env.CDN_PROXY);

const publicPathPrefix = process.env.CDN_URL || '';

Expand Down Expand Up @@ -40,6 +42,8 @@ export default {
'process.env.COMPETITION_SIDE': process.env.COMPETITION_SIDE === '1' ? '1' : '',
'process.env.CDN_URL': process.env.CDN_URL || '',
'process.env.MEDIA_URL': process.env.MEDIA_URL || '',
'process.env.CDN_RAW_URL_BEFORE_PROXY': process.env.CDN_RAW_URL_BEFORE_PROXY || '',
'process.env.CDN_PROXY': process.env.CDN_PROXY || '',
'process.env.PUBLIC_PATH': process.env.NODE_ENV === 'production' ? usingPublicPath : '/',
'process.env.DATA_USING_GIT': process.env.DATA_USING_GIT === '1',
},
Expand Down
2 changes: 2 additions & 0 deletions src/components/ProblemContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { loadCustomFont, getCustomFontStyleForReact } from '@/utils/customFont';
import ReactPlayer from 'react-player/file';
import { userActiveEmitter, UserActiveEvents } from '@/events/userActive';
import { pickGenshinAudioUrlFromConf } from '@/utils/spGenshin';
import { replaceString } from '@/utils/misc';

export interface Props {
loading: boolean;
Expand Down Expand Up @@ -97,6 +98,7 @@ class ProblemContent extends React.Component<Props, State> {
if (res === '<p></p>') {
res = '';
}
process.env.CDN_PROXY && (res = replaceString(res, [process.env.CDN_RAW_URL_BEFORE_PROXY], process.env.CDN_PROXY));
return res;
};

Expand Down
3 changes: 2 additions & 1 deletion src/pages/competitions/$id/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import ProblemTitle from '@/components/ProblemTitle';
import { Howl } from 'howler';
import GeneralForm from '@/components/GeneralForm';
import constants from '@/configs/constants';
import { sleep } from '@/utils/misc';
import { sleep, replaceString } from '@/utils/misc';
import GenshinTable from '@/components/GenshinTable';

export interface Props extends ReduxProps, FormProps {
Expand Down Expand Up @@ -480,6 +480,7 @@ class CompetitionOverview extends React.Component<Props, State> {
if (res === '<p></p>') {
res = '';
}
process.env.CDN_PROXY && (res = replaceString(res, [process.env.CDN_RAW_URL_BEFORE_PROXY], process.env.CDN_PROXY));
return res;
};

Expand Down
11 changes: 11 additions & 0 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,14 @@ export function isCompetitionSide() {
export function randomlyPickOne<T>(arr: T[]): T {
return arr[Math.floor(Math.random() * arr.length)];
}

export function replaceString(str: string, sourceString: string[], targetString: string): string {
let result = str;
sourceString.forEach((sourcePath) => {
// 为了安全地替换字符串,需要对 sourcePath 进行转义,因为它可能包含正则表达式的特殊字符
const escapedSourcePath = sourcePath.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const regex = new RegExp(escapedSourcePath, 'g');
result = result.replace(regex, targetString);
});
return result;
}

0 comments on commit cf036d4

Please sign in to comment.