Skip to content

Commit

Permalink
packages upgrade and remove image cache library
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinecraft committed Dec 7, 2024
1 parent 19b079e commit 826765f
Show file tree
Hide file tree
Showing 16 changed files with 7,295 additions and 8,350 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ RATELIMIT_API_PER_MINUTE=600
MARK_USER_VERIFYED_ON_ACCOUNT_LINK=true
DISABLE_PLAYER_UNLINKING=false
USE_USERNAME_FOR_SKINS=false
FETCH_AVATAR_FROM_URL_USING_CURL=false
PLAYER_FETCHER_CRON_INTERVAL=everyThirtyMinutes
FILESYSTEM_DISK=local
MEDIA_DISK=media
Expand Down
30 changes: 21 additions & 9 deletions app/Http/Controllers/PlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ public function getAvatarImage(Request $request, $uuid, $username = null, $textu
$size = $request->size ?? 100;

// If we got invalid uuid, and we are not using username for skins, return alex
if (! $useUsernameForSkins && $uuid === '00000000-0000-0000-0000-000000000000') {
if (!$useUsernameForSkins && $uuid === '00000000-0000-0000-0000-000000000000') {
$img = MinecraftSkinUtils::getDefaultSkinImage('avatar', $size);

return $img->response('jpg');
return $this->streamImage($img);
}

try {
Expand All @@ -158,7 +158,7 @@ public function getAvatarImage(Request $request, $uuid, $username = null, $textu
}
}

return $img->response('jpg');
return $this->streamImage($img);
}

public function getSkinImage(Request $request, $uuid, $username = null, $textureid = null)
Expand All @@ -168,10 +168,10 @@ public function getSkinImage(Request $request, $uuid, $username = null, $texture
$param = $useUsernameForSkins ? $username : $uuid;

// If we got invalid uuid, and we are not using username for skins, return alex
if (! $useUsernameForSkins && $uuid === '00000000-0000-0000-0000-000000000000') {
if (!$useUsernameForSkins && $uuid === '00000000-0000-0000-0000-000000000000') {
$img = MinecraftSkinUtils::getDefaultSkinImage('skin');

return $img->response('jpg');
return $this->streamImage($img, 'png');
}

try {
Expand All @@ -189,7 +189,7 @@ public function getSkinImage(Request $request, $uuid, $username = null, $texture
}
}

return $img->response('png');
return $this->streamImage($img, 'png');
}

public function getRenderImage(Request $request, $uuid, $username = null, $textureid = null)
Expand All @@ -200,10 +200,10 @@ public function getRenderImage(Request $request, $uuid, $username = null, $textu
$scale = $request->scale;

// If we got invalid uuid, and we are not using username for skins, return alex
if (! $useUsernameForSkins && $uuid === '00000000-0000-0000-0000-000000000000') {
if (!$useUsernameForSkins && $uuid === '00000000-0000-0000-0000-000000000000') {
$img = MinecraftSkinUtils::getDefaultSkinImage('render');

return $img->response('jpg');
return $this->streamImage($img, 'png');
}

try {
Expand All @@ -217,6 +217,18 @@ public function getRenderImage(Request $request, $uuid, $username = null, $textu
}
}

return $img->response('png');
return $this->streamImage($img, 'png');
}

private function streamImage($img, $imageType = 'jpeg')
{
$contentType = "image/{$imageType}";
return response()->stream(function () use ($img, $imageType) {
if ($imageType == 'png') {
echo $img->toPng();
} else {
echo $img->toJpeg();
}
}, 200, ['Content-Type' => $contentType]);
}
}
10 changes: 9 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\ServiceProvider;
use Laravel\Pulse\Facades\Pulse;
use DB;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -29,7 +30,9 @@ public function register()
*/
public function boot()
{
//
$this->configureCommands();

// Add User Info to Pulse
Pulse::users(function ($ids) {
return User::findMany($ids)->map(fn($user) => [
'id' => $user->id,
Expand All @@ -55,4 +58,9 @@ public function boot()
Cache::set('queue_last_processed', now());
});
}

private function configureCommands()
{
DB::prohibitDestructiveCommands($this->app->isProduction());
}
}
45 changes: 20 additions & 25 deletions app/Utils/Helpers/MinecraftSkinUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace App\Utils\Helpers;

use App\Services\MinecraftApiService;
use Intervention\Image\Laravel\Facades\Image;
use Http;
use Intervention\Image\Facades\Image;
use Cache;

class MinecraftSkinUtils
{
Expand All @@ -23,7 +24,7 @@ public static function getDefaultSkinImage($type, $size = null)
default:
throw new \Exception('Invalid type');
}
$image = Image::make($imagePath);
$image = Image::read($imagePath);
if ($size) {
$image->resize($size, $size);
}
Expand All @@ -33,8 +34,6 @@ public static function getDefaultSkinImage($type, $size = null)

public static function getSkinImageFromMinotar($type, $identifier, $size = null)
{
$fetchAvatarFromUrlUsingCurl = config('minetrax.fetch_avatar_from_url_using_curl');

switch ($type) {
case 'avatar':
$url = "https://minotar.net/avatar/$identifier";
Expand All @@ -51,17 +50,13 @@ public static function getSkinImageFromMinotar($type, $identifier, $size = null)
throw new \Exception('Invalid type');
}

$data = $fetchAvatarFromUrlUsingCurl ? Http::get($url)->body() : $url;

return Image::cache(function ($image) use ($data) {
return $image->make($data);
}, 60, true); // Cache lifetime is in minutes
$data = self::httpGetWithCache($url);
return Image::read($data);
}

public static function getSkinImageFromCrafatar($type, $identifier, $size = null)
{
$useUsernameForSkins = config('minetrax.use_username_for_skins');
$fetchAvatarFromUrlUsingCurl = config('minetrax.fetch_avatar_from_url_using_curl');

switch ($type) {
case 'avatar':
Expand All @@ -70,38 +65,33 @@ public static function getSkinImageFromCrafatar($type, $identifier, $size = null
} else {
$uuid = $identifier;
}
$url = 'https://crafatar.com/avatars/'.$uuid.'?size='.$size;
$url = 'https://crafatar.com/avatars/' . $uuid . '?size=' . $size;
break;
case 'skin':
if ($useUsernameForSkins) {
$uuid = MinecraftApiService::playerUsernameToUuid($identifier);
} else {
$uuid = $identifier;
}
$url = 'https://crafatar.com/skins/'.$uuid;
$url = 'https://crafatar.com/skins/' . $uuid;
break;
case 'render':
if ($useUsernameForSkins) {
$uuid = MinecraftApiService::playerUsernameToUuid($identifier);
} else {
$uuid = $identifier;
}
$url = 'https://crafatar.com/renders/body/'.$uuid.'?scale='.$size;
$url = 'https://crafatar.com/renders/body/' . $uuid . '?scale=' . $size;
default:
throw new \Exception('Invalid type');
}

$data = $fetchAvatarFromUrlUsingCurl ? Http::get($url)->body() : $url;

return Image::cache(function ($image) use ($data) {
return $image->make($data);
}, 60, true); // Cache lifetime is in minutes
$data = self::httpGetWithCache($url);
return Image::read($data);
}

public static function getSkinImageFromMcHeads($type, $identifier, $size = null)
{
$fetchAvatarFromUrlUsingCurl = config('minetrax.fetch_avatar_from_url_using_curl');

switch ($type) {
case 'avatar':
$url = "https://mc-heads.net/avatar/$identifier";
Expand All @@ -123,11 +113,8 @@ public static function getSkinImageFromMcHeads($type, $identifier, $size = null)
throw new \Exception('Invalid type');
}

$data = $fetchAvatarFromUrlUsingCurl ? Http::get($url)->body() : $url;

return Image::cache(function ($image) use ($data) {
return $image->make($data);
}, 60, true); // Cache lifetime is in minutes
$data = self::httpGetWithCache($url);
return Image::read($data);
}

public static function uploadSkinToMineSkin($file, $skinType): array
Expand All @@ -144,4 +131,12 @@ public static function uploadSkinToMineSkin($file, $skinType): array

return $response->json();
}

private static function httpGetWithCache($url)
{
$key = "imagecache::{$url}";
return Cache::store('file')->remember($key, 60, function () use ($url) {
return Http::get($url)->body();
});
}
}
83 changes: 40 additions & 43 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,63 @@
],
"license": "MIT",
"require": {
"php": ">=8.1",
"php": ">=8.2",
"ext-json": "*",
"ext-sockets": "*",
"ext-zip": "*",
"bensampo/laravel-enum": "^6.0",
"beyondcode/laravel-websockets": "^1.13",
"clue/socket-raw": "^1.6",
"cybercog/laravel-love": "^9.0",
"doctrine/dbal": "^3.6",
"geoip2/geoip2": "^2.12",
"cybercog/laravel-love": "^10.0",
"geoip2/geoip2": "^3.1",
"guzzlehttp/guzzle": "^7.4",
"inertiajs/inertia-laravel": "^0.6.0",
"intervention/imagecache": "^2.5",
"inertiajs/inertia-laravel": "^1.0",
"intervention/image-laravel": "^1.3",
"laravel-notification-channels/discord": "^1.6",
"laravel/framework": "^10.0",
"laravel/jetstream": "^4.0",
"laravel/framework": "^11.0",
"laravel/jetstream": "^5.0",
"laravel/pulse": "^1.0@beta",
"laravel/sanctum": "^3.3",
"laravel/sanctum": "^4.0",
"laravel/socialite": "^5.5",
"laravel/telescope": "^4.14",
"laravel/telescope": "^5.0",
"laravel/tinker": "^2.7",
"league/commonmark": "^2.3",
"league/flysystem-aws-s3-v3": "~3.0",
"league/flysystem-ftp": "^3.0",
"league/flysystem-sftp-v3": "^3.2",
"league/mime-type-detection": "^1.11",
"league/commonmark": "^2.5",
"league/flysystem-aws-s3-v3": "^3.29",
"league/flysystem-ftp": "^3.29",
"league/flysystem-sftp-v3": "^3.29",
"league/mime-type-detection": "^1.16",
"marvinlabs/laravel-discord-logger": "^1.4",
"nxp/math-executor": "^2.3",
"openai-php/laravel": "^0.6.0",
"predis/predis": "^2.0",
"pusher/pusher-php-server": "^7.0",
"socialiteproviders/discord": "^4.1",
"spatie/laravel-backup": "^8.3",
"spatie/laravel-medialibrary": "^10.9",
"spatie/laravel-permission": "^5.5",
"spatie/laravel-query-builder": "^5.2",
"spatie/laravel-ray": "^1.30",
"spatie/laravel-searchable": "^1.11",
"spatie/laravel-settings": "^3.2",
"spatie/once": "^3.1",
"openai-php/laravel": "^0.10.2",
"predis/predis": "^2.3",
"pusher/pusher-php-server": "^7.2",
"socialiteproviders/discord": "^4.2",
"spatie/laravel-backup": "^9.1",
"spatie/laravel-medialibrary": "^11.10",
"spatie/laravel-permission": "6.10",
"spatie/laravel-query-builder": "^6.2",
"spatie/laravel-ray": "^1.37",
"spatie/laravel-searchable": "^1.12",
"spatie/laravel-settings": "^3.4",
"spirit55555/php-minecraft": "^1.3",
"symfony/http-client": "^6.2",
"symfony/yaml": "^6.1",
"tanmuhittin/laravel-google-translate": "^2.1",
"symfony/http-client": "^7.0",
"symfony/yaml": "^7.0",
"tanmuhittin/laravel-google-translate": "^2.3",
"tightenco/ziggy": "^1.0",
"torann/geoip": "^3.0",
"xpaw/php-minecraft-query": "^4.0",
"xpaw/php-source-query-class": "^2.1"
"xpaw/php-minecraft-query": "^5.0",
"xpaw/php-source-query-class": "^4.0"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.7",
"barryvdh/laravel-ide-helper": "^2.9",
"nunomaduro/phpinsights": "dev-master",
"fakerphp/faker": "^1.9.1",
"laravel/pint": "^1.1",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^7.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"spatie/laravel-ignition": "^2.0"
"barryvdh/laravel-debugbar": "^3.14",
"barryvdh/laravel-ide-helper": "^3.2",
"nunomaduro/phpinsights": "^2.12",
"fakerphp/faker": "^1.24",
"laravel/pint": "^1.18",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.5",
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0",
"spatie/laravel-ignition": "^2.9"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit 826765f

Please sign in to comment.