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

View File

@ -3,14 +3,15 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController; 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 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 Minecraft;
use Response;
use Storage;
use Option; use Option;
use Event; use Event;
use Http; use Http;
@ -96,9 +97,19 @@ class TextureController extends BaseController
if ($t = Texture::find($tid)) { if ($t = Texture::find($tid)) {
if (Storage::disk('textures')->has($t->hash)) { 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); imagepng($png);
imagedestroy($png); imagedestroy($png);
return \Response::png(); return Response::png();
} }
public function avatarWithSize($size, $base64_email) public function avatarWithSize($size, $base64_email)
@ -121,10 +132,25 @@ class TextureController extends BaseController
if ($t = Texture::find($tid)) { if ($t = Texture::find($tid)) {
if (Storage::disk('textures')->has($t->hash)) { if (Storage::disk('textures')->has($t->hash)) {
$t->size = $size; $responses = Event::fire(new GetSkinPreview($t, $size));
Event::fire(new GetSkinPreview($t));
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); imagepng($png);
imagedestroy($png); imagedestroy($png);
return \Response::png(); return Response::png();
} }
public function previewWithSize($size, $tid) public function previewWithSize($size, $tid)
@ -144,7 +170,7 @@ class TextureController extends BaseController
if ($t = Texture::find($tid)) { if ($t = Texture::find($tid)) {
if (Storage::disk('textures')->has($t->hash)) { 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 { } else {
abort(404, '请求的材质文件已经被删除'); abort(404, '请求的材质文件已经被删除');
} }

View File

@ -23,13 +23,11 @@ class CacheAvatarPreview
$path = BASE_DIR."/storage/textures/$hash"; $path = BASE_DIR."/storage/textures/$hash";
if (!\Storage::disk('cache')->has("avatar/$tid")) { if (!\Storage::disk('cache')->has("avatar/$tid")) {
$hash = Texture::find($tid)->hash; $png = \Minecraft::generateAvatarFromSkin($path, $event->size);
$filename = BASE_DIR."/storage/textures/$hash";
$png = \Minecraft::generateAvatarFromSkin($filename, $event->size);
imagepng($png, BASE_DIR."/storage/cache/avatar/$tid"); imagepng($png, BASE_DIR."/storage/cache/avatar/$tid");
imagedestroy($png); imagedestroy($png);
} }
return \Response::png(\Storage::disk('cache')->get("avatar/$tid"));
} }
} }

View File

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

View File

@ -9,16 +9,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
class CachePlayerJson class CachePlayerJson
{ {
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/** /**
* Handle the event. * Handle the event.
* *
@ -30,9 +20,12 @@ class CachePlayerJson
$player = $event->player; $player = $event->player;
$api_type = $event->api_type; $api_type = $event->api_type;
if (!Storage::disk('cache')->has("json/{$player->pid}-{$api_type}")) { $filename = "json/{$player->pid}-{$api_type}";
Storage::disk('cache')->put("json/{$player->pid}-{$api_type}", $player->generateJsonProfile($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}"; $filename = BASE_DIR."/storage/textures/{$event->texture->hash}";
if ($event->texture->type == "cape") { 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"); imagepng($png, BASE_DIR."/storage/cache/preview/$tid");
imagedestroy($png); imagedestroy($png);
} else { } else {
$png = \Minecraft::generatePreviewFromSkin($filename, $event->texture->size); $png = \Minecraft::generatePreviewFromSkin($filename, $event->size);
imagepng($png, BASE_DIR."/storage/cache/preview/$tid"); imagepng($png, BASE_DIR."/storage/cache/preview/$tid");
imagedestroy($png); imagedestroy($png);
} }
} }
return \Response::png(Storage::disk('cache')->get("preview/$tid"));
} }
} }

View File

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

View File

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