From 0836ca5557dec6639e827c39ffacbdcc332a80c0 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Sun, 29 Jul 2018 21:58:28 +0100 Subject: [PATCH 1/2] #86: Switched the update check from Github to Gitea --- .../Controllers/Admin/DefaultController.php | 9 +- app/Services/GiteaService.php | 112 ++++++++++++++++++ config/services.php | 30 +---- 3 files changed, 123 insertions(+), 28 deletions(-) create mode 100644 app/Services/GiteaService.php diff --git a/app/Http/Controllers/Admin/DefaultController.php b/app/Http/Controllers/Admin/DefaultController.php index 9f43ac7..4c2d851 100644 --- a/app/Http/Controllers/Admin/DefaultController.php +++ b/app/Http/Controllers/Admin/DefaultController.php @@ -15,6 +15,7 @@ use App\Http\Requests\SaveSettingsRequest; use App\Label; use App\Mail\TestMailConfig; use App\Photo; +use App\Services\GiteaService; use App\Services\GithubService; use App\Services\PhotoService; use App\Storage; @@ -46,18 +47,18 @@ class DefaultController extends Controller { try { - $githubService = new GithubService(); - $releaseInfo = $githubService->checkForLatestRelease(); + $giteaService = new GiteaService(); + $releaseInfo = $giteaService->checkForLatestRelease(); // Convert the publish date so we can re-format it with the user's settings - $publishDate = \DateTime::createFromFormat('Y-m-d\TH:i:s\Z', $releaseInfo->published_at); + $publishDate = \DateTime::createFromFormat('Y-m-d\TH:i:sP', $releaseInfo->published_at); // HTML-ify the body text $body = nl2br($releaseInfo->body); $body = preg_replace('/\*\*(.+)\*\*/', '$1', $body); // Remove the "v" from the release name - $version = substr($releaseInfo->name, 1); + $version = substr($releaseInfo->tag_name, 1); // Determine if we can upgrade $canUpgrade = version_compare($version, config('app.version')) > 0; diff --git a/app/Services/GiteaService.php b/app/Services/GiteaService.php new file mode 100644 index 0000000..e37b7da --- /dev/null +++ b/app/Services/GiteaService.php @@ -0,0 +1,112 @@ +config = config('services.gitea'); + $this->cacheFile = storage_path('app/gitea_cache.txt'); + } + + public function checkForLatestRelease() + { + $cacheData = null; + + if ($this->doesCacheExist()) + { + // Get the etag from the cache + $cacheData = $this->getCacheData(); + } + else + { + // Lookup and store the version information + $statusCode = -1; + $result = $this->getLatestReleaseFromGitea($statusCode); + + if ($statusCode == 200) + { + $releases = json_decode($result[1]); + + $latestRelease = null; + foreach ($releases as $release) + { + if (is_null($latestRelease) || version_compare($release->tag_name, $latestRelease->tag_name) > 0) + { + $latestRelease = $release; + } + } + + $cacheData = $this->setCacheData($latestRelease); + } + } + + // GitHub compatibility + $cacheData->html_url = sprintf($this->config['releases_url'], $this->config['repo_owner'], $this->config['repo_name']); + + return $cacheData; + } + + private function doesCacheExist() + { + $exists = file_exists($this->cacheFile); + + if ($exists) + { + // Check modified time on the file + $stat = stat($this->cacheFile); + + $diff = time() - $stat['mtime']; + if ($diff > $this->config['cache_time_seconds']) + { + $exists = false; + } + } + + return $exists; + } + + private function getCacheData() + { + return json_decode(file_get_contents($this->cacheFile)); + } + + private function getLatestReleaseFromGitea(&$statusCode) + { + $httpHeaders = [ + sprintf('User-Agent: aheathershaw/blue-twilight (v%s)', config('app.version')) + ]; + + if (isset($this->config['api_key']) && !empty($this->config['api_key'])) + { + $httpHeaders[] = sprintf('Authorization: %s', $this->config['api_key']); + } + + $apiUrl = sprintf('%s/repos/%s/%s/releases', $this->config['api_url'], $this->config['repo_owner'], $this->config['repo_name']); + + $ch = curl_init($apiUrl); + curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders); + curl_setopt($ch, CURLOPT_HEADER, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $result = curl_exec($ch); + + if ($result === false) + { + throw new \Exception(sprintf('Error from Gitea: %s', curl_error($ch))); + } + + $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + return explode("\r\n\r\n", $result, 2); + } + + private function setCacheData($data) + { + file_put_contents($this->cacheFile, json_encode(get_object_vars($data))); + return $data; + } +} \ No newline at end of file diff --git a/config/services.php b/config/services.php index b539f9f..eab4683 100644 --- a/config/services.php +++ b/config/services.php @@ -14,33 +14,15 @@ return [ | */ - 'github' => [ - 'latest_release_url' => 'https://api.github.com/repos/andysh-uk/blue-twilight/releases/latest' + 'gitea' => [ + 'api_url' => 'https://apps.andysh.uk/api/v1', + 'cache_time_seconds' => 3600, + 'releases_url' => 'https://apps.andysh.uk/%s/%s/releases', + 'repo_name' => 'blue-twilight', + 'repo_owner' => 'aheathershaw' ], 'recaptcha' => [ 'verify_url' => 'https://www.google.com/recaptcha/api/siteverify' ] - - /*'mailgun' => [ - 'domain' => env('MAILGUN_DOMAIN'), - 'secret' => env('MAILGUN_SECRET'), - ], - - 'ses' => [ - 'key' => env('SES_KEY'), - 'secret' => env('SES_SECRET'), - 'region' => 'us-east-1', - ], - - 'sparkpost' => [ - 'secret' => env('SPARKPOST_SECRET'), - ], - - 'stripe' => [ - 'model' => App\User::class, - 'key' => env('STRIPE_KEY'), - 'secret' => env('STRIPE_SECRET'), - ],*/ - ]; From fa861c0b096a145b4ef5c0dbb31b7f5ab9f326ce Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Thu, 16 Aug 2018 08:46:19 +0100 Subject: [PATCH 2/2] #86: Updated more references from Github to Gitea (Admin > About page) --- resources/lang/en/admin.php | 8 ++++---- resources/views/themes/base/admin/about.blade.php | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/resources/lang/en/admin.php b/resources/lang/en/admin.php index 19bcbd6..67bd2ec 100644 --- a/resources/lang/en/admin.php +++ b/resources/lang/en/admin.php @@ -7,12 +7,12 @@ return [ 'about' => [ 'current_version' => 'You are running version', 'date_published_label' => 'Release date:', - 'github_link' => 'Github Project Website', - 'intro' => 'Blue Twilight is an App by Andy.', - 'intro_2' => 'It is made with in the UK by software developer Andy Heathershaw.', - 'latest_version_loading' => 'Checking for the latest release on Github...', + 'intro' => 'Blue Twilight is an App by Andy.', + 'intro_2' => 'It is made with in the UK by software developer Andy Heathershaw.', + 'latest_version_loading' => 'Checking for the latest release...', 'licence_header' => 'Licence', 'links_header' => 'Useful Links', + 'project_website_link' => 'Project Website', 'title' => 'About Blue Twilight', 'up_to_date' => 'Good job - your installation of Blue Twilight is up-to-date!', 'update_available' => 'An update is available to Blue Twilight.', diff --git a/resources/views/themes/base/admin/about.blade.php b/resources/views/themes/base/admin/about.blade.php index 0232980..c2263bb 100644 --- a/resources/views/themes/base/admin/about.blade.php +++ b/resources/views/themes/base/admin/about.blade.php @@ -27,9 +27,9 @@