separate event listeners from app core

This commit is contained in:
printempw 2016-08-30 10:10:11 +08:00
parent fffb8fd441
commit 7ed653e876
8 changed files with 71 additions and 58 deletions

View File

@ -3,6 +3,7 @@
namespace App\Events;
use App\Events\Event;
use App\Models\Texture;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
@ -10,16 +11,19 @@ class GetSkinPreview extends Event
{
use SerializesModels;
public $texture = null;
public $texture;
public $size;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\App\Models\Texture $texture)
public function __construct(Texture $texture, $size)
{
$this->texture = $texture;
$this->size = $size;
}
/**

View File

@ -3,14 +3,15 @@
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Events\GetSkinPreview;
use App\Events\GetAvatarPreview;
use App\Models\User;
use App\Models\Player;
use App\Models\Texture;
use App\Exceptions\PrettyPageException;
use Storage;
use App\Events\GetAvatarPreview;
use App\Events\GetSkinPreview;
use App\Models\Texture;
use App\Models\Player;
use App\Models\User;
use Minecraft;
use Response;
use Storage;
use Option;
use Event;
use Http;
@ -96,9 +97,19 @@ class TextureController extends BaseController
if ($t = Texture::find($tid)) {
if (Storage::disk('textures')->has($t->hash)) {
Event::fire(new GetAvatarPreview($t, $size));
$responses = Event::fire(new GetAvatarPreview($t, $size));
return \Response::png(Storage::disk('cache')->get("avatar/$tid"));
if (isset($responses[0]) && $responses[0] instanceof \Symfony\Component\HttpFoundation\Response) {
return $responses[0];
} else {
$filename = BASE_DIR."/storage/textures/{$t->hash}";
$png = \Minecraft::generateAvatarFromSkin($filename, $size);
imagepng($png);
imagedestroy($png);
return Response::png();
}
}
}
}
@ -107,7 +118,7 @@ class TextureController extends BaseController
imagepng($png);
imagedestroy($png);
return \Response::png();
return Response::png();
}
public function avatarWithSize($size, $base64_email)
@ -121,10 +132,25 @@ class TextureController extends BaseController
if ($t = Texture::find($tid)) {
if (Storage::disk('textures')->has($t->hash)) {
$t->size = $size;
Event::fire(new GetSkinPreview($t));
$responses = Event::fire(new GetSkinPreview($t, $size));
return \Response::png(Storage::disk('cache')->get("preview/$tid"));
if (isset($responses[0]) && $responses[0] instanceof \Symfony\Component\HttpFoundation\Response) {
return $responses[0];
} else {
$filename = BASE_DIR."/storage/textures/{$t->hash}";
if ($t->type == "cape") {
$png = \Minecraft::generatePreviewFromCape($filename, $size);
imagepng($png);
imagedestroy($png);
} else {
$png = \Minecraft::generatePreviewFromSkin($filename, $size);
imagepng($png);
imagedestroy($png);
}
return Response::png();
}
}
}
@ -132,7 +158,7 @@ class TextureController extends BaseController
imagepng($png);
imagedestroy($png);
return \Response::png();
return Response::png();
}
public function previewWithSize($size, $tid)
@ -144,7 +170,7 @@ class TextureController extends BaseController
if ($t = Texture::find($tid)) {
if (Storage::disk('textures')->has($t->hash)) {
return response(Storage::disk('textures')->get($t->hash))->header('Content-Type', 'image/png');
return Response::png(Storage::disk('textures')->get($t->hash));
} else {
abort(404, '请求的材质文件已经被删除');
}

View File

@ -23,13 +23,11 @@ class CacheAvatarPreview
$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);
$png = \Minecraft::generateAvatarFromSkin($path, $event->size);
imagepng($png, BASE_DIR."/storage/cache/avatar/$tid");
imagedestroy($png);
}
return \Response::png(\Storage::disk('cache')->get("avatar/$tid"));
}
}

View File

@ -9,16 +9,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
class CachePlayerExists
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*

View File

@ -9,16 +9,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
class CachePlayerJson
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
@ -30,9 +20,12 @@ class CachePlayerJson
$player = $event->player;
$api_type = $event->api_type;
if (!Storage::disk('cache')->has("json/{$player->pid}-{$api_type}")) {
Storage::disk('cache')->put("json/{$player->pid}-{$api_type}", $player->generateJsonProfile($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);
}
}

View File

@ -23,14 +23,16 @@ class CacheSkinPreview
$filename = BASE_DIR."/storage/textures/{$event->texture->hash}";
if ($event->texture->type == "cape") {
$png = \Minecraft::generatePreviewFromCape($filename, $event->texture->size);
$png = \Minecraft::generatePreviewFromCape($filename, $event->size);
imagepng($png, BASE_DIR."/storage/cache/preview/$tid");
imagedestroy($png);
} else {
$png = \Minecraft::generatePreviewFromSkin($filename, $event->texture->size);
$png = \Minecraft::generatePreviewFromSkin($filename, $event->size);
imagepng($png, BASE_DIR."/storage/cache/preview/$tid");
imagedestroy($png);
}
}
return \Response::png(Storage::disk('cache')->get("preview/$tid"));
}
}

View File

@ -8,16 +8,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
class FreshNotFoundCache
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*

View File

@ -2,9 +2,9 @@
namespace App\Models;
use App\Exceptions\PrettyPageException;
use App\Events\PlayerProfileUpdated;
use App\Events\GetPlayerJson;
use App\Exceptions\PrettyPageException;
use Event;
use Utils;
use View;
@ -151,16 +151,26 @@ class Player
* @return string User profile in json format
*/
public function getJsonProfile($api_type) {
Event::fire(new GetPlayerJson($this, $api_type));
// Support both CustomSkinLoader API & UniSkinAPI
if ($api_type == self::CSL_API || $api_type == self::USM_API) {
return \Storage::disk('cache')->get("json/{$this->pid}-{$api_type}");
$responses = Event::fire(new GetPlayerJson($this, $api_type));
// if listeners return nothing
if (isset($responses[0]) && $responses[0] !== null) {
return $responses[0];
} else {
return $this->generateJsonProfile($api_type);
}
} else {
throw new PrettyPageException('不支持的 API_TYPE。', -1);
}
}
/**
* Generate player profile in json string
*
* @param int $api_type
* @return string
*/
public function generateJsonProfile($api_type)
{
$json[($api_type == self::CSL_API) ? 'username' : 'player_name'] = $this->player_name;