Show if there's a new version available on the admin dashboard #143

This commit is contained in:
Nabeel Shahzad
2018-01-19 18:44:17 -05:00
parent a1b9894f97
commit 2cd45acbdf
6 changed files with 114 additions and 4 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Facades;
use GuzzleHttp\Client;
use \Illuminate\Support\Facades\Facade;
class Utils extends Facade
@@ -11,6 +12,33 @@ class Utils extends Facade
return 'utils';
}
/**
* Download a URI. If a file is given, it will save the downloaded
* content into that file
* @param $uri
* @param null $file
* @return string
* @throws \RuntimeException
*/
public static function downloadUrl($uri, $file=null)
{
$opts = [];
if($file !== null) {
$opts['sink'] = $file;
}
$client = new Client();
$response = $client->request('GET', $uri, $opts);
$body = $response->getBody()->getContents();
if($response->getHeader('content-type') === 'application/json') {
$body = \GuzzleHttp\json_decode($body);
}
return $body;
}
/**
* Returns a 40 character API key that a user can use
* @return string

View File

@@ -3,12 +3,17 @@
namespace App\Http\Controllers\Admin;
use Auth;
use Flash;
use Version;
use Illuminate\Http\Request;
use App\Facades\Utils;
use App\Repositories\NewsRepository;
use App\Repositories\PirepRepository;
use App\Repositories\UserRepository;
use vierbergenlars\SemVer\version as semver;
class DashboardController extends BaseController
{
private $newsRepo, $pirepRepo, $userRepo;
@@ -24,10 +29,29 @@ class DashboardController extends BaseController
}
/**
* Display a listing of the Airlines.
* Check if a new version is available by checking the VERSION file from
* S3 and then using the semver library to do the comparison. Just show
* a session flash file on this page that'll get cleared right away
* @throws \RuntimeException
*/
protected function checkNewVersion()
{
$current_version = new semver(Version::compact());
$latest_version = new semver(Utils::downloadUrl(config('phpvms.version_file')));
if(semver::gt($latest_version, $current_version)) {
Flash::warning('New version '.$latest_version.' is available!');
}
}
/**
* Show the admin dashboard
* @throws \RuntimeException
*/
public function index(Request $request)
{
$this->checkNewVersion();
return view('admin.dashboard.index', [
'news' => $this->newsRepo->getLatest(),
'pending_pireps' => $this->pirepRepo->getPendingCount(),