Skip to content

Commit

Permalink
initial work on directories
Browse files Browse the repository at this point in the history
  • Loading branch information
awcodes committed Jan 8, 2024
1 parent af0bb12 commit 6fc2ed2
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 69 deletions.
2 changes: 1 addition & 1 deletion config/curator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'curation_presets' => [
\Awcodes\Curator\Curations\ThumbnailPreset::class,
],
'directory' => 'media',
'directory' => null,
'disk' => env('FILAMENT_FILESYSTEM_DISK', 'public'),
'glide' => [
'server' => \Awcodes\Curator\Glide\DefaultServerFactory::class,
Expand Down
195 changes: 172 additions & 23 deletions database/factories/MediaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,90 @@
use Awcodes\Curator\Models\Media;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use \Illuminate\Http\UploadedFile;

class MediaFactory extends Factory
{
protected $model = Media::class;

protected ?string $directory = null;

protected ?string $disk = null;

protected ?string $type = null;

public function definition(): array
{
$fileName = collect([
return match ($this->getType()) {
'svg' => $this->handleSvg(),
'pdf' => $this->handlePdf(),
'video' => $this->handleVideo(),
default => $this->handleJpg(),
};
}

public function private(): MediaFactory
{
return $this->state(function (array $attributes) {
return [
'visibility' => 'private',
];
});
}

public function randomTimestamps(): MediaFactory
{
return $this->state(function (array $attributes) {
return [
'created_at' => \Carbon\Carbon::now()->addDays(rand(-800, 0))->addMinutes(rand(0,
60 * 23))->addSeconds(rand(0, 60)),
'updated_at' => \Carbon\Carbon::now()->addDays(rand(-799, 0))->addMinutes(rand(0,
60 * 23))->addSeconds(rand(0, 60))
];
});
}

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

return $this;
}

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

return $this;
}

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

return $this;
}

public function getDirectory(): ?string
{
return $this->directory ?? config('curator.directory');
}

public function getDisk(): ?string
{
return $this->disk ?? config('curator.disk');
}

public function getType(): string
{
return $this->type ?? 'jpg';
}

public function handleJpg(): array
{
$filename = collect([
'alberto-restifo-Ni4NgA64TFQ-unsplash',
'blake-verdoorn-cssvEZacHvQ-unsplash',
'daniel-roe-lpjb_UMOyx8-unsplash',
Expand All @@ -38,19 +113,19 @@ public function definition(): array
'tim-swaan-eOpewngf68w-unsplash',
])->random() . '.jpg';

$directory = config('curator.directory');
$disk = config('curator.disk');
$disk = $this->getDisk();
$directory = $this->getDirectory();

if (!Storage::disk($disk)->exists($directory . '/' . $fileName)) {
$fileContents = file_get_contents('https://res.cloudinary.com/aw-codes/image/upload/curator/seed-data/' . $fileName);
Storage::disk($disk)->put($directory . '/' . $fileName, $fileContents);
if (!Storage::disk($disk)->exists($directory . '/' . $filename)) {
$fileContents = file_get_contents('https://res.cloudinary.com/aw-codes/image/upload/curator/seed-data/' . $filename);
Storage::disk($disk)->put($directory . '/' . $filename, $fileContents);
}

$data = Image::make(Storage::disk($disk)->path($directory . '/' . $fileName));
$data = Image::make(Storage::disk($disk)->path($directory . '/' . $filename));

return [
'name' => $data->filename,
'path' => $directory . '/' . $fileName,
'path' => $directory ? $directory . '/' . $filename : $filename,
'ext' => $data->extension,
'type' => $data->mime(),
'alt' => $this->faker->words(rand(3, 8), true),
Expand All @@ -66,24 +141,98 @@ public function definition(): array
];
}

public function private(): MediaFactory
public function handleSvg(): array
{
return $this->state(function (array $attributes) {
return [
'visibility' => 'private',
];
});
$filename = collect([
'awcodes-logo',
'search',
'screen',
'brand',
'apps',
'link',
])->random() . '.svg';

$disk = $this->getDisk();
$directory = $this->getDirectory();

if (!Storage::disk($disk)->exists($directory . '/' . $filename)) {
$fileContents = file_get_contents('https://res.cloudinary.com/aw-codes/image/upload/curator/seed-data/' . $filename);
Storage::disk($disk)->put($directory . '/' . $filename, $fileContents);
}

return [
'name' => Str::of($filename)->before('.svg')->toString(),
'path' => $directory ? $directory . '/' . $filename : $filename,
'ext' => 'svg',
'type' => Storage::disk($disk)->mimeType($directory . '/' . $filename),
'alt' => $this->faker->words(rand(3, 8), true),
'title' => null,
'caption' => null,
'description' => null,
'width' => null,
'height' => null,
'disk' => $disk,
'directory' => $directory,
'size' => Storage::disk($disk)->size($directory . '/' . $filename) ?? null,
'visibility' => 'public',
];
}

public function randomTimestamps(): MediaFactory
public function handlePdf(): array
{
return $this->state(function (array $attributes) {
return [
'created_at' => \Carbon\Carbon::now()->addDays(rand(-800, 0))->addMinutes(rand(0,
60 * 23))->addSeconds(rand(0, 60)),
'updated_at' => \Carbon\Carbon::now()->addDays(rand(-799, 0))->addMinutes(rand(0,
60 * 23))->addSeconds(rand(0, 60))
];
});
$filename = Str::uuid() . '.pdf';
$filesize = mt_rand(1000, 2000);
$disk = $this->getDisk();
$directory = $this->getDirectory();

UploadedFile::fake()->create($filename, $filesize, 'application/pdf')->storeAs($directory . '/' . $filename, [
'disk' => $disk,
]);

return [
'name' => Str::of($filename)->before('.pdf')->toString(),
'path' => $directory ? $directory . '/' . $filename : $filename,
'ext' => 'pdf',
'type' => 'application/pdf',
'alt' => $this->faker->words(rand(3, 8), true),
'title' => null,
'caption' => null,
'description' => null,
'width' => null,
'height' => null,
'disk' => $disk,
'directory' => $directory,
'size' => $filesize,
'visibility' => 'public',
];
}

public function handleVideo(): array
{
$filename = Str::uuid() . '.mp4';
$filesize = mt_rand(1000, 2000);
$disk = $this->getDisk();
$directory = $this->getDirectory();

UploadedFile::fake()->create($filename, $filesize, 'video/mp4')->storeAs($directory . '/' . $filename, [
'disk' => $disk,
]);

return [
'name' => Str::of($filename)->before('.pdf')->toString(),
'path' => $directory ? $directory . '/' . $filename : $filename,
'ext' => 'mp4',
'type' => 'video/mp4',
'alt' => $this->faker->words(rand(3, 8), true),
'title' => null,
'caption' => null,
'description' => null,
'width' => null,
'height' => null,
'disk' => $disk,
'directory' => $directory,
'size' => $filesize,
'visibility' => 'public',
];
}
}
2 changes: 1 addition & 1 deletion database/migrations/create_media_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ return new class extends Migration
Schema::create(app(config('curator.model'))->getTable(), function (Blueprint $table) {
$table->id();
$table->string('disk')->default('public');
$table->string('directory')->default('media');
$table->string('directory')->nullable();
$table->string('visibility')->default('public');
$table->string('name');
$table->string('path');
Expand Down
23 changes: 0 additions & 23 deletions database/migrations/upgrade_media_table.php.stub

This file was deleted.

16 changes: 8 additions & 8 deletions resources/css/plugin.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
}
}

.curator-picker-grid {
@apply grid grid-cols-3 gap-4 sm:grid-cols-4 md:grid-cols-6 xl:grid-cols-8 2xl:grid-cols-10;
}

.filepond--panel {
@apply bg-transparent border-dashed border-2 border-gray-950/5 dark:border-white/20;
}

[wire\:key*="open_curator_picker"],
[wire\:key*="open_curation_panel"],
.curator-panel {
Expand Down Expand Up @@ -99,13 +107,5 @@
> .fi-modal-footer {
@apply pb-0;
}

.curator-picker-grid {
@apply grid grid-cols-3 gap-4 sm:grid-cols-4 md:grid-cols-6 xl:grid-cols-8 2xl:grid-cols-10;
}

.filepond--panel {
@apply bg-transparent border-dashed border-2 border-gray-950/5 dark:border-white/20;
}
}
}
Loading

0 comments on commit 6fc2ed2

Please sign in to comment.