add events and listeners for cache

This commit is contained in:
printempw 2016-08-29 14:25:24 +08:00
parent 35d71b331b
commit 3949ddd59c
11 changed files with 196 additions and 75 deletions

View File

@ -0,0 +1,37 @@
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class GetAvatarPreview extends Event
{
use SerializesModels;
public $texture;
public $size;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\App\Models\Texture $texture, $size)
{
$this->texture = $texture;
$this->size = $size;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class GetSkinPreview extends Event
{
use SerializesModels;
public $texture = null;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\App\Models\Texture $texture)
{
$this->texture = $texture;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,35 @@
<?php
namespace App\Listeners;
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;
$path = BASE_DIR."/storage/textures/$hash";
if (!\Storage::disk('cache')->has("avatar/$tid")) {
$hash = Texture::find($tid)->hash;
$filename = BASE_DIR."/storage/textures/$hash";
$png = \Minecraft::generateAvatarFromSkin($filename, $event->size);
imagepng($png, BASE_DIR."/storage/cache/avatar/$tid");
imagedestroy($png);
}
}
}

View File

@ -0,0 +1,36 @@
<?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;
if (!Storage::disk('cache')->has("preview/$tid")) {
$filename = BASE_DIR."/storage/textures/{$event->texture->hash}";
if ($event->texture->type == "cape") {
$png = \Minecraft::generatePreviewFromCape($filename, $event->texture->size);
imagepng($png, BASE_DIR."/storage/cache/preview/$tid");
imagedestroy($png);
} else {
$png = \Minecraft::generatePreviewFromSkin($filename, $event->texture->size);
imagepng($png, BASE_DIR."/storage/cache/preview/$tid");
imagedestroy($png);
}
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -13,8 +13,11 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
'App\Events\GetSkinPreview' => [
'App\Listeners\CacheSkinPreview',
],
'App\Events\GetAvatarPreview' => [
'App\Listeners\CacheAvatarPreview',
],
];

View File

@ -54,6 +54,12 @@ return [
'visibility' => 'public',
],
'cache' => [
'driver' => 'local',
'root' => storage_path('cache'),
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => 'your-key',

3
storage/cache/avatar/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*
!public/
!.gitignore

3
storage/cache/preview/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*
!public/
!.gitignore