mirror of
https://github.com/the-programmers-hangout/Hermes.git
synced 2026-09-04 01:06:00 +02:00
move to b64 images
This commit is contained in:
@@ -17,6 +17,7 @@ class Emote extends Model
|
||||
protected $casts = [
|
||||
'emote_id' => 'string',
|
||||
'guild_id' => 'string',
|
||||
'image' => 'string',
|
||||
];
|
||||
|
||||
public function logs()
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Emote;
|
||||
use GuzzleHttp\Client;
|
||||
use Laracord\Services\Service;
|
||||
|
||||
class UpdateEmoteImages extends Service
|
||||
@@ -9,7 +11,7 @@ class UpdateEmoteImages extends Service
|
||||
/**
|
||||
* The service interval.
|
||||
*/
|
||||
protected int $interval = 600; // 10 minutes
|
||||
protected int $interval = 5; // 10 minutes
|
||||
|
||||
/**
|
||||
* Handle the service.
|
||||
@@ -22,8 +24,14 @@ class UpdateEmoteImages extends Service
|
||||
return;
|
||||
}
|
||||
|
||||
$client = new Client;
|
||||
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
@@ -32,11 +40,15 @@ class UpdateEmoteImages extends Service
|
||||
$url = "https://cdn.discordapp.com/emojis/{$emoji->id}.{$ext}";
|
||||
|
||||
try {
|
||||
$client = new \GuzzleHttp\Client;
|
||||
$response = $client->get($url);
|
||||
if ($response->getStatusCode() === 200) {
|
||||
$stream = $response->getBody()->detach();
|
||||
$emote->image = $stream;
|
||||
$imageBinary = (string) $response->getBody();
|
||||
|
||||
if ($imageBinary === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$emote->image = base64_encode($imageBinary);
|
||||
$emote->save();
|
||||
}
|
||||
} 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');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -6,7 +6,18 @@
|
||||
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';
|
||||
|
||||
if (str_starts_with($binary, 'GIF8')) {
|
||||
|
||||
Reference in New Issue
Block a user