mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
Move version checking logic to a service
This commit is contained in:
parent
a475454b5f
commit
351efe4092
5 changed files with 78 additions and 49 deletions
|
@ -2,12 +2,8 @@
|
|||
|
||||
namespace App;
|
||||
|
||||
use Cache;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Foundation\Application as IlluminateApplication;
|
||||
use InvalidArgumentException;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Extends \Illuminate\Foundation\Application to override some defaults.
|
||||
|
@ -19,7 +15,7 @@ class Application extends IlluminateApplication
|
|||
*
|
||||
* @link https://github.com/phanan/koel/releases
|
||||
*/
|
||||
const KOEL_VERSION = 'v3.7.2';
|
||||
public const KOEL_VERSION = 'v3.8.0';
|
||||
|
||||
/**
|
||||
* We have merged public path and base path.
|
||||
|
@ -69,24 +65,4 @@ class Application extends IlluminateApplication
|
|||
|
||||
return $cdnUrl ? $cdnUrl.'/'.trim(ltrim($name, '/')) : trim(asset($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest version number of Koel from GitHub.
|
||||
*/
|
||||
public function getLatestVersion(Client $client = null): string
|
||||
{
|
||||
return Cache::remember('latestKoelVersion', 1 * 24 * 60, static function () use ($client) {
|
||||
$client = $client ?: new Client();
|
||||
|
||||
try {
|
||||
return json_decode(
|
||||
$client->get('https://api.github.com/repos/phanan/koel/tags')->getBody()
|
||||
)[0]->name;
|
||||
} catch (Exception $e) {
|
||||
Log::error($e);
|
||||
|
||||
return self::KOEL_VERSION;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ use App\Repositories\InteractionRepository;
|
|||
use App\Repositories\PlaylistRepository;
|
||||
use App\Repositories\SettingRepository;
|
||||
use App\Repositories\UserRepository;
|
||||
use App\Services\ApplicationInformationService;
|
||||
use App\Services\iTunesService;
|
||||
use App\Services\LastfmService;
|
||||
use App\Services\MediaCacheService;
|
||||
|
@ -24,6 +25,7 @@ class DataController extends Controller
|
|||
private $playlistRepository;
|
||||
private $interactionRepository;
|
||||
private $userRepository;
|
||||
private $applicationInformationService;
|
||||
|
||||
public function __construct(
|
||||
LastfmService $lastfmService,
|
||||
|
@ -33,7 +35,8 @@ class DataController extends Controller
|
|||
SettingRepository $settingRepository,
|
||||
PlaylistRepository $playlistRepository,
|
||||
InteractionRepository $interactionRepository,
|
||||
UserRepository $userRepository
|
||||
UserRepository $userRepository,
|
||||
ApplicationInformationService $applicationInformationService
|
||||
) {
|
||||
$this->lastfmService = $lastfmService;
|
||||
$this->youTubeService = $youTubeService;
|
||||
|
@ -43,6 +46,7 @@ class DataController extends Controller
|
|||
$this->playlistRepository = $playlistRepository;
|
||||
$this->interactionRepository = $interactionRepository;
|
||||
$this->userRepository = $userRepository;
|
||||
$this->applicationInformationService = $applicationInformationService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,7 +70,9 @@ class DataController extends Controller
|
|||
&& is_executable(config('koel.streaming.ffmpeg_path')),
|
||||
'cdnUrl' => app()->staticUrl(),
|
||||
'currentVersion' => Application::KOEL_VERSION,
|
||||
'latestVersion' => $request->user()->is_admin ? app()->getLatestVersion() : Application::KOEL_VERSION,
|
||||
'latestVersion' => $request->user()->is_admin
|
||||
? $this->applicationInformationService->getLatestVersionNumber()
|
||||
: Application::KOEL_VERSION,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
40
app/Services/ApplicationInformationService.php
Normal file
40
app/Services/ApplicationInformationService.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Application;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
||||
use Illuminate\Log\Logger;
|
||||
|
||||
class ApplicationInformationService
|
||||
{
|
||||
private $client;
|
||||
private $cache;
|
||||
private $logger;
|
||||
|
||||
public function __construct(Client $client, Cache $cache, Logger $logger)
|
||||
{
|
||||
$this->client = $client;
|
||||
$this->cache = $cache;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest version number of Koel from GitHub.
|
||||
*/
|
||||
public function getLatestVersionNumber(): string {
|
||||
return $this->cache->remember('latestKoelVersion', 1 * 24 * 60, function (): string {
|
||||
try {
|
||||
return json_decode(
|
||||
$this->client->get('https://api.github.com/repos/phanan/koel/tags')->getBody()
|
||||
)[0]->name;
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e);
|
||||
|
||||
return Application::KOEL_VERSION;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Integration\Services;
|
||||
|
||||
use App\Services\ApplicationInformationService;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Log\Logger;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
||||
|
||||
class ApplicationInformationServiceTest extends TestCase
|
||||
{
|
||||
public function testGetLatestVersionNumber(): void
|
||||
{
|
||||
$latestVersion = 'v1.1.2';
|
||||
|
||||
$mock = new MockHandler([
|
||||
new Response(200, [], file_get_contents(__DIR__.'../../../blobs/github-tags.json')),
|
||||
]);
|
||||
|
||||
$client = new Client(['handler' => HandlerStack::create($mock)]);
|
||||
$service = new ApplicationInformationService($client, app(Cache::class), app(Logger::class));
|
||||
|
||||
self::assertEquals($latestVersion, $service->getLatestVersionNumber());
|
||||
}
|
||||
}
|
|
@ -2,10 +2,6 @@
|
|||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ApplicationTest extends TestCase
|
||||
|
@ -77,22 +73,4 @@ class ApplicationTest extends TestCase
|
|||
// Then I see they're constructed correctly
|
||||
$this->assertEquals('http://cdn.tld/public/foo00.css', $assetURL);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function koels_latest_version_can_be_retrieved()
|
||||
{
|
||||
// Given there is a latest version
|
||||
$latestVersion = 'v1.1.2';
|
||||
|
||||
// When I check for the latest version
|
||||
$mock = new MockHandler([
|
||||
new Response(200, [], file_get_contents(__DIR__.'../../blobs/github-tags.json')),
|
||||
]);
|
||||
|
||||
$client = new Client(['handler' => HandlerStack::create($mock)]);
|
||||
$checkedVersion = app()->getLatestVersion($client);
|
||||
|
||||
// Then I receive the latest version
|
||||
$this->assertEquals($latestVersion, $checkedVersion);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue