Skip to content

Commit

Permalink
Media model and resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Nov 24, 2024
1 parent 09d9ca4 commit 87713df
Show file tree
Hide file tree
Showing 9 changed files with 430 additions and 23 deletions.
29 changes: 26 additions & 3 deletions config/media-picker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
<?php

// config for Vormkracht10/MediaPicker
return [

];
'accepted_file_types' => [
'image/jpeg',
'image/png',
'image/webp',
'image/svg+xml',
'application/pdf',
],
'directory' => 'media',
'disk' => env('FILAMENT_FILESYSTEM_DISK', 'public'),
'is_tenant_aware' => true,
'tenant_ownership_relationship_name' => 'tenant',
'model' => \Vormkracht10\MediaPicker\Models\Media::class,
'resources' => [
'label' => 'Media',
'plural_label' => 'Media',
'navigation_group' => null,
'navigation_label' => 'Media',
'navigation_icon' => 'heroicon-o-photo',
'navigation_sort' => null,
'navigation_count_badge' => false,
// 'resource' => \Awcodes\Curator\Resources\MediaResource::class,
],
'should_preserve_filenames' => false,
'should_register_navigation' => true,
'visibility' => 'public',
];
19 changes: 0 additions & 19 deletions database/migrations/create_media_picker_table.php.stub

This file was deleted.

32 changes: 32 additions & 0 deletions database/migrations/create_media_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::create(app(config('media-picker.model'))->getTable(), function (Blueprint $table) {
$table->id();
$table->string('disk')->default('public');
$table->string('filename');
$table->string('uuid);
$table->string('extension);
$table->string('mime);
$table->integer('size');
$table-integer('width);
$table->integer('height);
table->string('checksum');
$table->boolean('public');
$table->timestamps();
$table->softDeletes();
});
}

public function down(): void
{
Schema::dropIfExists(app(config('media-picker.model'))->getTable());
}
};
135 changes: 134 additions & 1 deletion src/MediaPickerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,36 @@

namespace Vormkracht10\MediaPicker;

use Closure;

use Filament\Contracts\Plugin;
use Filament\Panel;
use Filament\Support\Concerns\EvaluatesClosures;

class MediaPickerPlugin implements Plugin
{
use EvaluatesClosures;

protected string | Closure | null $label = null;

protected string | Closure | null $navigationGroup = null;

protected string | Closure | null $navigationLabel = null;

protected string | Closure | null $navigationIcon = null;

protected int | Closure | null $navigationSort = null;

protected bool | Closure | null $navigationCountBadge = null;

protected bool | Closure | null $shouldRegisterNavigation = null;

protected string | Closure | null $defaultListView = null;

protected string | Closure | null $pluralLabel = null;

protected ?string $resource = null;

public function getId(): string
{
return 'filament-media-picker';
Expand Down Expand Up @@ -34,4 +59,112 @@ public static function get(): static

return $plugin;
}
}

public function getResource(): string
{
return $this->resource ?? config('media-picker.resources.resource');
}

public function getLabel(): string
{
return $this->evaluate($this->label) ?? config('media-picker.resources.label');
}

public function getPluralLabel(): string
{
return $this->evaluate($this->pluralLabel) ?? config('media-picker.resources.plural_label');
}

public function getNavigationGroup(): ?string
{
return $this->evaluate($this->navigationGroup) ?? config('media-picker.resources.navigation_group');
}

public function getNavigationLabel(): ?string
{
return $this->evaluate($this->navigationLabel) ?? config('media-picker.resources.navigation_label');
}

public function getNavigationIcon(): ?string
{
return $this->evaluate($this->navigationIcon) ?? config('media-picker.resources.navigation_icon');
}

public function getNavigationSort(): ?int
{
return $this->evaluate($this->navigationSort) ?? config('media-picker.resources.navigation_sort');
}

public function getNavigationCountBadge(): ?bool
{
return $this->navigationCountBadge ?? config('media-picker.resources.navigation_count_badge');
}

public function shouldRegisterNavigation(): ?bool
{
return $this->evaluate($this->shouldRegisterNavigation) ?? config('media-picker.should_register_navigation');
}

public function navigationGroup(string | Closure | null $group = null): static
{
$this->navigationGroup = $group;

return $this;
}

public function navigationLabel(string | Closure | null $label = null): static
{
$this->navigationLabel = $label;

return $this;
}

public function navigationIcon(string | Closure $icon): static
{
$this->navigationIcon = $icon;

return $this;
}

public function navigationSort(int | Closure $order): static
{
$this->navigationSort = $order;

return $this;
}

public function navigationCountBadge(bool $show = true): static
{
$this->navigationCountBadge = $show;

return $this;
}

public function registerNavigation(bool | Closure $condition = true): static
{
$this->shouldRegisterNavigation = $condition;

return $this;
}

public function pluralLabel(string | Closure $label): static
{
$this->pluralLabel = $label;

return $this;
}

public function resource(string $resource): static
{
$this->resource = $resource;

return $this;
}

public function label(string | Closure $label): static
{
$this->label = $label;

return $this;
}
}
47 changes: 47 additions & 0 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Vormkracht10\MediaPicker\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\Casts\Attribute;

class Media extends Model
{
protected $guarded = [];

protected $casts = [
'width' => 'integer',
'height' => 'integer',
'size' => 'integer',
];

protected function url(): Attribute
{
return Attribute::make(
get: function () {
if (Storage::disk($this->disk)->exists($this->path) === false) {

Check failure on line 23 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$disk.

Check failure on line 23 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$path.
return '';
}

try {
$isPrivate = Storage::disk($this->disk)->getVisibility($this->path) === 'private';

Check failure on line 28 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$disk.

Check failure on line 28 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$path.
} catch (\Throwable) {
$isPrivate = config(sprintf('filesystems.disks.%s.visibility', $this->disk)) !== 'public';

Check failure on line 30 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$disk.
}

return $isPrivate ? Storage::disk($this->disk)->temporaryUrl(

Check failure on line 33 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$disk.
$this->path,

Check failure on line 34 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$path.
now()->addMinutes(5)
) : Storage::disk($this->disk)->url($this->path);

Check failure on line 36 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$disk.

Check failure on line 36 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$path.
},
);
}

protected function fullPath(): Attribute
{
return Attribute::make(
get: fn() => Storage::disk($this->disk)->path($this->directory . '/' . $this->name . '.' . $this->extension),

Check failure on line 44 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Vormkracht10\MediaPicker\Models\Media::$directory.
);
}
}
Loading

0 comments on commit 87713df

Please sign in to comment.