From 972dd1292ee2e701fe2c193f39448a87ac510644 Mon Sep 17 00:00:00 2001 From: Bart van der Braak Date: Mon, 15 Jul 2024 14:52:36 +0200 Subject: [PATCH 1/2] Web: Add v2 JSON endpoint with checksum key-value pairs --- source/Build.php | 10 ++++++++++ source/BuildsRenderer.php | 14 ++++++++++++-- source/main.php | 6 +++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/source/Build.php b/source/Build.php index 280b813..01b4b56 100644 --- a/source/Build.php +++ b/source/Build.php @@ -22,6 +22,7 @@ class Build { public $file_size; public $file_extension; public $release_cycle; + public $sha256_checksum; // New property public $directory_lister; public function __construct(string $file_path) { @@ -83,6 +84,15 @@ class Build { $this->file_size = filesize($file_path); $this->file_extension = $file_extension; + // Check for existing .sha256 file. + $sha256_file = $file_path . '.sha256'; + if (file_exists($sha256_file)) { + $this->sha256_checksum = trim(str_replace(["\r", "\n", "\t"], '', file_get_contents($sha256_file))); + } else { + // Compute SHA256 checksum. + $this->sha256_checksum = strtoupper(hash_file('sha256', $file_path)); + } + // Release cycle. // // NOTE: Keep it last, so the rule matcher can access all possible diff --git a/source/BuildsRenderer.php b/source/BuildsRenderer.php index 438cd89..54293db 100644 --- a/source/BuildsRenderer.php +++ b/source/BuildsRenderer.php @@ -8,11 +8,11 @@ class BuildsRenderer { $this->builds = $builds; } - public function renderJSON() { + public function renderJSON($version = 1) { /* Return a JSON formatted list of builds. */ $builds_json = array(); foreach ($this->builds as $build) { - $builds_json[] = array( + $build_data = array( 'url' => $this->getFileNameURL($build), 'app' => $build->app, 'version' => $build->version, @@ -29,6 +29,16 @@ class BuildsRenderer { 'file_extension' => $build->file_extension, 'release_cycle' => $build->release_cycle, ); + + if ($version == 2) { + // If version 2 is requested, skip any .sha256 files and add a checksum key-value entry + if (substr($build->file_name, -7) === '.sha256') { + continue; + } + $build_data['checksum'] = $build->sha256_checksum; + } + + $builds_json[] = $build_data; } echo(json_encode($builds_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); } diff --git a/source/main.php b/source/main.php index c820f94..6e98c76 100644 --- a/source/main.php +++ b/source/main.php @@ -16,15 +16,15 @@ function handleNotFoundRequest() { function renderDownloadResponseAsJSON($lister) { header('Content-Type: application/json; charset=utf-8'); // Require v=1 to be specified - if (!isset($_GET['v']) || $_GET['v'] != '1') { - $data = [ 'error' => 'Missing supported format version, for example v=1']; + if (!isset($_GET['v']) || ($_GET['v'] != '1' && $_GET['v'] != '2')) { + $data = ['error' => 'Invalid version specified. Please provide a supported version using v=1 or v=2.']; echo json_encode( $data ); http_response_code(400); return; } $builds = $lister->getBuilds(); $renderer = new BuildsRenderer($builds); - $renderer->renderJSON(); + $renderer->renderJSON($_GET['v']); return; } -- 2.30.2 From 0ac2ca0896db72afa6da6c83df92baef92638b29 Mon Sep 17 00:00:00 2001 From: Bart van der Braak Date: Mon, 15 Jul 2024 18:21:44 +0200 Subject: [PATCH 2/2] Fix: Never compute checksum and use endsWith util --- source/Build.php | 5 ++--- source/BuildsRenderer.php | 2 +- source/main.php | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/source/Build.php b/source/Build.php index 01b4b56..1e3b02e 100644 --- a/source/Build.php +++ b/source/Build.php @@ -22,7 +22,7 @@ class Build { public $file_size; public $file_extension; public $release_cycle; - public $sha256_checksum; // New property + public $sha256_checksum; public $directory_lister; public function __construct(string $file_path) { @@ -89,8 +89,7 @@ class Build { if (file_exists($sha256_file)) { $this->sha256_checksum = trim(str_replace(["\r", "\n", "\t"], '', file_get_contents($sha256_file))); } else { - // Compute SHA256 checksum. - $this->sha256_checksum = strtoupper(hash_file('sha256', $file_path)); + $this->sha256_checksum = null; } // Release cycle. diff --git a/source/BuildsRenderer.php b/source/BuildsRenderer.php index 54293db..30dad52 100644 --- a/source/BuildsRenderer.php +++ b/source/BuildsRenderer.php @@ -32,7 +32,7 @@ class BuildsRenderer { if ($version == 2) { // If version 2 is requested, skip any .sha256 files and add a checksum key-value entry - if (substr($build->file_name, -7) === '.sha256') { + if (endsWith($build->file_name, '.sha256')) { continue; } $build_data['checksum'] = $build->sha256_checksum; diff --git a/source/main.php b/source/main.php index 6e98c76..2a8ce83 100644 --- a/source/main.php +++ b/source/main.php @@ -15,7 +15,7 @@ function handleNotFoundRequest() { function renderDownloadResponseAsJSON($lister) { header('Content-Type: application/json; charset=utf-8'); - // Require v=1 to be specified + // Require v=1 or v=2 to be specified if (!isset($_GET['v']) || ($_GET['v'] != '1' && $_GET['v'] != '2')) { $data = ['error' => 'Invalid version specified. Please provide a supported version using v=1 or v=2.']; echo json_encode( $data ); -- 2.30.2