From 6102d27530fa54e828c8f169071d1b77b13fb55b Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Mon, 30 Jul 2018 15:13:14 +0800 Subject: [PATCH] Support retrieving avatar by tid --- app/Http/Controllers/TextureController.php | 67 ++++++++++++++-------- routes/static.php | 2 + tests/TextureControllerTest.php | 10 ++++ 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/TextureController.php b/app/Http/Controllers/TextureController.php index 9906b4e5..7868de64 100644 --- a/app/Http/Controllers/TextureController.php +++ b/app/Http/Controllers/TextureController.php @@ -117,6 +117,47 @@ class TextureController extends Controller } else { abort(404, trans('general.texture-not-uploaded', ['type' => $type])); } + } // @codeCoverageIgnore + + public function avatarByTid($tid, $size = 128) + { + if ($t = Texture::find($tid)) { + try { + if (Storage::disk('textures')->has($t->hash)) { + $responses = event(new GetAvatarPreview($t, $size)); + + if (isset($responses[0]) && $responses[0] instanceof SymfonyResponse) { + return $responses[0]; // @codeCoverageIgnore + } else { + $png = Minecraft::generateAvatarFromSkin(Storage::disk('textures')->read($t->hash), $size); + + ob_start(); + imagepng($png); + imagedestroy($png); + $image = ob_get_contents(); + ob_end_clean(); + + return Response::png($image); + } + } + } catch (Exception $e) { + report($e); + } + } + + $png = imagecreatefromstring(base64_decode(static::getDefaultAvatar())); + ob_start(); + imagepng($png); + imagedestroy($png); + $image = ob_get_contents(); + ob_end_clean(); + + return Response::png($image); + } + + public function avatarByTidWithSize($size, $tid) + { + return $this->avatarByTid($tid, $size); } public function avatar($base64_email, UserRepository $users, $size = 128) @@ -124,31 +165,7 @@ class TextureController extends Controller $user = $users->get(base64_decode($base64_email), 'email'); if ($user) { - $tid = $user->getAvatarId(); - - if ($t = Texture::find($tid)) { - try { - if (Storage::disk('textures')->has($t->hash)) { - $responses = event(new GetAvatarPreview($t, $size)); - - if (isset($responses[0]) && $responses[0] instanceof SymfonyResponse) { - return $responses[0]; // @codeCoverageIgnore - } else { - $png = Minecraft::generateAvatarFromSkin(Storage::disk('textures')->read($t->hash), $size); - - ob_start(); - imagepng($png); - imagedestroy($png); - $image = ob_get_contents(); - ob_end_clean(); - - return Response::png($image); - } - } - } catch (Exception $e) { - report($e); - } - } + return $this->avatarByTid($user->getAvatarId()); } $png = imagecreatefromstring(base64_decode(static::getDefaultAvatar())); diff --git a/routes/static.php b/routes/static.php index 47e7c12c..34a66b98 100644 --- a/routes/static.php +++ b/routes/static.php @@ -30,6 +30,8 @@ Route::get('/{api}/textures/{hash}', 'TextureController@textureWithAp Route::get('/avatar/{base64_email}.png', 'TextureController@avatar'); Route::get('/avatar/{size}/{base64_email}.png', 'TextureController@avatarWithSize'); +Route::get('/avatar/{tid}', 'TextureController@avatarByTid'); +Route::get('/avatar/{size}/{tid}', 'TextureController@avatarByTidWithSize'); Route::get('/raw/{tid}.png', 'TextureController@raw'); diff --git a/tests/TextureControllerTest.php b/tests/TextureControllerTest.php index 0b509435..61cfae10 100644 --- a/tests/TextureControllerTest.php +++ b/tests/TextureControllerTest.php @@ -161,6 +161,16 @@ class TextureControllerTest extends TestCase ->assertSee(trans('general.texture-deleted')); } + public function testAvatarByTid() + { + $this->get('/avatar/1')->assertHeader('Content-Type', 'image/png'); + } + + public function testAvatarByTidWithSize() + { + $this->get('/avatar/50/1')->assertHeader('Content-Type', 'image/png'); + } + public function testAvatar() { $base64_email = base64_encode('a@b.c');