Serve static assets via CDN if possible

This commit is contained in:
An Phan 2016-01-28 13:35:51 +08:00
parent f87a5c3f52
commit 24cb32ceea
6 changed files with 105 additions and 8 deletions

View file

@ -35,24 +35,41 @@ class Application extends IlluminateApplication
* This is a copycat of L5's Elixir, but catered to our directory structure.
*
* @param string $file
* @param string $manifestFile
*
* @return string
*/
public function rev($file)
public function rev($file, $manifestFile = null)
{
static $manifest = null;
$manifestFile = $manifestFile ?: $this->publicPath().'/public/build/rev-manifest.json';
if (is_null($manifest)) {
$manifest = json_decode(file_get_contents($this->publicPath().'/public/build/rev-manifest.json'), true);
$manifest = json_decode(file_get_contents($manifestFile), true);
}
if (isset($manifest[$file])) {
return "/public/build/{$manifest[$file]}";
return $this->staticUrl("public/build/{$manifest[$file]}");
}
throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
}
/**
* Get a URL for static file requests.
* If this installation of Koel has a CDN_URL configured, use it as the base.
* Otherwise, just use a relative '/'.
*
* @param string The additional resource name/path.
*
* @return string
*/
public function staticUrl($name = null)
{
return trim(env('CDN_URL'), '/ ').'/'.trim(ltrim($name, '/'));
}
/**
* Get the latest version number of Koel from Github.
*

View file

@ -33,7 +33,7 @@ class DataController extends Controller
'users' => auth()->user()->is_admin ? User::all() : [],
'currentUser' => auth()->user(),
'useLastfm' => env('LASTFM_API_KEY') && env('LASTFM_API_SECRET'),
'cdnUrl' => trim(env('CDN_URL'), '/ '),
'cdnUrl' => app()->staticUrl(),
'currentVersion' => Application::VERSION,
'latestVersion' => auth()->user()->is_admin ? app()->getLatestVersion() : Application::VERSION,
]);

View file

@ -9,10 +9,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<link rel="manifest" href="manifest.json" />
<link rel="icon" type="image/x-icon" href="public/img/favicon.ico" />
<link rel="icon" href="public/img/icon.png">
<link rel="apple-touch-icon" href="public/img/icon.png">
<link rel="manifest" href="{{ App::staticUrl('manifest.json') }}" />
<link rel="icon" type="image/x-icon" href="{{ App::staticUrl('public/img/favicon.ico') }}" />
<link rel="icon" href="{{ App::staticUrl('public/img/icon.png') }}">
<link rel="apple-touch-icon" href="{{ App::staticUrl('public/img/icon.png') }}">
<link rel="stylesheet" href="{{ App::rev('css/vendors.css') }}">
<link rel="stylesheet" href="{{ App::rev('css/app.css') }}">

47
tests/ApplicationTest.php Normal file
View file

@ -0,0 +1,47 @@
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
class ApplicationTest extends TestCase
{
public function testStaticUrlWithoutCDN()
{
putenv('CDN_URL');
$this->assertEquals(App::staticUrl(), '/');
$this->assertEquals(App::staticUrl('foo.css '), '/foo.css');
}
public function testStaticUrlWithCDN()
{
putenv('CDN_URL=http://cdn.bar');
$this->assertEquals(App::staticUrl(), 'http://cdn.bar/');
$this->assertEquals(App::staticUrl('foo.css '), 'http://cdn.bar/foo.css');
}
public function testRev()
{
putenv('CDN_URL');
$manifestFile = dirname(__FILE__) . '/blobs/rev-manifest.json';
$this->assertEquals(App::rev('foo.css', $manifestFile), '/public/build/foo00.css');
putenv('CDN_URL=http://cdn.bar');
$this->assertEquals(App::rev('bar.js', $manifestFile), 'http://cdn.bar/public/build/bar00.js');
}
public function testGetLatestVersion()
{
$mock = new MockHandler([
new Response(200, [], file_get_contents(dirname(__FILE__) . '/blobs/github-tags.json')),
]);
$client = new Client(['handler' => HandlerStack::create($mock)]);
$this->assertEquals('v1.1.2', App::getLatestVersion($client));
}
}

View file

@ -0,0 +1,29 @@
[
{
"name": "v1.1.2",
"zipball_url": "https://api.github.com/repos/phanan/koel/zipball/v1.1.2",
"tarball_url": "https://api.github.com/repos/phanan/koel/tarball/v1.1.2",
"commit": {
"sha": "e1279ba5c6b07fbddf65fa2300c6515512a87b17",
"url": "https://api.github.com/repos/phanan/koel/commits/e1279ba5c6b07fbddf65fa2300c6515512a87b17"
}
},
{
"name": "v1.1.1",
"zipball_url": "https://api.github.com/repos/phanan/koel/zipball/v1.1.1",
"tarball_url": "https://api.github.com/repos/phanan/koel/tarball/v1.1.1",
"commit": {
"sha": "3bbb4375e7d1ffbe852bdf9d9952dbdcaf2d6658",
"url": "https://api.github.com/repos/phanan/koel/commits/3bbb4375e7d1ffbe852bdf9d9952dbdcaf2d6658"
}
},
{
"name": "1.0.0-beta",
"zipball_url": "https://api.github.com/repos/phanan/koel/zipball/1.0.0-beta",
"tarball_url": "https://api.github.com/repos/phanan/koel/tarball/1.0.0-beta",
"commit": {
"sha": "e536ff6d35248eab627e0e41dfb7fd969a77c8be",
"url": "https://api.github.com/repos/phanan/koel/commits/e536ff6d35248eab627e0e41dfb7fd969a77c8be"
}
}
]

View file

@ -0,0 +1,4 @@
{
"foo.css": "foo00.css",
"bar.js": "bar00.js"
}