From 53b1238d553e8534d3d82bffa68f1559ed9f1b69 Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione <18699708+giuscris@users.noreply.github.com> Date: Thu, 24 Oct 2024 22:07:50 +0200 Subject: [PATCH] Avoid entire file hashing and add separate `File::contentHash()` --- formwork/src/Files/File.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/formwork/src/Files/File.php b/formwork/src/Files/File.php index 7e80ac31..db639577 100644 --- a/formwork/src/Files/File.php +++ b/formwork/src/Files/File.php @@ -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; /** @@ -172,11 +178,19 @@ class File extends Model implements Arrayable, Stringable */ 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'); }