Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use scaled endpoint for nft images #308

Merged
merged 23 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/Support/NftImageUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ private static function getAlchemyCdn(string $url, ?ImageSize $imageSize): ?stri
$size = 'w_'.$imageSize->width().',h_'.$imageSize->height();
$newPath = preg_replace('/(?<=\/)upload(?=\/)/', "upload/{$size}", $path);

$url = str_replace($path, $newPath, $url);
// Remove the height & replace thumbnailv2, to keep the proper aspect ratio when width is given.
// E.g `w_512,h_512/thumbnailv2` becomes `w_512/scaled`
$scaledPath = preg_replace('/,h_\d+\/thumbnailv2/', '/scaled', $newPath);

// Replace thumbnailv2 for those that don't have dimensions specified (if any).
$scaledPath = preg_replace('/thumbnailv2/', 'scaled', $newPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we update existing image urls by replacing it as such as well? if so, it would be good to add a migration that would update the existing NFTs so they are all using the new format


$url = str_replace($path, $scaledPath, $url);
}

return $url;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class() extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::select(get_query('migrations.nfts.update_thumbnailv2_to_scaled'));
}
};
23 changes: 23 additions & 0 deletions queries/migrations.nfts.update_thumbnailv2_to_scaled.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
UPDATE nfts
SET extra_attributes =
jsonb_set(
jsonb_set(
jsonb_set(
extra_attributes::jsonb,
'{images,thumb}',
to_jsonb(REGEXP_REPLACE(extra_attributes->'images'->>'thumb', ',h_\d+\/thumbnailv2', '/scaled')),
true
),
'{images,large}',
to_jsonb(REGEXP_REPLACE(extra_attributes->'images'->>'large', ',h_\d+\/thumbnailv2', '/scaled/')),
true
),
'{images,small}',
to_jsonb(REGEXP_REPLACE(extra_attributes->'images'->>'small', ',h_\d+\/thumbnailv2', '/scaled/')),
true
)::json

WHERE
extra_attributes->'images'->>'thumb' LIKE '%/thumbnailv2/%'
OR extra_attributes->'images'->>'large' LIKE '%/thumbnailv2/%'
OR extra_attributes->'images'->>'small' LIKE '%/thumbnailv2/%';
10 changes: 5 additions & 5 deletions tests/App/Support/NftImageUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
'OpenSea (no change)' => [null, 'https://i.seadn.io/gcs/files/5ff2b7fa5c94616f34a27e37eadfbfd1.png?w=96&auto=format', 'https://i.seadn.io/gcs/files/5ff2b7fa5c94616f34a27e37eadfbfd1.png?w=96&auto=format'],
'OpenSea (custom size)' => [ImageSize::Large, 'https://i.seadn.io/gcs/files/5ff2b7fa5c94616f34a27e37eadfbfd1.png?w=96&auto=format', 'https://i.seadn.io/gcs/files/5ff2b7fa5c94616f34a27e37eadfbfd1.png?w=512&auto=format'],

'Alchemy (no change)' => [null, 'https://res.cloudinary.com/alchemyapi/image/upload/thumbnailv2/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c', 'https://res.cloudinary.com/alchemyapi/image/upload/thumbnailv2/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c'],
'Alchemy (custom size)' => [ImageSize::Thumb, 'https://res.cloudinary.com/alchemyapi/image/upload/thumbnailv2/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c', 'https://res.cloudinary.com/alchemyapi/image/upload/w_96,h_96/thumbnailv2/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c'],
'Alchemy (no change)' => [null, 'https://res.cloudinary.com/alchemyapi/image/upload/scaled/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c', 'https://res.cloudinary.com/alchemyapi/image/upload/scaled/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c'],
'Alchemy (custom size)' => [ImageSize::Thumb, 'https://res.cloudinary.com/alchemyapi/image/upload/thumbnailv2/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c', 'https://res.cloudinary.com/alchemyapi/image/upload/w_96,h_96/scaled/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c'],

'Moralis (no change)' => [null, 'https://nft-preview-media.s3.us-east-1.amazonaws.com/evm/0x1/0xc54567b294d7ec7807529fbaec71d326543453c5/0xbecb6f250337775b7d85ef2720d80dfc2f4e0c3a94ee2486038fd5e8ac1b43d6/low.jpeg', 'https://nft-preview-media.s3.us-east-1.amazonaws.com/evm/0x1/0xc54567b294d7ec7807529fbaec71d326543453c5/0xbecb6f250337775b7d85ef2720d80dfc2f4e0c3a94ee2486038fd5e8ac1b43d6/low.jpeg'],
'Moralis (no extension, defaults to large)' => [null, 'https://nft-preview-media.s3.us-east-1.amazonaws.com/evm/0x1/0xc54567b294d7ec7807529fbaec71d326543453c5/0xbecb6f250337775b7d85ef2720d80dfc2f4e0c3a94ee2486038fd5e8ac1b43d6', 'https://nft-preview-media.s3.us-east-1.amazonaws.com/evm/0x1/0xc54567b294d7ec7807529fbaec71d326543453c5/0xbecb6f250337775b7d85ef2720d80dfc2f4e0c3a94ee2486038fd5e8ac1b43d6/high.jpeg'],
Expand All @@ -39,9 +39,9 @@
],
'Alchemy' => ['https://res.cloudinary.com/alchemyapi/image/upload/thumbnailv2/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c',
[
'thumb' => 'https://res.cloudinary.com/alchemyapi/image/upload/w_96,h_96/thumbnailv2/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c',
'small' => 'https://res.cloudinary.com/alchemyapi/image/upload/w_256,h_256/thumbnailv2/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c',
'large' => 'https://res.cloudinary.com/alchemyapi/image/upload/w_512,h_512/thumbnailv2/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c',
'thumb' => 'https://res.cloudinary.com/alchemyapi/image/upload/w_96,h_96/scaled/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c',
'small' => 'https://res.cloudinary.com/alchemyapi/image/upload/w_256,h_256/scaled/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c',
'large' => 'https://res.cloudinary.com/alchemyapi/image/upload/w_512,h_512/scaled/eth-mainnet/f065afd5cf799f4ab1252111f8f3d38c',
],
],
'Moralis' => ['https://nft-preview-media.s3.us-east-1.amazonaws.com/evm/0x1/0xc54567b294d7ec7807529fbaec71d326543453c5/0xbecb6f250337775b7d85ef2720d80dfc2f4e0c3a94ee2486038fd5e8ac1b43d6/low.jpeg',
Expand Down
Loading