2022-12-29 21:00:50 -06:00
|
|
|
import { query, appendQuery } from '@/scripts/url';
|
2022-11-11 18:39:11 -06:00
|
|
|
import { url } from '@/config';
|
2023-02-03 22:38:51 -06:00
|
|
|
import { instance } from '@/instance';
|
2022-11-11 18:39:11 -06:00
|
|
|
|
2022-11-26 17:57:11 -06:00
|
|
|
export function getProxiedImageUrl(imageUrl: string, type?: 'preview'): string {
|
2023-02-03 22:38:51 -06:00
|
|
|
if (imageUrl.startsWith(instance.mediaProxy + '/') || imageUrl.startsWith('/proxy/')) {
|
2022-12-29 21:00:50 -06:00
|
|
|
// もう既にproxyっぽそうだったらsearchParams付けるだけ
|
|
|
|
return appendQuery(imageUrl, query({
|
|
|
|
fallback: '1',
|
|
|
|
...(type ? { [type]: '1' } : {}),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-02-03 22:38:51 -06:00
|
|
|
return `${instance.mediaProxy}/image.webp?${query({
|
2022-11-11 18:39:11 -06:00
|
|
|
url: imageUrl,
|
2022-12-08 02:16:50 -06:00
|
|
|
fallback: '1',
|
|
|
|
...(type ? { [type]: '1' } : {}),
|
2022-11-11 18:39:11 -06:00
|
|
|
})}`;
|
|
|
|
}
|
|
|
|
|
2022-11-26 17:57:11 -06:00
|
|
|
export function getProxiedImageUrlNullable(imageUrl: string | null | undefined, type?: 'preview'): string | null {
|
2022-11-11 18:39:11 -06:00
|
|
|
if (imageUrl == null) return null;
|
2022-11-26 17:57:11 -06:00
|
|
|
return getProxiedImageUrl(imageUrl, type);
|
2022-11-11 18:39:11 -06:00
|
|
|
}
|
2022-12-29 21:00:50 -06:00
|
|
|
|
|
|
|
export function getStaticImageUrl(baseUrl: string): string {
|
|
|
|
const u = baseUrl.startsWith('http') ? new URL(baseUrl) : new URL(baseUrl, url);
|
|
|
|
|
2023-02-03 22:38:51 -06:00
|
|
|
if (u.href.startsWith(`${url}/emoji/`)) {
|
|
|
|
// もう既にemojiっぽそうだったらsearchParams付けるだけ
|
2022-12-29 21:00:50 -06:00
|
|
|
u.searchParams.set('static', '1');
|
|
|
|
return u.href;
|
|
|
|
}
|
|
|
|
|
2023-02-03 22:38:51 -06:00
|
|
|
if (u.href.startsWith(instance.mediaProxy + '/')) {
|
|
|
|
// もう既にproxyっぽそうだったらsearchParams付けるだけ
|
2022-12-29 21:00:50 -06:00
|
|
|
u.searchParams.set('static', '1');
|
|
|
|
return u.href;
|
|
|
|
}
|
|
|
|
|
2023-02-03 22:38:51 -06:00
|
|
|
return `${instance.mediaProxy}/static.webp?${query({
|
2022-12-29 21:00:50 -06:00
|
|
|
url: u.href,
|
|
|
|
static: '1',
|
|
|
|
})}`;
|
|
|
|
}
|