move to b64 images

This commit is contained in:
itsAzaria
2026-02-20 14:14:13 +00:00
parent a628eea61f
commit 6cd3504d76
4 changed files with 66 additions and 6 deletions
+1
View File
@@ -17,6 +17,7 @@ class Emote extends Model
protected $casts = [ protected $casts = [
'emote_id' => 'string', 'emote_id' => 'string',
'guild_id' => 'string', 'guild_id' => 'string',
'image' => 'string',
]; ];
public function logs() public function logs()
+17 -5
View File
@@ -2,6 +2,8 @@
namespace App\Services; namespace App\Services;
use App\Models\Emote;
use GuzzleHttp\Client;
use Laracord\Services\Service; use Laracord\Services\Service;
class UpdateEmoteImages extends Service class UpdateEmoteImages extends Service
@@ -9,7 +11,7 @@ class UpdateEmoteImages extends Service
/** /**
* The service interval. * The service interval.
*/ */
protected int $interval = 600; // 10 minutes protected int $interval = 5; // 10 minutes
/** /**
* Handle the service. * Handle the service.
@@ -22,8 +24,14 @@ class UpdateEmoteImages extends Service
return; return;
} }
$client = new Client;
foreach ($guild->emojis as $emoji) { foreach ($guild->emojis as $emoji) {
$emote = \App\Models\Emote::where('emote_id', $emoji->id)->whereIn('type', ['ANIMATED', 'STATIC'])->whereNull('image')->first(); $emote = Emote::where('emote_id', $emoji->id)
->whereIn('type', ['ANIMATED', 'STATIC'])
->whereNull('image')
->first();
if (! $emote) { if (! $emote) {
continue; continue;
} }
@@ -32,11 +40,15 @@ class UpdateEmoteImages extends Service
$url = "https://cdn.discordapp.com/emojis/{$emoji->id}.{$ext}"; $url = "https://cdn.discordapp.com/emojis/{$emoji->id}.{$ext}";
try { try {
$client = new \GuzzleHttp\Client;
$response = $client->get($url); $response = $client->get($url);
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {
$stream = $response->getBody()->detach(); $imageBinary = (string) $response->getBody();
$emote->image = $stream;
if ($imageBinary === '') {
continue;
}
$emote->image = base64_encode($imageBinary);
$emote->save(); $emote->save();
} }
} catch (\Exception $e) { } catch (\Exception $e) {
@@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('emotes', function (Blueprint $table) {
$table->dropColumn('image');
});
Schema::table('emotes', function (Blueprint $table) {
$table->text('image')->nullable()->after('type');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('emotes', function (Blueprint $table) {
$table->dropColumn('image');
});
Schema::table('emotes', function (Blueprint $table) {
$table->binary('image')->nullable()->after('type');
});
}
};
+12 -1
View File
@@ -6,7 +6,18 @@
return $placeholderImage; return $placeholderImage;
} }
$binary = (string) $image; $value = (string) $image;
if (str_starts_with($value, 'data:image/')) {
return $value;
}
$binary = base64_decode($value, true);
if ($binary === false || $binary === '') {
return $placeholderImage;
}
$mime = 'image/png'; $mime = 'image/png';
if (str_starts_with($binary, 'GIF8')) { if (str_starts_with($binary, 'GIF8')) {