Support retrieving avatar by tid

This commit is contained in:
Pig Fang 2018-07-30 15:13:14 +08:00
parent 6c6cecc5bd
commit 6102d27530
3 changed files with 54 additions and 25 deletions

View File

@ -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()));

View File

@ -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');

View File

@ -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');