Skip to content

Commit

Permalink
Avoid entire file hashing and add separate File::contentHash()
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Oct 24, 2024
1 parent ef900d2 commit 53b1238
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions formwork/src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class File extends Model implements Arrayable, Stringable
#[ReadonlyModelProperty]
protected string $hash;

/**
* File content hash
*/
#[ReadonlyModelProperty]
protected string $contentHash;

protected FileUriGenerator $uriGenerator;

/**
Expand Down Expand Up @@ -172,11 +178,19 @@ public function lastModifiedTime(): int
*/
public function hash(): string
{
if (isset($this->hash)) {
return $this->hash;
return $this->hash ??= hash('sha256', $this->path . ':' . $this->lastModifiedTime());
}

/**
* Get file content hash
*/
public function contentHash(): string
{
if (isset($this->contentHash)) {
return $this->contentHash;
}
if ($hash = hash_file('sha256', $this->path)) {
return $this->hash = $hash;
return $this->contentHash = $hash;
}
throw new RuntimeException('Cannot calculate file hash');
}
Expand Down

0 comments on commit 53b1238

Please sign in to comment.