2023-02-28 00:14:23 -08:00
|
|
|
import { query } from '@/scripts/url';
|
2022-11-11 16:39:11 -08:00
|
|
|
import { url } from '@/config';
|
2023-02-03 20:38:51 -08:00
|
|
|
import { instance } from '@/instance';
|
2022-11-11 16:39:11 -08:00
|
|
|
|
2023-02-28 00:14:23 -08:00
|
|
|
export function getProxiedImageUrl(imageUrl: string, type?: 'preview', mustOrigin: boolean = false): string {
|
|
|
|
const localProxy = `${url}/proxy`;
|
|
|
|
|
|
|
|
if (imageUrl.startsWith(instance.mediaProxy + '/') || imageUrl.startsWith('/proxy/') || imageUrl.startsWith(localProxy + '/')) {
|
|
|
|
// もう既にproxyっぽそうだったらurlを取り出す
|
|
|
|
imageUrl = (new URL(imageUrl)).searchParams.get('url') ?? imageUrl;
|
2022-12-29 19:00:50 -08:00
|
|
|
}
|
|
|
|
|
2023-03-10 21:11:40 -08:00
|
|
|
return `${mustOrigin ? localProxy : instance.mediaProxy}/${
|
2023-03-12 00:31:52 -08:00
|
|
|
type === 'preview' ? 'preview.webp'
|
2023-03-10 21:11:40 -08:00
|
|
|
: 'image.webp'
|
|
|
|
}?${query({
|
2022-11-11 16:39:11 -08:00
|
|
|
url: imageUrl,
|
2022-12-08 00:16:50 -08:00
|
|
|
fallback: '1',
|
|
|
|
...(type ? { [type]: '1' } : {}),
|
2023-02-28 00:14:23 -08:00
|
|
|
...(mustOrigin ? { origin: '1' } : {}),
|
2022-11-11 16:39:11 -08:00
|
|
|
})}`;
|
|
|
|
}
|
|
|
|
|
2022-11-26 15:57:11 -08:00
|
|
|
export function getProxiedImageUrlNullable(imageUrl: string | null | undefined, type?: 'preview'): string | null {
|
2022-11-11 16:39:11 -08:00
|
|
|
if (imageUrl == null) return null;
|
2022-11-26 15:57:11 -08:00
|
|
|
return getProxiedImageUrl(imageUrl, type);
|
2022-11-11 16:39:11 -08:00
|
|
|
}
|
2022-12-29 19:00:50 -08:00
|
|
|
|
|
|
|
export function getStaticImageUrl(baseUrl: string): string {
|
|
|
|
const u = baseUrl.startsWith('http') ? new URL(baseUrl) : new URL(baseUrl, url);
|
|
|
|
|
2023-02-03 20:38:51 -08:00
|
|
|
if (u.href.startsWith(`${url}/emoji/`)) {
|
|
|
|
// もう既にemojiっぽそうだったらsearchParams付けるだけ
|
2022-12-29 19:00:50 -08:00
|
|
|
u.searchParams.set('static', '1');
|
|
|
|
return u.href;
|
|
|
|
}
|
|
|
|
|
2023-02-03 20:38:51 -08:00
|
|
|
if (u.href.startsWith(instance.mediaProxy + '/')) {
|
|
|
|
// もう既にproxyっぽそうだったらsearchParams付けるだけ
|
2022-12-29 19:00:50 -08:00
|
|
|
u.searchParams.set('static', '1');
|
|
|
|
return u.href;
|
|
|
|
}
|
|
|
|
|
2023-03-12 00:31:52 -08:00
|
|
|
return `${instance.mediaProxy}/static.webp?${query({
|
2022-12-29 19:00:50 -08:00
|
|
|
url: u.href,
|
|
|
|
static: '1',
|
|
|
|
})}`;
|
|
|
|
}
|