mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2025-12-26 07:13:35 +01:00
Added functions to download latest zip from github
Have moved UpdateHelper into a facade
This commit is contained in:
@@ -3,22 +3,37 @@
|
||||
namespace App\Helpers;
|
||||
|
||||
use Exception;
|
||||
use ZipArchive;
|
||||
|
||||
class UpdateHelper {
|
||||
public static function check()
|
||||
public $url;
|
||||
public $currentVersion;
|
||||
public $user;
|
||||
public $repo;
|
||||
public $branch;
|
||||
|
||||
function __construct() {
|
||||
$this->currentVersion = config('speedtest.version');
|
||||
$this->user = config('speedtest.user');
|
||||
$this->repo = config('speedtest.repo');
|
||||
$this->branch = config('speedtest.branch');
|
||||
$this->latestVersion = 'unknown';
|
||||
$this->download = null;
|
||||
}
|
||||
|
||||
public function check()
|
||||
{
|
||||
$current = config('speedtest.version', false);
|
||||
if($current === false) {
|
||||
if($this->currentVersion === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$gitVersion = UpdateHelper::checkLatestVersion();
|
||||
$gitVersion = $this->checkLatestVersion();
|
||||
if($gitVersion === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if((bool)(version_compare($current, $gitVersion['version']))) {
|
||||
$changelog = UpdateHelper::getChangelog();
|
||||
if((bool)(version_compare($this->currentVersion, $gitVersion['version']))) {
|
||||
$changelog = $this->getChangelog();
|
||||
return [
|
||||
'version' => $gitVersion['version'],
|
||||
'changelog' => $changelog[$gitVersion['version']],
|
||||
@@ -28,18 +43,15 @@ class UpdateHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public static function checkLatestVersion()
|
||||
public function checkLatestVersion()
|
||||
{
|
||||
$user = config('speedtest.user');
|
||||
$repo = config('speedtest.repo');
|
||||
$branch = config('speedtest.branch');
|
||||
|
||||
$url = 'https://raw.githubusercontent.com/'
|
||||
.$user
|
||||
.$this->user
|
||||
.'/'
|
||||
.$repo
|
||||
.$this->repo
|
||||
.'/'
|
||||
.$branch
|
||||
.$this->branch
|
||||
.'/config/speedtest.php';
|
||||
|
||||
try {
|
||||
@@ -51,27 +63,23 @@ class UpdateHelper {
|
||||
$pattern = "/'version' => '([0-9]{1,}\.[0-9]{1,}\.[0-9]{1,})'/";
|
||||
$version = [];
|
||||
preg_match($pattern, $gitFile, $version);
|
||||
$version = $version[1];
|
||||
$this->latestVersion = $version[1];
|
||||
|
||||
return [
|
||||
'repo' => $user . '/' . $repo,
|
||||
'branch' => $branch,
|
||||
'version' => $version,
|
||||
'repo' => $this->user . '/' . $this->repo,
|
||||
'branch' => $this->branch,
|
||||
'version' => $this->latestVersion,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getChangelog()
|
||||
public function getChangelog()
|
||||
{
|
||||
$user = config('speedtest.user');
|
||||
$repo = config('speedtest.repo');
|
||||
$branch = config('speedtest.branch');
|
||||
|
||||
$url = 'https://raw.githubusercontent.com/'
|
||||
.$user
|
||||
.$this->user
|
||||
.'/'
|
||||
.$repo
|
||||
.$this->repo
|
||||
.'/'
|
||||
.$branch
|
||||
.$this->branch
|
||||
.'/changelog.json';
|
||||
|
||||
try {
|
||||
@@ -82,4 +90,44 @@ class UpdateHelper {
|
||||
|
||||
return $changelog;
|
||||
}
|
||||
|
||||
public function downloadLatest()
|
||||
{
|
||||
$url = 'https://github.com/'
|
||||
.$this->user
|
||||
.'/'
|
||||
.$this->repo
|
||||
.'/archive/'
|
||||
.$this->branch
|
||||
.'.zip';
|
||||
|
||||
try {
|
||||
$zip = file_get_contents($url);
|
||||
$name = '/tmp/'.$this->repo.'-update.zip';
|
||||
file_put_contents($name, $zip);
|
||||
return true;
|
||||
} catch(Exception $e) {
|
||||
return $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function extractFiles()
|
||||
{
|
||||
$zip = new ZipArchive();
|
||||
$res = $zip->open('/tmp/'.$this->repo.'-update.zip');
|
||||
if($res === true) {
|
||||
$zip->extractTo('/tmp/'.$this->repo.'-update/');
|
||||
$zip->close();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function updateFiles()
|
||||
{
|
||||
foreach (glob('/tmp/'.$this->repo.'-update/') as $folder) {
|
||||
return $folder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user