2023-07-27 08:31:52 +03:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-09-19 10:37:43 +03:00
|
|
|
import { query } from '@/scripts/url.js';
|
|
|
|
import { url } from '@/config.js';
|
|
|
|
import { instance } from '@/instance.js';
|
2022-11-12 02:39:11 +02:00
|
|
|
|
2023-07-27 08:31:52 +03:00
|
|
|
export function getProxiedImageUrl(imageUrl: string, type?: 'preview' | 'emoji' | 'avatar', mustOrigin = false, noFallback = false): string {
|
2023-02-28 10:14:23 +02:00
|
|
|
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-30 05:00:50 +02:00
|
|
|
}
|
|
|
|
|
2023-03-11 07:11:40 +02:00
|
|
|
return `${mustOrigin ? localProxy : instance.mediaProxy}/${
|
2023-03-12 10:31:52 +02:00
|
|
|
type === 'preview' ? 'preview.webp'
|
2023-03-11 07:11:40 +02:00
|
|
|
: 'image.webp'
|
|
|
|
}?${query({
|
2022-11-12 02:39:11 +02:00
|
|
|
url: imageUrl,
|
2023-04-12 04:58:56 +03:00
|
|
|
...(!noFallback ? { 'fallback': '1' } : {}),
|
2022-12-08 10:16:50 +02:00
|
|
|
...(type ? { [type]: '1' } : {}),
|
2023-02-28 10:14:23 +02:00
|
|
|
...(mustOrigin ? { origin: '1' } : {}),
|
2022-11-12 02:39:11 +02:00
|
|
|
})}`;
|
|
|
|
}
|
|
|
|
|
2022-11-27 01:57:11 +02:00
|
|
|
export function getProxiedImageUrlNullable(imageUrl: string | null | undefined, type?: 'preview'): string | null {
|
2022-11-12 02:39:11 +02:00
|
|
|
if (imageUrl == null) return null;
|
2022-11-27 01:57:11 +02:00
|
|
|
return getProxiedImageUrl(imageUrl, type);
|
2022-11-12 02:39:11 +02:00
|
|
|
}
|
2022-12-30 05:00:50 +02:00
|
|
|
|
|
|
|
export function getStaticImageUrl(baseUrl: string): string {
|
|
|
|
const u = baseUrl.startsWith('http') ? new URL(baseUrl) : new URL(baseUrl, url);
|
|
|
|
|
2023-02-04 06:38:51 +02:00
|
|
|
if (u.href.startsWith(`${url}/emoji/`)) {
|
|
|
|
// もう既にemojiっぽそうだったらsearchParams付けるだけ
|
2022-12-30 05:00:50 +02:00
|
|
|
u.searchParams.set('static', '1');
|
|
|
|
return u.href;
|
|
|
|
}
|
|
|
|
|
2023-02-04 06:38:51 +02:00
|
|
|
if (u.href.startsWith(instance.mediaProxy + '/')) {
|
|
|
|
// もう既にproxyっぽそうだったらsearchParams付けるだけ
|
2022-12-30 05:00:50 +02:00
|
|
|
u.searchParams.set('static', '1');
|
|
|
|
return u.href;
|
|
|
|
}
|
|
|
|
|
2023-03-12 10:31:52 +02:00
|
|
|
return `${instance.mediaProxy}/static.webp?${query({
|
2022-12-30 05:00:50 +02:00
|
|
|
url: u.href,
|
|
|
|
static: '1',
|
|
|
|
})}`;
|
|
|
|
}
|