diff --git a/app/Application.php b/app/Application.php index b8d6c232..8c6df1dd 100644 --- a/app/Application.php +++ b/app/Application.php @@ -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. * diff --git a/app/Http/Controllers/API/DataController.php b/app/Http/Controllers/API/DataController.php index 23cda522..5c05bbc2 100644 --- a/app/Http/Controllers/API/DataController.php +++ b/app/Http/Controllers/API/DataController.php @@ -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, ]); diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index 573f1a17..e36c89c4 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -9,10 +9,10 @@ - - - - + + + + diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php new file mode 100644 index 00000000..715be208 --- /dev/null +++ b/tests/ApplicationTest.php @@ -0,0 +1,47 @@ +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)); + } +} diff --git a/tests/blobs/github-tags.json b/tests/blobs/github-tags.json new file mode 100644 index 00000000..3ab02605 --- /dev/null +++ b/tests/blobs/github-tags.json @@ -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" + } + } +] diff --git a/tests/blobs/rev-manifest.json b/tests/blobs/rev-manifest.json new file mode 100644 index 00000000..1e95170a --- /dev/null +++ b/tests/blobs/rev-manifest.json @@ -0,0 +1,4 @@ +{ + "foo.css": "foo00.css", + "bar.js": "bar00.js" +}