Skip to content

Commit cea3d19

Browse files
authored
9.4.0 (#617)
* feat: support string url as path for files * Fix styling --------- Co-authored-by: binaryk <binaryk@users.noreply.github.com>
1 parent 011644b commit cea3d19

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

src/Fields/File.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,21 +229,44 @@ protected function columnsThatShouldBeDeleted(): array
229229

230230
public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = null)
231231
{
232+
// Handle URL input first
233+
if ($request->has($this->attribute) && is_string($request->input($this->attribute))) {
234+
$url = $request->input($this->attribute);
235+
236+
if (filter_var($url, FILTER_VALIDATE_URL)) {
237+
if ($this->isPrunable()) {
238+
call_user_func(
239+
$this->deleteCallback,
240+
$request,
241+
$model,
242+
$this->getStorageDisk(),
243+
$this->getStoragePath()
244+
);
245+
}
246+
247+
$model->{$this->attribute} = $url;
248+
249+
if ($this->originalNameColumn) {
250+
$model->{$this->originalNameColumn} = basename($url);
251+
}
252+
253+
return $this;
254+
}
255+
}
256+
257+
// Existing file upload logic
232258
if (is_null($file = $request->file($this->attribute)) || ! $file->isValid()) {
233259
return $this;
234260
}
235261

236262
if ($this->isPrunable()) {
237-
// Delete old file if exists.
238-
// return function () use ($model, $request) {
239263
call_user_func(
240264
$this->deleteCallback,
241265
$request,
242266
$model,
243267
$this->getStorageDisk(),
244268
$this->getStoragePath()
245269
);
246-
// };
247270
}
248271

249272
$result = call_user_func(
@@ -276,6 +299,20 @@ public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = n
276299
return $this;
277300
}
278301

302+
public function getStoringRules(): array
303+
{
304+
$rules = parent::getStoringRules();
305+
306+
// Modify validation to accept URLs
307+
foreach ($rules as &$rule) {
308+
if (is_string($rule) && Str::startsWith($rule, 'file')) {
309+
$rule = 'sometimes|'.$rule;
310+
}
311+
}
312+
313+
return $rules;
314+
}
315+
279316
/**
280317
* Get the full path that the field is stored at on disk.
281318
*

0 commit comments

Comments
 (0)