Move to mediable model for storing files

This commit is contained in:
Ryan Scherler
2020-04-23 13:15:21 -07:00
parent 9710366fd6
commit 1c108698bd
9 changed files with 40 additions and 22 deletions
+2 -3
View File
@@ -169,13 +169,12 @@ return [
| Storage
|--------------------------------------------------------------------------
|
| This is the storage disk Filament will use to put file uploads, you may
| This is the storage disk Filament will use to put media, you may
| use any of the disks defined in the `config/filesystems.php`.
|
*/
'storage_disk' => env('FILAMENT_STORAGE_DISK', 'public'),
'storage_path' => env('FILAMENT_STORAGE_PATH', 'uploads'),
'storage_path' => env('FILAMENT_STORAGE_PATH', 'media'),
];
@@ -25,6 +25,7 @@ class CreateMediaTable extends Migration
$table->integer('mediable_id')->integer();
$table->integer('mediable_type')->string();
$table->string('value');
$table->timestamps();
});
}
+3 -3
View File
@@ -44,9 +44,9 @@
</div>
</div>
@include('filament::fields.error-help')
@if ($this->form_data[$field->name])
@if ($field->files)
<ul>
@foreach ($this->form_data[$field->name] as $key => $value)
@foreach ($field->files as $id => $value)
<li class="mt-2 p-2 text-sm leading-5 bg-gray-50 dark:bg-gray-700 rounded overflow-hidden shadow flex items-center justify-between">
<a href="{{ Storage::url($value['path']) }}"
target="_blank"
@@ -76,7 +76,7 @@
</dl>
</a>
<button type="button"
wire:click.prevent="fileRemove('{{ $field->name }}', '{{ $value['path'] }}', {{ $key }})"
wire:click.prevent="fileRemove('{{ $field->name }}', {{ $id }}, {{ $loop->index }})"
class="flex-shrink-0 flex items-center p-2"
>
<x-heroicon-o-x class="h-4 w-4 text-red-500" />
+3
View File
@@ -7,6 +7,7 @@ use Filament\Support\Fields\Field;
use Illuminate\Validation\Rule;
use Filament\Models\Role;
use Filament\Models\Permission;
use Filament\Models\Media;
class UserEditFieldset implements Fieldset
{
@@ -41,6 +42,8 @@ class UserEditFieldset implements Fieldset
->fileValidationMessages([
'image' => __('The Avatar must be a valid image.'),
])
->files(Media::whereIn('id', $model->avatar)->pluck('value', 'id')->all())
->multiple()
->group('account'),
Field::make('password', 'New password')
->input('password')
+7
View File
@@ -23,6 +23,13 @@ class Media extends Model
'value' => 'array',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['value'];
/**
* Get the owning mediable model.
*/
+8 -1
View File
@@ -20,9 +20,10 @@ class BaseField
protected $rules;
protected $required = false;
protected $view;
protected $file_multiple;
protected $file_multiple = false;
protected $file_rules = ['file'];
protected $file_validation_messages = ['file' => 'Must be a valid file.'];
protected $files = [];
protected $disabled = false;
protected $allowed = true;
@@ -78,6 +79,12 @@ class BaseField
return $this;
}
public function files($files)
{
$this->files = (array) $files;
return $this;
}
public function disabled($is_disabled = true)
{
$this->disabled = (bool) $is_disabled;
+1
View File
@@ -12,6 +12,7 @@ use Filament\Traits\Fields\FollowsRules;
use Filament\Traits\Fields\HandlesFiles;
use Filament\Traits\Fields\HandlesArrays;
use Filament\Contracts\Fieldset;
use Filament\Models\Media;
class FormComponent extends Component
{
+13 -13
View File
@@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Filament\Models\Media;
trait HandlesFiles
{
@@ -30,11 +31,10 @@ trait HandlesFiles
$files = [];
$storage_disk = config('filament.storage_disk');
$folder = Str::uuid();
foreach (request()->file('files') as $file) {
$files[] = [
'path' => $file->storeAs(config('filament.storage_path').'/'.$folder, $file->getClientOriginalName(), $storage_disk),
'path' => $file->storeAs(config('filament.storage_path').'/'.Str::uuid(), $file->getClientOriginalName(), $storage_disk),
'name' => $file->getClientOriginalName(),
'disk' => $storage_disk,
'size' => $file->getSize(),
@@ -57,9 +57,17 @@ trait HandlesFiles
public function fileUpdate($field_name, $uploaded_files)
{
$media = [];
foreach($uploaded_files as $file) {
$media[] = new Media(['value' => $file]);
}
$this->model->media()->saveMany($media);
$field = $this->getField($field_name);
if ($field) {
$value = $field->file_multiple ? array_merge($this->form_data[$field_name], $uploaded_files) : $uploaded_files;
$media_ids = collect($media)->pluck('id')->all();
$value = $field->file_multiple ? array_merge($this->form_data[$field_name], $media_ids) : $media_ids;
}
$this->form_data[$field_name] = $value ?? [];
@@ -68,17 +76,9 @@ trait HandlesFiles
$this->updated('form_data.'.$field_name);
}
public function fileRemove($field_name, $file_path, $key)
public function fileRemove($field_name, $id, $key)
{
$files = $this->form_data[$field_name];
foreach($files as $file) {
$storage = Storage::disk($file['disk']);
if ($storage->exists($file['path']) && $file['path'] === $file_path) {
$storage->deleteDirectory(dirname($file['path']));
break;
}
}
$this->model->media()->where('id', $id)->delete();
$this->arrayRemove($field_name, $key);
$this->saveField($field_name);
$this->updated('form_data.'.$field_name);
+2 -2
View File
@@ -38,8 +38,8 @@ trait FilamentUser
public function avatar($size = 48)
{
if ($this->avatar) {
$path = collect(Arr::first($this->avatar))->get('path');
return Filament::image($path, [
$value = collect(Media::find($this->avatar)->pluck('value')->first());
return Filament::image($value->get('path'), [
'w' => 48,
'h' => 48,
'fit' => 'crop',