fix previews and avatars cannot be indivdually cached

by image format

fix #212
This commit is contained in:
Pig Fang 2020-07-29 18:38:21 +08:00
parent f607ba8a41
commit 353db6f250
No known key found for this signature in database
GPG Key ID: A8198F548DADA9E2
5 changed files with 16 additions and 10 deletions

View File

@ -51,6 +51,7 @@ class TextureController extends Controller
$tid = $texture->tid;
$hash = $texture->hash;
$usePNG = $request->has('png') || !(imagetypes() & IMG_WEBP);
$format = $usePNG ? 'png' : 'webp';
$disk = Storage::disk('textures');
abort_if($disk->missing($hash), 404);
@ -58,7 +59,7 @@ class TextureController extends Controller
$height = (int) $request->query('height', 200);
$now = Carbon::now();
$response = Cache::remember(
'preview-t'.$tid,
'preview-t'.$tid."-$format",
option('enable_preview_cache') ? $now->addYear() : $now->addMinute(),
function () use ($minecraft, $disk, $texture, $hash, $height, $usePNG) {
$file = $disk->get($hash);
@ -136,6 +137,7 @@ class TextureController extends Controller
$size = (int) $request->query('size', 100);
$mode = $request->has('3d') ? '3d' : '2d';
$usePNG = $request->has('png') || !(imagetypes() & IMG_WEBP);
$format = $usePNG ? 'png' : 'webp';
$disk = Storage::disk('textures');
if (is_null($texture) || $disk->missing($texture->hash)) {
@ -147,7 +149,7 @@ class TextureController extends Controller
$hash = $texture->hash;
$now = Carbon::now();
$response = Cache::remember(
'avatar-'.$mode.'-t'.$texture->tid.'-s'.$size,
'avatar-'.$mode.'-t'.$texture->tid.'-s'.$size."-$format",
option('enable_avatar_cache') ? $now->addYear() : $now->addMinute(),
function () use ($minecraft, $disk, $hash, $size, $mode, $usePNG) {
$file = $disk->get($hash);

View File

@ -10,3 +10,4 @@
- Fixed duplicated route names.
- Fixed duplication of private textures.
- Fixed that previews and avatars cannot be indivdually cached by image format.

View File

@ -10,3 +10,4 @@
- 修复重复的路由命名
- 修复私有材质的重复问题
- 修复预览图和头像不能根据图像格式来单独缓存的问题

View File

@ -14,6 +14,8 @@ Route::prefix('avatar')->name('avatar.')->group(function () {
Route::get('{tid}', 'TextureController@avatarByTexture')->name('texture');
});
Route::get('preview/{texture}', 'TextureController@preview')
->middleware(Illuminate\Routing\Middleware\SubstituteBindings::class);
Route::get('preview/hash/{hash}', 'TextureController@previewByHash');
Route::prefix('preview')->name('preview.')->group(function () {
Route::get('{texture}', 'TextureController@preview')->name('texture')
->middleware(Illuminate\Routing\Middleware\SubstituteBindings::class);
Route::get('hash/{hash}', 'TextureController@previewByHash')->name('hash');
});

View File

@ -73,12 +73,12 @@ class TextureControllerTest extends TestCase
Cache::clear();
$this->get('/preview/'.$skin->tid.'?png')
->assertHeader('Content-Type', 'image/png');
$this->assertTrue(Cache::has('preview-t'.$skin->tid));
$this->assertTrue(Cache::has('preview-t'.$skin->tid.'-png'));
$cape = factory(Texture::class)->states('cape')->create();
$disk->put($cape->hash, '');
$this->get('/preview/'.$cape->tid.'?height=100')->assertHeader('Content-Type', 'image/webp');
$this->assertTrue(Cache::has('preview-t'.$cape->tid));
$this->assertTrue(Cache::has('preview-t'.$cape->tid.'-webp'));
}
public function testRaw()
@ -245,17 +245,17 @@ class TextureControllerTest extends TestCase
$image = Image::make($image);
$this->assertEquals(100, $image->width());
$this->assertEquals(100, $image->height());
$this->assertTrue(Cache::has('avatar-2d-t'.$texture->tid.'-s100'));
$this->assertTrue(Cache::has('avatar-2d-t'.$texture->tid.'-s100-png'));
$image = $this->get('/avatar/'.$texture->tid.'?size=50&png')->getContent();
$image = Image::make($image);
$this->assertEquals(50, $image->width());
$this->assertEquals(50, $image->height());
$this->assertTrue(Cache::has('avatar-2d-t'.$texture->tid.'-s50'));
$this->assertTrue(Cache::has('avatar-2d-t'.$texture->tid.'-s50-png'));
$this->get('/avatar/'.$texture->tid.'?3d')
->assertSuccessful()
->assertHeader('Content-Type', 'image/webp');
$this->assertTrue(Cache::has('avatar-3d-t'.$texture->tid.'-s100'));
$this->assertTrue(Cache::has('avatar-3d-t'.$texture->tid.'-s100-webp'));
}
}