Refactor static routes

This commit is contained in:
Pig Fang 2019-12-22 17:28:55 +08:00
parent 591b9969aa
commit ae4be6fbda
9 changed files with 19 additions and 85 deletions

View File

@ -122,14 +122,9 @@ class TextureController extends Controller
return response()->file(storage_path('static_textures/avatar.png'));
}
public function avatarByTidWithSize($size, $tid)
public function avatar($uid, $size = 128)
{
return $this->avatarByTid($tid, $size);
}
public function avatar($base64_email, $size = 128)
{
$user = User::where('email', base64_decode($base64_email))->first();
$user = User::find($uid);
if ($user) {
return $this->avatarByTid($user->avatar, $size);
@ -138,11 +133,6 @@ class TextureController extends Controller
return response()->file(storage_path('static_textures/avatar.png'));
}
public function avatarWithSize($size, $base64_email)
{
return $this->avatar($base64_email, $size);
}
public function preview($tid, $size = 250)
{
if ($t = Texture::find($tid)) {
@ -177,11 +167,6 @@ class TextureController extends Controller
return response()->file(storage_path('static_textures/broken.png'));
}
public function previewWithSize($size, $tid)
{
return $this->preview($tid, $size);
}
public function raw($tid)
{
abort_unless(option('allow_downloading_texture'), 404);

View File

@ -26,7 +26,7 @@ class UserMenuComposer
$email = base64_encode($user->email);
$avatar = $this->filter->apply(
'user_avatar',
url('avatar/25/'.$email.'.png?tid='.$user->avatar),
url('/avatar/user/'.$user->uid.'/25'),
[$user]
);

View File

@ -25,7 +25,7 @@ class UserPanelComposer
$user = auth()->user();
$avatar = $this->filter->apply(
'user_avatar',
url('avatar/45/'.base64_encode($user->email).'.png?tid='.$user->avatar),
url('/avatar/user/'.$user->uid.'/45'),
[$user]
);

View File

@ -92,7 +92,7 @@ export default {
}
},
avatarUrl(player) {
return `${blessing.base_url}/avatar/35/${player.tid_skin}`
return `${blessing.base_url}/avatar/${player.tid_skin}/35`
},
},
}

View File

@ -48,5 +48,5 @@ test('compute avatar URL', () => {
// eslint-disable-next-line camelcase
const wrapper = mount<Vue & { avatarUrl(player: { tid_skin: number }): string }>(ApplyToPlayerDialog)
const { avatarUrl } = wrapper.vm
expect(avatarUrl({ tid_skin: 1 })).toBe('/avatar/35/1')
expect(avatarUrl({ tid_skin: 1 })).toBe('/avatar/1/35')
})

View File

@ -210,7 +210,7 @@ test('apply texture', async () => {
button.trigger('click')
await flushPromises()
expect(wrapper.find('input[type="radio"]').attributes('value')).toBe('1')
expect(wrapper.find('.model-label > img').attributes('src')).toBe('/avatar/35/10')
expect(wrapper.find('.model-label > img').attributes('src')).toBe('/avatar/10/35')
expect(wrapper.find('.modal-body').text()).toContain('name')
jest.runAllTimers()
})

View File

