Skip to content

Commit

Permalink
9.4.0 (#617)
Browse files Browse the repository at this point in the history
* feat: support string url as path for files

* Fix styling

---------

Co-authored-by: binaryk <[email protected]>
  • Loading branch information
binaryk and binaryk authored Dec 1, 2024
1 parent 011644b commit cea3d19
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,44 @@ 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,
$model,
$this->getStorageDisk(),
$this->getStoragePath()
);
// };
}

$result = call_user_func(
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit cea3d19

Please sign in to comment.