2022-12-30 03:00:50 +00:00
|
|
|
import { query, appendQuery } from '@/scripts/url';
|
2022-11-12 00:39:11 +00:00
|
|
|
import { url } from '@/config';
|
|
|
|
|
2022-11-26 23:57:11 +00:00
|
|
|
export function getProxiedImageUrl(imageUrl: string, type?: 'preview'): string {
|
2022-12-30 03:00:50 +00:00
|
|
|
if (imageUrl.startsWith(`${url}/proxy/`) || imageUrl.startsWith('/proxy/')) {
|
|
|
|
// もう既にproxyっぽそうだったらsearchParams付けるだけ
|
|
|
|
return appendQuery(imageUrl, query({
|
|
|
|
fallback: '1',
|
|
|
|
...(type ? { [type]: '1' } : {}),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-11-12 00:39:11 +00:00
|
|
|
return `${url}/proxy/image.webp?${query({
|
|
|
|
url: imageUrl,
|
2022-12-08 08:16:50 +00:00
|
|
|
fallback: '1',
|
|
|
|
...(type ? { [type]: '1' } : {}),
|
2022-11-12 00:39:11 +00:00
|
|
|
})}`;
|
|
|
|
}
|
|
|
|
|
2022-11-26 23:57:11 +00:00
|
|
|
export function getProxiedImageUrlNullable(imageUrl: string | null | undefined, type?: 'preview'): string | null {
|
2022-11-12 00:39:11 +00:00
|
|
|
if (imageUrl == null) return null;
|
2022-11-26 23:57:11 +00:00
|
|
|
return getProxiedImageUrl(imageUrl, type);
|
2022-11-12 00:39:11 +00:00
|
|
|
}
|
2022-12-30 03:00:50 +00:00
|
|
|
|
|
|
|
export function getStaticImageUrl(baseUrl: string): string {
|
|
|
|
const u = baseUrl.startsWith('http') ? new URL(baseUrl) : new URL(baseUrl, url);
|
|
|
|
|
|
|
|
if (u.href.startsWith(`${url}/proxy/`)) {
|
|
|
|
// もう既にproxyっぽそうだったらsearchParams付けるだけ
|
|
|
|
u.searchParams.set('static', '1');
|
|
|
|
return u.href;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u.href.startsWith(`${url}/emoji/`)) {
|
|
|
|
// もう既にemojiっぽそうだったらsearchParams付けるだけ
|
|
|
|
u.searchParams.set('static', '1');
|
|
|
|
return u.href;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 拡張子がないとキャッシュしてくれないCDNがあるのでダミーの名前を指定する
|
|
|
|
const dummy = `${encodeURIComponent(`${u.host}${u.pathname}`)}.webp`;
|
|
|
|
|
|
|
|
return `${url}/proxy/${dummy}?${query({
|
|
|
|
url: u.href,
|
|
|
|
static: '1',
|
|
|
|
})}`;
|
|
|
|
}
|