@ -1,19 +1,5 @@
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
/**
* Resources.
*/
Route::group(['middleware' => 'player'], function () {
// Json profile
Route::get('/{player_name}.json', 'TextureController@json');
@ -27,12 +13,9 @@ Route::get('/textures/{hash}', 'TextureController@texture');
Route::get('/{api}/textures/{hash}', 'TextureController@textureWithApi')->where('api', 'usm|csl');
Route::get('/avatar/player/{size}/{name}.png', 'TextureController@avatarByPlayer');
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('/avatar/user/{uid}/{size?}', 'TextureController@avatar');
Route::get('/avatar/{tid}/{size?}', 'TextureController@avatarByTid');
Route::get('/raw/{tid}.png', 'TextureController@raw');
Route::get('/preview/{tid}.png', 'TextureController@preview');
Route::get('/preview/{size}/{tid}.png', 'TextureController@previewWithSize');
Route::get('/preview/{tid}/{size?}', 'TextureController@preview');

View File

@ -190,19 +190,12 @@ class TextureControllerTest extends TestCase
$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()
{
Event::fake();
Storage::fake('textures');
$base64_email = base64_encode('a@b.c');
$this->get("/avatar/$base64_email.png")
->assertHeader('Content-Type', 'image/png');
$this->get("/avatar/5.png")->assertHeader('Content-Type', 'image/png');
$steve = factory(Texture::class)->create();
$png = base64_decode(\App\Http\Controllers\TextureController::getDefaultSteveSkin());
@ -215,36 +208,15 @@ class TextureControllerTest extends TestCase
->once()
->andReturn(imagecreatefromstring($png));
$this->get('/avatar/'.base64_encode($user->email).'.png')
$this->get('/avatar/user/'.$user->uid)
->assertHeader('Content-Type', 'image/png');
Event::assertDispatched(\App\Events\GetAvatarPreview::class);
Storage::shouldReceive('disk')->with('textures')->andThrow(new Exception());
$this->get('/avatar/'.base64_encode($user->email).'.png')
$this->get('/avatar/user/'.$user->uid.'/45')
->assertHeader('Content-Type', 'image/png');
}
public function testAvatarWithSize()
{
Event::fake();
Storage::fake('textures');
$steve = factory(Texture::class)->create();
$png = base64_decode(\App\Http\Controllers\TextureController::getDefaultSteveSkin());
Storage::disk('textures')->put($steve->hash, $png);
$user = factory(User::class)->create(['avatar' => $steve->tid]);
$mock = Mockery::mock('overload:Minecraft');
$mock->shouldReceive('generateAvatarFromSkin')
->once()
->andReturn(imagecreatefromstring($png));
$this->get('/avatar/50/'.base64_encode($user->email).'.png')
->assertHeader('Content-Type', 'image/png');
Event::fake(\App\Events\GetAvatarPreview::class);
}
public function testPreview()
{
Event::fake();
@ -253,10 +225,10 @@ class TextureControllerTest extends TestCase
$steve = factory(Texture::class)->create();
$cape = factory(Texture::class, 'cape')->create();
$this->get('/preview/0.png')
$this->get('/preview/0')
->assertHeader('Content-Type', 'image/png');
$this->get("/preview/{$steve->tid}.png")
$this->get("/preview/{$steve->tid}")
->assertHeader('Content-Type', 'image/png');
$png = base64_decode(\App\Http\Controllers\TextureController::getDefaultSteveSkin());
@ -267,24 +239,18 @@ class TextureControllerTest extends TestCase
$mock->shouldReceive('generatePreviewFromSkin')
->once()
->andReturn(imagecreatefromstring($png));
$this->get("/preview/{$steve->tid}.png")
$this->get("/preview/{$steve->tid}/56")
->assertHeader('Content-Type', 'image/png');
Event::fake(\App\Events\GetSkinPreview::class);
$mock->shouldReceive('generatePreviewFromCape')
->once()
->andReturn(imagecreatefromstring($png));
$this->get("/preview/{$cape->tid}.png")
$this->get("/preview/{$cape->tid}")
->assertHeader('Content-Type', 'image/png');
Storage::shouldReceive('disk')->with('textures')->andThrow(new Exception());
$this->get("/preview/{$steve->tid}.png")
->assertHeader('Content-Type', 'image/png');
}
public function testPreviewWithSize()
{
$this->get('/preview/200/0.png')
$this->get("/preview/{$steve->tid}")
->assertHeader('Content-Type', 'image/png');
}

View File

@ -50,7 +50,7 @@ class UserControllerTest extends TestCase
->assertSee((string) $user->score);
$filter->assertApplied('grid:user.index');
$filter->assertApplied('user_avatar', function ($url, $user) use ($uid) {
$this->assertTrue(Str::endsWith($url, 'tid=0'));
$this->assertTrue(Str::contains($url, '/avatar/user/'.$uid));
$this->assertEquals($uid, $user->uid);
return true;