Skip to content

Commit

Permalink
Add support for sha1sum.txt file
Browse files Browse the repository at this point in the history
Fix writing to hash files to match old format
  • Loading branch information
shivammathur committed Feb 10, 2025
1 parent 03ad5dc commit b7d5d36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 17 additions & 8 deletions src/Actions/GetListing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/Actions/GetListingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b7d5d36

Please sign in to comment.