add caching headers

This commit is contained in:
printempw 2016-09-25 11:40:50 +08:00
parent d5bb4ed6bf
commit 084e04c30a
4 changed files with 38 additions and 30 deletions

View File

@ -47,7 +47,11 @@ class TextureController extends Controller
public function texture($hash) { public function texture($hash) {
if (Storage::disk('textures')->has($hash)) { if (Storage::disk('textures')->has($hash)) {
return Response::png(Storage::disk('textures')->get($hash)); return Response::png(Storage::disk('textures')->get($hash), 200, [
'Last-Modified' => Storage::disk('textures')->lastModified($hash),
'Accept-Ranges' => 'bytes',
'Content-Length' => Storage::disk('textures')->size($hash),
]);
} else { } else {
abort(404); abort(404);
} }
@ -64,12 +68,10 @@ class TextureController extends Controller
if ($player->is_banned) if ($player->is_banned)
abort(404, '该角色拥有者已被本站封禁。'); abort(404, '该角色拥有者已被本站封禁。');
if (!$this->checkCache($player_name)) { $model_preference = ($player->getPreference() == "default") ? "steve" : "alex";
$model_preference = ($player->getPreference() == "default") ? "steve" : "alex"; $model = ($model == "") ? $model_preference : $model;
$model = ($model == "") ? $model_preference : $model;
return $player->getBinaryTexture($model); return $player->getBinaryTexture($model);
}
} }
public function skinWithModel($model, $player_name) public function skinWithModel($model, $player_name)
@ -84,9 +86,7 @@ class TextureController extends Controller
if ($player->is_banned) if ($player->is_banned)
abort(404, '该角色拥有者已被本站封禁。'); abort(404, '该角色拥有者已被本站封禁。');
if (!$this->checkCache($player_name)) { return $player->getBinaryTexture('cape');
return $player->getBinaryTexture('cape');
}
} }
public function avatar($base64_email, $size = 128) public function avatar($base64_email, $size = 128)
@ -181,18 +181,4 @@ class TextureController extends Controller
} }
private function checkCache($player_name)
{
// Cache friendly
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : null;
if ($if_modified_since >= (new Player(0, $player_name))->getLastModified()) {
http_response_code(304);
return true;
} else {
return false;
}
}
} }

View File

@ -34,6 +34,8 @@ class CacheSkinPreview
} }
} }
return \Response::png(Storage::disk('cache')->get("preview/$tid-$size")); return \Response::png(Storage::disk('cache')->get("preview/$tid-$size"), 200, [
'Last-Modified' => Storage::disk('cache')->lastModified("preview/$tid-$size")
]);
} }
} }

View File

@ -5,6 +5,8 @@ namespace App\Models;
use View; use View;
use Event; use Event;
use Utils; use Utils;
use Storage;
use Response;
use App\Events\GetPlayerJson; use App\Events\GetPlayerJson;
use App\Events\PlayerWasDeleted; use App\Events\PlayerWasDeleted;
use App\Events\PlayerProfileUpdated; use App\Events\PlayerProfileUpdated;
@ -105,12 +107,13 @@ class Player
$hash = $this->getTexture($type); $hash = $this->getTexture($type);
$path = BASE_DIR."/storage/textures/".$hash; $path = BASE_DIR."/storage/textures/".$hash;
if (\Storage::disk('textures')->has($hash)) { if (Storage::disk('textures')->has($hash)) {
// Cache friendly // Cache friendly
return response(\Storage::disk('textures')->get($hash)) return Response::png(Storage::disk('textures')->get($hash), 200, [
->header('Content-Type', 'image/png') 'Last-Modified' => $this->getLastModified(),
->header('Last-Modified', gmdate('D, d M Y H:i:s', $this->getLastModified()).' GMT') 'Accept-Ranges' => 'bytes',
->header('Content-Length', filesize($path)); 'Content-Length' => Storage::disk('textures')->size($hash),
]);
} else { } else {
abort(404, '请求的贴图已被删除。'); abort(404, '请求的贴图已被删除。');
} }

View File

@ -3,6 +3,7 @@
namespace App\Providers; namespace App\Providers;
use Response; use Response;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class ResponseMacroServiceProvider extends ServiceProvider class ResponseMacroServiceProvider extends ServiceProvider
@ -16,10 +17,26 @@ class ResponseMacroServiceProvider extends ServiceProvider
public function boot() public function boot()
{ {
Response::macro('png', function ($src = "", $status = 200, $header = []) { Response::macro('png', function ($src = "", $status = 200, $header = []) {
$last_modified = Arr::pull($header, 'Last-Modified', time());
$etag = md5($src);
// Checking if the client is validating his cache and if it is current.
if ((strtotime(Arr::get($_SERVER, 'If-Modified-Since')) == $last_modified) ||
trim(Arr::get($_SERVER, 'HTTP_IF_NONE_MATCH')) == $etag
) {
// Client's cache IS current, so we just respond '304 Not Modified'.
$status = 304;
$src = "";
}
return Response::stream(function() use ($src, $status) { return Response::stream(function() use ($src, $status) {
echo $src; echo $src;
}, $status, array_merge([ }, $status, array_merge([
'Content-type' => 'image/png', 'Content-type' => 'image/png',
'Last-Modified' => gmdate('D, d M Y H:i:s', $last_modified).' GMT',
'Cache-Control' => 'public, max-age=31536000', // 365 days
'Expires' => gmdate('D, d M Y H:i:s', $last_modified + 31536000).' GMT',
'Etag' => $etag
], $header)); ], $header));
}); });