feat: support nostr.build thumbnail url

This commit is contained in:
Shusui MOYATANI
2024-01-07 17:06:09 +09:00
parent b82b925053
commit a1ee72f662
2 changed files with 34 additions and 1 deletions

View File

@@ -10,4 +10,22 @@ describe('thumbnailUrl', () => {
const expected = 'https://i.imgur.com/uBf5Qtsl.webp';
assert.deepStrictEqual(actual, expected);
});
it('should return url for nostr.build', () => {
const actual = thumbnailUrl(
'https://nostr.build/i/2489ee648a4fef6943f4a7c88349477e78a91e28232246b801fe8ce86e64624e.png',
);
const expected =
'https://nostr.build/responsive/240p/i/2489ee648a4fef6943f4a7c88349477e78a91e28232246b801fe8ce86e64624e.png';
assert.deepStrictEqual(actual, expected);
});
it('should return url for image.nostr.build', () => {
const actual = thumbnailUrl(
'https://image.nostr.build/f56ee902307158c1ebbcb5ac00430dbf1425eac12d55e4277ebccbe54d09671b.jpg',
);
const expected =
'https://nostr.build/responsive/240p/i/f56ee902307158c1ebbcb5ac00430dbf1425eac12d55e4277ebccbe54d09671b.jpg';
assert.deepStrictEqual(actual, expected);
});
});

View File

@@ -33,7 +33,7 @@ export const thumbnailUrl = (urlString: string): string => {
const url = new URL(urlString);
// Imgur
if (url.host === 'i.imgur.com' || url.host === 'imgur.com') {
const match = url.pathname.match(/^\/([a-zA-Z0-9]+)\.(jpg|jpeg|png|gif)/);
const match = url.pathname.match(/^\/([a-zA-Z0-9]+)\.(jpeg|jpg|png|gif|webp|avif|apng)/);
if (match != null) {
const result = new URL(url);
const imageId = match[1];
@@ -52,6 +52,21 @@ export const thumbnailUrl = (urlString: string): string => {
return result.toString();
}
// nostr.build
// https://github.com/nostrbuild/nostr.build/blob/main/api/v2/routes_upload.php
if (url.host === 'nostr.build' || url.host === 'image.nostr.build') {
const result = new URL(url);
result.host = 'nostr.build';
if (url.pathname.startsWith('/i/')) {
result.pathname = `/responsive/240p${url.pathname}`;
} else if (url.pathname.match(/^\/[0-9a-zA-Z]+\.(jpeg|jpg|png|gif|webp|avif|apng)$/)) {
result.pathname = `/responsive/240p/i${url.pathname}`;
} else {
return urlString;
}
return result.toString();
}
return url.toString();
} catch {
return urlString;