Fix timezone of Last-Modified header
This commit is contained in:
parent
3ac4a98ecb
commit
16dbc6cff0
|
|
@ -40,10 +40,8 @@ class TextureController extends Controller
|
||||||
$content = $player->getJsonProfile(Option::get('api_type'));
|
$content = $player->getJsonProfile(Option::get('api_type'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response::rawJson($content, 200, [
|
return Response::jsonProfile($content, 200, [
|
||||||
'Last-Modified' => Carbon::createFromTimestamp(
|
'Last-Modified' => strtotime($player->last_modified)
|
||||||
$player->getLastModified()
|
|
||||||
)->format('D, d M Y H:i:s \G\M\T')
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,20 +50,20 @@ class TextureController extends Controller
|
||||||
return $this->json($player_name, $api);
|
return $this->json($player_name, $api);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function texture($hash) {
|
public function texture($hash, $headers = [], $message = '') {
|
||||||
try {
|
try {
|
||||||
if (Storage::disk('textures')->has($hash)) {
|
if (Storage::disk('textures')->has($hash)) {
|
||||||
return Response::png(Storage::disk('textures')->get($hash), 200, [
|
return Response::png(Storage::disk('textures')->get($hash), 200, array_merge([
|
||||||
'Last-Modified' => Storage::disk('textures')->lastModified($hash),
|
'Last-Modified' => Storage::disk('textures')->lastModified($hash),
|
||||||
'Accept-Ranges' => 'bytes',
|
'Accept-Ranges' => 'bytes',
|
||||||
'Content-Length' => Storage::disk('textures')->size($hash),
|
'Content-Length' => Storage::disk('textures')->size($hash),
|
||||||
]);
|
], $headers));
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
report($e);
|
report($e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return abort(404);
|
return abort(404, $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function textureWithApi($api, $hash) {
|
public function textureWithApi($api, $hash) {
|
||||||
|
|
@ -104,16 +102,9 @@ class TextureController extends Controller
|
||||||
$player = $this->getPlayerInstance($player_name);
|
$player = $this->getPlayerInstance($player_name);
|
||||||
|
|
||||||
if ($hash = $player->getTexture($type)) {
|
if ($hash = $player->getTexture($type)) {
|
||||||
if (Storage::disk('textures')->has($hash)) {
|
return $this->texture($hash, [
|
||||||
// Cache friendly
|
'Last-Modified' => strtotime($player->last_modified)
|
||||||
return Response::png(Storage::disk('textures')->read($hash), 200, [
|
], trans('general.texture-deleted'));
|
||||||
'Last-Modified' => $player->getLastModified(),
|
|
||||||
'Accept-Ranges' => 'bytes',
|
|
||||||
'Content-Length' => Storage::disk('textures')->size($hash),
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
abort(404, trans('general.texture-deleted'));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
abort(404, trans('general.texture-not-uploaded', ['type' => $type]));
|
abort(404, trans('general.texture-not-uploaded', ['type' => $type]));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -249,7 +249,7 @@ class Player extends Model
|
||||||
$sec_model = ($model == 'default') ? 'slim' : 'default';
|
$sec_model = ($model == 'default') ? 'slim' : 'default';
|
||||||
|
|
||||||
if ($api_type == self::USM_API) {
|
if ($api_type == self::USM_API) {
|
||||||
$json['last_update'] = $this->getLastModified();
|
$json['last_update'] = strtotime($this->last_modified);
|
||||||
$json['model_preference'] = [$model, $sec_model];
|
$json['model_preference'] = [$model, $sec_model];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -275,14 +275,4 @@ class Player extends Model
|
||||||
$this->update(['last_modified' => Utils::getTimeFormatted()]);
|
$this->update(['last_modified' => Utils::getTimeFormatted()]);
|
||||||
return event(new PlayerProfileUpdated($this));
|
return event(new PlayerProfileUpdated($this));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get time of last modified.
|
|
||||||
*
|
|
||||||
* @return int|false
|
|
||||||
*/
|
|
||||||
public function getLastModified()
|
|
||||||
{
|
|
||||||
return strtotime($this['last_modified']);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,41 +16,42 @@ 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 = []) {
|
||||||
|
// Handle fucking cache control
|
||||||
$last_modified = Arr::pull($header, 'Last-Modified', time());
|
$last_modified = Arr::pull($header, 'Last-Modified', time());
|
||||||
|
$if_modified_since = strtotime(request()->headers->get('If-Modified-Since'));
|
||||||
|
$if_none_match = strtotime(request()->headers->get('If-None-Match'));
|
||||||
$etag = md5($src);
|
$etag = md5($src);
|
||||||
|
|
||||||
// Checking if the client is validating his cache and if it is current.
|
// Return `304 Not Modified` if given `If-Modified-Since` header
|
||||||
if ((strtotime(Arr::get($_SERVER, 'If-Modified-Since')) == $last_modified) ||
|
// is newer than our `Last-Modified` time or the `Etag` matches.
|
||||||
trim(Arr::get($_SERVER, 'HTTP_IF_NONE_MATCH')) == $etag
|
if ($if_modified_since >= $last_modified || $if_none_match == $etag) {
|
||||||
) {
|
$src = '';
|
||||||
// Client's cache IS current, so we just respond '304 Not Modified'.
|
|
||||||
$status = 304;
|
$status = 304;
|
||||||
$src = "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response::stream(function() use ($src, $status) {
|
return Response::make($src, $status, array_merge([
|
||||||
echo $src;
|
|
||||||
}, $status, array_merge([
|
|
||||||
'Content-type' => 'image/png',
|
'Content-type' => 'image/png',
|
||||||
'Last-Modified' => gmdate('D, d M Y H:i:s', $last_modified).' GMT',
|
'Last-Modified' => format_http_date($last_modified),
|
||||||
'Cache-Control' => 'public, max-age='.option('cache_expire_time'), // 365 days
|
'Cache-Control' => 'public, max-age='.option('cache_expire_time'),
|
||||||
'Expires' => gmdate('D, d M Y H:i:s', $last_modified + option('cache_expire_time')).' GMT',
|
'Expires' => format_http_date($last_modified + option('cache_expire_time')),
|
||||||
'Etag' => $etag
|
'Etag' => $etag
|
||||||
], $header));
|
], $header));
|
||||||
});
|
});
|
||||||
|
|
||||||
Response::macro('rawJson', function ($src = "", $status = 200, $header = []) {
|
Response::macro('jsonProfile', function ($src = '', $status = 200, $header = []) {
|
||||||
$last_modified = Arr::get($header, 'Last-Modified', time());
|
$last_modified = Arr::pull($header, 'Last-Modified', time());
|
||||||
|
$if_modified_since = strtotime(request()->headers->get('If-Modified-Since'));
|
||||||
|
|
||||||
if (strtotime(Arr::get($_SERVER, 'If-Modified-Since')) >= $last_modified) {
|
if ($if_modified_since && $if_modified_since >= $last_modified) {
|
||||||
|
$src = '';
|
||||||
$status = 304;
|
$status = 304;
|
||||||
$src = "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response::make($src, $status, array_merge([
|
return Response::make($src, $status, array_merge([
|
||||||
'Content-type' => 'application/json',
|
'Content-type' => 'application/json',
|
||||||
'Cache-Control' => 'public, max-age='.option('cache_expire_time') // 365 days
|
'Cache-Control' => 'public, max-age='.option('cache_expire_time'),
|
||||||
|
'Last-Modified' => format_http_date($last_modified),
|
||||||
], $header));
|
], $header));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user