mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2025-03-07 15:16:40 +08:00
separate cache logic from core
This commit is contained in:
parent
2559f7907b
commit
5c042ae0ab
@ -4,7 +4,7 @@ namespace App\Events;
|
||||
|
||||
use Illuminate\Routing\Router;
|
||||
|
||||
class ConfigureWebRoutes extends Event
|
||||
class ConfigureRoutes extends Event
|
||||
{
|
||||
public $router;
|
||||
|
@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Storage;
|
||||
use App\Models\Texture;
|
||||
use App\Events\GetAvatarPreview;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class CacheAvatarPreview
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param GetAvatarPreview $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(GetAvatarPreview $event)
|
||||
{
|
||||
$tid = $event->texture->tid;
|
||||
$hash = $event->texture->hash;
|
||||
$size = $event->size;
|
||||
|
||||
$path = BASE_DIR."/storage/textures/$hash";
|
||||
|
||||
if (!Storage::disk('cache')->has("avatar/$tid-$size")) {
|
||||
$png = \Minecraft::generateAvatarFromSkin($path, $event->size);
|
||||
imagepng($png, BASE_DIR."/storage/cache/avatar/$tid-$size");
|
||||
imagedestroy($png);
|
||||
}
|
||||
|
||||
return \Response::png(Storage::disk('cache')->get("avatar/$tid-$size"));
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Storage;
|
||||
use App\Models\Player;
|
||||
use App\Events\CheckPlayerExists;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class CachePlayerExists
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param CheckPlayerExists $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(CheckPlayerExists $event)
|
||||
{
|
||||
$player_name = $event->player_name;
|
||||
|
||||
if ($player_name && !Storage::disk('cache')->has("notfound/$player_name")) {
|
||||
if (Player::where('player_name', $player_name)->get()->isEmpty()) {
|
||||
Storage::disk('cache')->put("notfound/$player_name", '');
|
||||
}
|
||||
} else {
|
||||
if (option('return_200_when_notfound') == "1") {
|
||||
return json([
|
||||
'player_name' => $player_name,
|
||||
'message' => 'Player Not Found.'
|
||||
]);
|
||||
} else {
|
||||
abort(404, trans('general.unexistent-player'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Storage;
|
||||
use App\Events\GetPlayerJson;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class CachePlayerJson
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param GetPlayerJson $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(GetPlayerJson $event)
|
||||
{
|
||||
$player = $event->player;
|
||||
$api_type = $event->api_type;
|
||||
|
||||
$filename = "json/{$player->pid}-{$api_type}";
|
||||
|
||||
if (!Storage::disk('cache')->has($filename)) {
|
||||
Storage::disk('cache')->put($filename, $player->generateJsonProfile($api_type));
|
||||
}
|
||||
|
||||
return Storage::disk('cache')->get($filename);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Storage;
|
||||
use App\Events\GetSkinPreview;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class CacheSkinPreview
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param GetSkinPreview $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(GetSkinPreview $event)
|
||||
{
|
||||
$tid = $event->texture->tid;
|
||||
$size = $event->size;
|
||||
|
||||
if (!Storage::disk('cache')->has("preview/$tid-$size")) {
|
||||
$filename = BASE_DIR."/storage/textures/{$event->texture->hash}";
|
||||
|
||||
if ($event->texture->type == "cape") {
|
||||
$png = \Minecraft::generatePreviewFromCape($filename, $event->size);
|
||||
imagepng($png, BASE_DIR."/storage/cache/preview/$tid-$size");
|
||||
imagedestroy($png);
|
||||
} else {
|
||||
$png = \Minecraft::generatePreviewFromSkin($filename, $event->size);
|
||||
imagepng($png, BASE_DIR."/storage/cache/preview/$tid-$size");
|
||||
imagedestroy($png);
|
||||
}
|
||||
}
|
||||
|
||||
return \Response::png(Storage::disk('cache')->get("preview/$tid-$size"), 200, [
|
||||
'Last-Modified' => Storage::disk('cache')->lastModified("preview/$tid-$size")
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Storage;
|
||||
use App\Events\PlayerWasAdded;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class FreshNotFoundCache
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param PlayerWasAdded $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(PlayerWasAdded $event)
|
||||
{
|
||||
$player_name = $event->player->player_name;
|
||||
|
||||
if ($player_name && Storage::disk('cache')->has("notfound/$player_name")) {
|
||||
Storage::disk('cache')->delete("notfound/$player_name", '');
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Storage;
|
||||
use App\Events\PlayerProfileUpdated;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class FreshPlayerJson
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param PlayerProfileUpdated $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(PlayerProfileUpdated $event)
|
||||
{
|
||||
$player = $event->player;
|
||||
|
||||
$files = [
|
||||
"json/{$player->pid}-0",
|
||||
"json/{$player->pid}-1"
|
||||
];
|
||||
|
||||
foreach ($files as $file) {
|
||||
if (Storage::disk('cache')->has($file)) {
|
||||
Storage::disk('cache')->delete($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -13,24 +13,7 @@ class EventServiceProvider extends ServiceProvider
|
||||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
'App\Events\GetSkinPreview' => [
|
||||
'App\Listeners\CacheSkinPreview',
|
||||
],
|
||||
'App\Events\GetAvatarPreview' => [
|
||||
'App\Listeners\CacheAvatarPreview',
|
||||
],
|
||||
'App\Events\GetPlayerJson' => [
|
||||
'App\Listeners\CachePlayerJson',
|
||||
],
|
||||
'App\Events\PlayerProfileUpdated' => [
|
||||
'App\Listeners\FreshPlayerJson',
|
||||
],
|
||||
'App\Events\CheckPlayerExists' => [
|
||||
'App\Listeners\CachePlayerExists',
|
||||
],
|
||||
'App\Events\PlayerWasAdded' => [
|
||||
'App\Listeners\FreshNotFoundCache',
|
||||
],
|
||||
//
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -3,7 +3,7 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Routing\Router;
|
||||
use App\Events\ConfigureWebRoutes;
|
||||
use App\Events\ConfigureRoutes;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
@ -42,7 +42,7 @@ class RouteServiceProvider extends ServiceProvider
|
||||
|
||||
$this->mapWebRoutes($router);
|
||||
|
||||
//
|
||||
event(new ConfigureRoutes($router));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,8 +61,6 @@ class RouteServiceProvider extends ServiceProvider
|
||||
], function ($router) {
|
||||
require app_path('Http/Routes/web.php');
|
||||
});
|
||||
|
||||
event(new ConfigureWebRoutes($router));
|
||||
}
|
||||
|
||||
/**
|
||||
|
60
app/Services/Hook.php
Normal file
60
app/Services/Hook.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Event;
|
||||
use Closure;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Events\ConfigureRoutes;
|
||||
use App\Events\ConfigureUserMenu;
|
||||
|
||||
class Hook
|
||||
{
|
||||
/**
|
||||
* Add an item to menu.
|
||||
*
|
||||
* @param string $category 'user' or 'admin'
|
||||
* @param int $position Where to insert the given item, start from 0.
|
||||
* @param array $menu e.g.
|
||||
* [
|
||||
* 'title' => 'Title', # will be translated by translator
|
||||
* 'link' => 'user/config', # route link
|
||||
* 'icon' => 'fa-book' # font-awesome icon
|
||||
* ]
|
||||
* @return void
|
||||
*/
|
||||
public static function addMenuItem($category, $position, array $menu)
|
||||
{
|
||||
Event::listen(ConfigureUserMenu::class, function ($event) use ($menu, $position)
|
||||
{
|
||||
$new = [];
|
||||
|
||||
$offset = 0;
|
||||
foreach ($event->menu['user'] as $item) {
|
||||
// push new menu items at the given position
|
||||
if ($offset == $position) {
|
||||
$new[] = $menu;
|
||||
}
|
||||
|
||||
$new[] = $item;
|
||||
$offset++;
|
||||
}
|
||||
|
||||
$event->menu['user'] = $new;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a route. A router instance will be passed to the given callback.
|
||||
*
|
||||
* @param Closure $callback
|
||||
*/
|
||||
public static function addRoute(Closure $callback)
|
||||
{
|
||||
Event::listen(ConfigureRoutes::class, function($event) use ($callback)
|
||||
{
|
||||
return call_user_func($callback, $event->router);
|
||||
});
|
||||
}
|
||||
}
|
@ -99,9 +99,6 @@
|
||||
|
||||
@section('script')
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
$('.box-body').css('min-height', $('.content-wrapper').height() - $('.content-header').outerHeight() - 120);
|
||||
});
|
||||
|
||||
function deletePlugin(name) {
|
||||
swal({
|
||||
|
3
storage/cache/avatar/.gitignore
vendored
3
storage/cache/avatar/.gitignore
vendored
@ -1,3 +0,0 @@
|
||||
*
|
||||
!public/
|
||||
!.gitignore
|
3
storage/cache/json/.gitignore
vendored
3
storage/cache/json/.gitignore
vendored
@ -1,3 +0,0 @@
|
||||
*
|
||||
!public/
|
||||
!.gitignore
|
3
storage/cache/notfound/.gitignore
vendored
3
storage/cache/notfound/.gitignore
vendored
@ -1,3 +0,0 @@
|
||||
*
|
||||
!public/
|
||||
!.gitignore
|
3
storage/cache/preview/.gitignore
vendored
3
storage/cache/preview/.gitignore
vendored
@ -1,3 +0,0 @@
|
||||
*
|
||||
!public/
|
||||
!.gitignore
|
Loading…
Reference in New Issue
Block a user