[PHP-WEBMASTER] [web-downloads] main: Add support for sha1sum.txt file

Author: Shivam Mathur (shivammathur)
Date: 2025-02-10T21:35:22+05:30

Commit: Add support for sha1sum.txt file · php/web-downloads@b7d5d36 · GitHub
Raw diff: https://github.com/php/web-downloads/commit/b7d5d367006ea27a7dac17af1373108e1907181d.diff

Add support for sha1sum.txt file

Fix writing to hash files to match old format

Changed paths:
  M src/Actions/GetListing.php
  M tests/Actions/GetListingTest.php

Diff:

diff --git a/src/Actions/GetListing.php b/src/Actions/GetListing.php
index c36e2dc..ad5142e 100644
--- a/src/Actions/GetListing.php
+++ b/src/Actions/GetListing.php
@@ -12,7 +12,8 @@ public function handle(string $directory): array
         }

         $releases = ;
- $sha256sums = $this->getSha256Sums($directory);
+ $sha256sums = $this->getShaSums($directory, 'sha256');
+ $this->getShaSums($directory, 'sha1');
         foreach ($builds as $file) {
             $file_ori = $file;
             $mtime = date('Y-M-d H:i:s', filemtime($file));
@@ -59,20 +60,28 @@ public function handle(string $directory): array
         return $releases;
     }

- public function getSha256Sums($directory): array
+ public function getShaSums(string $directory, string $algo): array
     {
         $result = ;
- if(!file_exists("$directory/sha256sum.txt")) {
- file_put_contents("$directory/sha256sum.txt", '');
+ $hashes = ;
+ if(!file_exists("$directory/{$algo}sum.txt")) {
+ file_put_contents("$directory/{$algo}sum.txt", '');
         }
- $sha_file = fopen("$directory/sha256sum.txt", 'w');
         foreach (scandir($directory) as $filename) {
             if (pathinfo($filename, PATHINFO_EXTENSION) !== 'zip') {
                 continue;
             }
- $sha256 = hash_file('sha256', "$directory/$filename");
- fwrite($sha_file, "$sha256 *$filename\n");
- $result[strtolower(basename($filename))] = $sha256;
+ $hash = hash_file($algo, "$directory/$filename");
+ $result[strtolower(basename($filename))] = $hash;
+ preg_match('/-(\d+\.\d+)/', $filename, $matches);
+ $hashes[$matches[1]][$filename] = $hash;
+ }
+ $sha_file = fopen("$directory/{$algo}sum.txt", 'w');
+ foreach ($hashes as $version) {
+ foreach ($version as $filename => $hash) {
+ fwrite($sha_file, "$hash *$filename\n");
+ }
+ fwrite($sha_file, "\n");
         }
         fclose($sha_file);
         return $result;
diff --git a/tests/Actions/GetListingTest.php b/tests/Actions/GetListingTest.php
index 985bcf7..8d5ee50 100644
--- a/tests/Actions/GetListingTest.php
+++ b/tests/Actions/GetListingTest.php
@@ -97,11 +97,11 @@ public function testParseFileName(string $fileName, array $expected): void

     public function testGetSha256SumsCreatesFileAndReturnsHashes(): void
     {
- $dummyZip = $this->tempDir . '/dummy.zip';
+ $dummyZip = $this->tempDir . '/php-7.4.34.zip';
         $content = "dummy content";
         file_put_contents($dummyZip, $content);

- $sums = $this->getListing->getSha256Sums($this->tempDir);
+ $sums = $this->getListing->getShaSums($this->tempDir, 'sha256');

         $key = strtolower(basename($dummyZip));
         $expectedHash = hash_file('sha256', $dummyZip);