2016-08-29 22:48:55 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
|
|
|
use Response;
|
2016-09-25 11:40:50 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2016-08-29 22:48:55 +08:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
|
|
|
class ResponseMacroServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Perform post-registration booting of services.
|
|
|
|
*
|
|
|
|
* @param ResponseFactory $factory
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
2018-08-05 16:38:46 +08:00
|
|
|
Response::macro('png', function ($src = '', $status = 200, $header = []) {
|
|
|
|
// Handle fucking cache control
|
2016-09-25 11:40:50 +08:00
|
|
|
$last_modified = Arr::pull($header, 'Last-Modified', time());
|
2018-08-05 16:38:46 +08:00
|
|
|
$if_modified_since = strtotime(request()->headers->get('If-Modified-Since'));
|
|
|
|
$if_none_match = strtotime(request()->headers->get('If-None-Match'));
|
2016-09-25 11:40:50 +08:00
|
|
|
$etag = md5($src);
|
|
|
|
|
2018-08-05 16:38:46 +08:00
|
|
|
// Return `304 Not Modified` if given `If-Modified-Since` header
|
|
|
|
// is newer than our `Last-Modified` time or the `Etag` matches.
|
|
|
|
if ($if_modified_since >= $last_modified || $if_none_match == $etag) {
|
2019-03-02 22:58:37 +08:00
|
|
|
$src = '';
|
2016-09-25 11:40:50 +08:00
|
|
|
$status = 304;
|
|
|
|
}
|
|
|
|
|
2018-08-05 16:38:46 +08:00
|
|
|
return Response::make($src, $status, array_merge([
|
2016-09-25 11:40:50 +08:00
|
|
|
'Content-type' => 'image/png',
|
2018-08-05 16:38:46 +08:00
|
|
|
'Last-Modified' => format_http_date($last_modified),
|
|
|
|
'Cache-Control' => 'public, max-age='.option('cache_expire_time'),
|
2019-03-02 22:58:37 +08:00
|
|
|
'Etag' => $etag,
|
2016-08-29 22:48:55 +08:00
|
|
|
], $header));
|
|
|
|
});
|
2016-09-04 15:35:12 +08:00
|
|
|
|
2018-08-05 16:38:46 +08:00
|
|
|
Response::macro('jsonProfile', function ($src = '', $status = 200, $header = []) {
|
|
|
|
$last_modified = Arr::pull($header, 'Last-Modified', time());
|
|
|
|
$if_modified_since = strtotime(request()->headers->get('If-Modified-Since'));
|
2016-10-30 11:56:59 +08:00
|
|
|
|
2018-08-05 16:38:46 +08:00
|
|
|
if ($if_modified_since && $if_modified_since >= $last_modified) {
|
2019-03-02 22:58:37 +08:00
|
|
|
$src = '';
|
2016-10-30 11:56:59 +08:00
|
|
|
$status = 304;
|
|
|
|
}
|
|
|
|
|
2016-09-28 23:26:19 +08:00
|
|
|
return Response::make($src, $status, array_merge([
|
|
|
|
'Content-type' => 'application/json',
|
2018-08-05 16:38:46 +08:00
|
|
|
'Cache-Control' => 'public, max-age='.option('cache_expire_time'),
|
|
|
|
'Last-Modified' => format_http_date($last_modified),
|
2016-09-28 23:26:19 +08:00
|
|
|
], $header));
|
2016-09-04 15:35:12 +08:00
|
|
|
});
|
2016-08-29 22:48:55 +08:00
|
|
|
}
|
|
|
|
}
|