Skip to content
Merged

9.4.0 #617

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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