57 lines
1.8 KiB
PHP
57 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Zoomyboy\MedialibraryHelper;
|
|
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Spatie\LaravelData\Attributes\MapInputName;
|
|
use Spatie\LaravelData\Attributes\MapOutputName;
|
|
use Spatie\LaravelData\Data;
|
|
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
|
use Illuminate\Support\Collection;
|
|
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
|
use Spatie\LaravelData\DataCollection;
|
|
use Spatie\LaravelData\Lazy;
|
|
|
|
#[MapInputName(SnakeCaseMapper::class)]
|
|
#[MapOutputName(SnakeCaseMapper::class)]
|
|
class DeferredMediaData extends Data
|
|
{
|
|
public string $collectionName;
|
|
|
|
public Lazy|string $path;
|
|
|
|
public static function fromPath(string $path, MediaCollection $collection): self
|
|
{
|
|
return self::factory()->withoutMagicalCreation()->from([
|
|
'path' => Lazy::create(fn () => $path),
|
|
'collection_name' => $collection->name,
|
|
]);
|
|
}
|
|
|
|
public static function collectionFromPaths(Collection $paths, MediaCollection $collection): DataCollection
|
|
{
|
|
return static::collect($paths->map(fn ($path) => static::fromPath($path, $collection)));
|
|
}
|
|
|
|
public function with(): array
|
|
{
|
|
$file = new MediaFile(Storage::disk(config('media-library.temp_disk'))->path($this->path->resolve()));
|
|
|
|
return [
|
|
'is_deferred' => true,
|
|
'original_url' => $this->storage()->url($this->path->resolve()),
|
|
'name' => $file->getBasename(),
|
|
'size' => $file->getSize(),
|
|
'file_name' => $file->getFilename(),
|
|
'mime_type' => $file->getMimeType(),
|
|
'icon' => Storage::disk('public')->url('filetypes/' . str()->slug($file->getMimeType()) . '.svg'),
|
|
];
|
|
}
|
|
|
|
protected function storage(): FilesystemAdapter
|
|
{
|
|
return Storage::disk(config('media-library.temp_disk'));
|
|
}
|
|
}
|