From 4b04070c9aa488069824569d1a3ddbcab16fa65d Mon Sep 17 00:00:00 2001 From: Jack Wilkinson Date: Mon, 22 Aug 2022 00:58:30 +0100 Subject: [PATCH 1/4] Added support for creating a file object from file in storage --- src/Database/Attach/File.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Database/Attach/File.php b/src/Database/Attach/File.php index 5ecf7b5a1..35d18c7c3 100644 --- a/src/Database/Attach/File.php +++ b/src/Database/Attach/File.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Storage; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\File as FileObj; +use Winter\Storm\Exception\ApplicationException; /** * File attachment model @@ -141,6 +142,29 @@ public function fromFile($filePath) return $this; } + /** + * Creates a file object from a file in storage. + */ + public function fromStorage(string $filePath): static + { + $disk = $this->getDisk(); + + if (!$disk->exists($filePath)) { + throw new \InvalidArgumentException(sprintf('File `%s` not found in storage', $filePath)); + } + + $this->file_name = basename($filePath); + $this->file_size = $disk->size($filePath); + $this->content_type = $disk->mimeType($filePath); + $this->disk_name = $this->getDiskName(); + + if (!$disk->copy($filePath, $this->getDiskPath())) { + throw new ApplicationException('Unable to move uploaded file'); + } + + return $this; + } + /** * Creates a file object from raw data. * @@ -529,8 +553,10 @@ public function beforeSave() if ($this->data !== null) { if ($this->data instanceof UploadedFile) { $this->fromPost($this->data); - } else { + } elseif (file_exists($this->data)) { $this->fromFile($this->data); + } else { + $this->fromStorage($this->data); } $this->data = null; From b3395654e6c8fd4527e95d43a1bbad0684852cd2 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Mon, 22 Aug 2022 15:25:01 -0600 Subject: [PATCH 2/4] Update src/Database/Attach/File.php --- src/Database/Attach/File.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Attach/File.php b/src/Database/Attach/File.php index 35d18c7c3..a107ec1f4 100644 --- a/src/Database/Attach/File.php +++ b/src/Database/Attach/File.php @@ -143,7 +143,7 @@ public function fromFile($filePath) } /** - * Creates a file object from a file in storage. + * Creates a file object from a file on the disk returned by $this->getDisk() */ public function fromStorage(string $filePath): static { From 5ced0a8ddb640bbded6387fc60659aea40c19fab Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Mon, 22 Aug 2022 15:25:26 -0600 Subject: [PATCH 3/4] Update src/Database/Attach/File.php --- src/Database/Attach/File.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Attach/File.php b/src/Database/Attach/File.php index a107ec1f4..fcdc5b780 100644 --- a/src/Database/Attach/File.php +++ b/src/Database/Attach/File.php @@ -150,7 +150,7 @@ public function fromStorage(string $filePath): static $disk = $this->getDisk(); if (!$disk->exists($filePath)) { - throw new \InvalidArgumentException(sprintf('File `%s` not found in storage', $filePath)); + throw new \InvalidArgumentException(sprintf('File `%s` was not found on the storage disk', $filePath)); } $this->file_name = basename($filePath); From 58ef38105cfdb9d191c2d17fe469261b3737e7d5 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Mon, 22 Aug 2022 15:26:33 -0600 Subject: [PATCH 4/4] Update src/Database/Attach/File.php --- src/Database/Attach/File.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Attach/File.php b/src/Database/Attach/File.php index fcdc5b780..ccaba0dfe 100644 --- a/src/Database/Attach/File.php +++ b/src/Database/Attach/File.php @@ -159,7 +159,7 @@ public function fromStorage(string $filePath): static $this->disk_name = $this->getDiskName(); if (!$disk->copy($filePath, $this->getDiskPath())) { - throw new ApplicationException('Unable to move uploaded file'); + throw new ApplicationException(sprintf('Unable to copy `%s` to `%s`', $filePath, $this->getDiskPath())); } return $this;