From cea3d19e3b0dfb870b4fd2588bb9380b077ae41e Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Sun, 1 Dec 2024 14:45:45 +0200 Subject: [PATCH] 9.4.0 (#617) * feat: support string url as path for files * Fix styling --------- Co-authored-by: binaryk --- src/Fields/File.php | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Fields/File.php b/src/Fields/File.php index 0713a002..c3a8f21c 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -229,13 +229,37 @@ protected function columnsThatShouldBeDeleted(): array public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = null) { + // Handle URL input first + if ($request->has($this->attribute) && is_string($request->input($this->attribute))) { + $url = $request->input($this->attribute); + + if (filter_var($url, FILTER_VALIDATE_URL)) { + if ($this->isPrunable()) { + call_user_func( + $this->deleteCallback, + $request, + $model, + $this->getStorageDisk(), + $this->getStoragePath() + ); + } + + $model->{$this->attribute} = $url; + + if ($this->originalNameColumn) { + $model->{$this->originalNameColumn} = basename($url); + } + + return $this; + } + } + + // Existing file upload logic if (is_null($file = $request->file($this->attribute)) || ! $file->isValid()) { return $this; } if ($this->isPrunable()) { - // Delete old file if exists. - // return function () use ($model, $request) { call_user_func( $this->deleteCallback, $request, @@ -243,7 +267,6 @@ public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = n $this->getStorageDisk(), $this->getStoragePath() ); - // }; } $result = call_user_func( @@ -276,6 +299,20 @@ public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = n return $this; } + public function getStoringRules(): array + { + $rules = parent::getStoringRules(); + + // Modify validation to accept URLs + foreach ($rules as &$rule) { + if (is_string($rule) && Str::startsWith($rule, 'file')) { + $rule = 'sometimes|'.$rule; + } + } + + return $rules; + } + /** * Get the full path that the field is stored at on disk. *