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 8 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
3 changes: 2 additions & 1 deletion app/Support/NftImageUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ private static function getAlchemyCdn(string $url, ?ImageSize $imageSize): ?stri

$size = 'w_'.$imageSize->width().',h_'.$imageSize->height();
$newPath = preg_replace('/(?<=\/)upload(?=\/)/', "upload/{$size}", $path);
$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, $newPath, $url);
$url = str_replace($path, $scaledPath, $url);
}

return $url;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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::table('nfts')
->whereRaw("extra_attributes->'images'->>'thumb' LIKE '%thumbnailv2%'")
->orWhereRaw("extra_attributes->'images'->>'small' LIKE '%thumbnailv2%'")
->orWhereRaw("extra_attributes->'images'->>'large' LIKE '%thumbnailv2%'")->get()->each(function ($nft) {
$extraAttributes = json_decode($nft->extra_attributes);
Copy link
Contributor

Choose a reason for hiding this comment

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

for this I meant an actual query that updates the values and doesn't rely on eloquent models. You rewrote it now, but it will contain the same amount of models as before as they basically all use that thumbnail


$extraAttributes->images->thumb = $this->replaceThumbnailsWithScaled($extraAttributes->images->thumb);
$extraAttributes->images->small = $this->replaceThumbnailsWithScaled($extraAttributes->images->small);
$extraAttributes->images->large = $this->replaceThumbnailsWithScaled($extraAttributes->images->large);

DB::table('nfts')
->where('id', $nft->id)
->update(['extra_attributes' => json_encode($extraAttributes)]);

});
}

public function replaceThumbnailsWithScaled(string $url): string
{
return preg_replace('/thumbnailv2/', 'scaled', $url);
}
};
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