This commit is contained in:
philipp lang 2024-01-11 23:43:19 +01:00
parent 7c33160b13
commit 5c05ab7c80
1 changed files with 23 additions and 22 deletions

View File

@ -2,6 +2,7 @@
namespace Zoomyboy\MedialibraryHelper; namespace Zoomyboy\MedialibraryHelper;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Spatie\LaravelData\Attributes\MapInputName; use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\MapOutputName; use Spatie\LaravelData\Attributes\MapOutputName;
@ -10,40 +11,21 @@ use Spatie\LaravelData\Mappers\SnakeCaseMapper;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Spatie\MediaLibrary\MediaCollections\MediaCollection; use Spatie\MediaLibrary\MediaCollections\MediaCollection;
use Spatie\LaravelData\DataCollection; use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\Lazy;
#[MapInputName(SnakeCaseMapper::class)] #[MapInputName(SnakeCaseMapper::class)]
#[MapOutputName(SnakeCaseMapper::class)] #[MapOutputName(SnakeCaseMapper::class)]
class DeferredMediaData extends Data class DeferredMediaData extends Data
{ {
public ?int $id;
public string $originalUrl;
public int $size;
public string $name;
public string $collectionName; public string $collectionName;
public string $fileName; public Lazy|string $path;
public string $mimeType;
public bool $fallback = false;
public bool $isDeferred = true;
public static function fromPath(string $path, MediaCollection $collection): self public static function fromPath(string $path, MediaCollection $collection): self
{ {
$file = new MediaFile(Storage::disk(config('media-library.temp_disk'))->path($path));
return static::withoutMagicalCreationFrom([ return static::withoutMagicalCreationFrom([
'path' => Lazy::create(fn () => $path),
'collection_name' => $collection->name, 'collection_name' => $collection->name,
'original_url' => Storage::disk(config('media-library.temp_disk'))->url($path),
'size' => $file->getSize(),
'name' => $file->getBasename(),
'file_name' => $file->getFilename(),
'mime_type' => Storage::disk(config('media-library.temp_disk'))->mimeType($path),
'path' => $path,
]); ]);
} }
@ -51,4 +33,23 @@ class DeferredMediaData extends Data
{ {
return static::collection($paths->map(fn ($path) => static::fromPath($path, $collection))); return static::collection($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(),
];
}
protected function storage(): FilesystemAdapter
{
return Storage::disk(config('media-library.temp_disk'));
}
} }