2021-09-09 19:18:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aweos\Resizer\Lib;
|
|
|
|
|
|
|
|
use Aweos\Resizer\Compressors\Compressor;
|
|
|
|
use Aweos\Resizer\Compressors\Factory as CompressorFactory;
|
|
|
|
use Aweos\Resizer\Models\Setting;
|
|
|
|
use Illuminate\Support\Collection;
|
2022-02-15 18:06:51 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2021-12-17 01:29:15 +01:00
|
|
|
use MediaLibrary;
|
2021-09-09 19:18:41 +02:00
|
|
|
|
2022-02-16 02:49:19 +01:00
|
|
|
abstract class MediaPath
|
2021-09-09 19:18:41 +02:00
|
|
|
{
|
|
|
|
|
2022-02-16 02:49:19 +01:00
|
|
|
protected string $path;
|
|
|
|
abstract public function get(): string;
|
|
|
|
abstract public function root(): string;
|
|
|
|
abstract public function publicUrl(): string;
|
2021-09-09 19:18:41 +02:00
|
|
|
|
|
|
|
public function __construct(string $path)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function normal(): string
|
|
|
|
{
|
|
|
|
return preg_replace('|^/*|', '', $this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function compressor(): Compressor
|
|
|
|
{
|
|
|
|
return app(CompressorFactory::class)->resolve($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function filename(): string
|
|
|
|
{
|
2022-02-16 02:49:19 +01:00
|
|
|
return pathinfo($this->root(), PATHINFO_FILENAME);
|
2021-09-09 19:18:41 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 21:13:55 +02:00
|
|
|
public function exists(): bool
|
|
|
|
{
|
|
|
|
return file_exists($this->root());
|
|
|
|
}
|
|
|
|
|
2021-09-17 17:09:17 +02:00
|
|
|
public function type(): ?string
|
2021-09-16 01:11:00 +02:00
|
|
|
{
|
2021-10-30 21:13:55 +02:00
|
|
|
if (!$this->exists()) {
|
2021-09-17 17:09:17 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-16 01:11:00 +02:00
|
|
|
return mime_content_type($this->root());
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:18:41 +02:00
|
|
|
public function extension(): string
|
|
|
|
{
|
2022-02-16 02:49:19 +01:00
|
|
|
return pathinfo($this->root(), PATHINFO_EXTENSION);
|
2021-09-09 19:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function versionsPath(): string
|
|
|
|
{
|
|
|
|
return "uploads/public/c/{$this->normal()}";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function versionsDirPath(): string
|
|
|
|
{
|
2022-02-16 02:49:19 +01:00
|
|
|
return pathinfo($this->versionsPath(), PATHINFO_DIRNAME);
|
2021-09-09 19:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function shouldProcess(): bool
|
|
|
|
{
|
|
|
|
return collect(Setting::get('folders'))->pluck('folder')->first(
|
|
|
|
fn ($folder) => starts_with('/'.$this->normal(), $folder.'/')
|
|
|
|
) !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function versions(): Collection
|
|
|
|
{
|
|
|
|
$return = collect([]);
|
|
|
|
|
2021-09-20 00:29:21 +02:00
|
|
|
$extensionRegex = $this->compressor()->getExtensionRegex();
|
|
|
|
|
2021-09-09 19:18:41 +02:00
|
|
|
foreach (Storage::files($this->versionsDirPath()) as $file) {
|
2021-09-20 00:29:21 +02:00
|
|
|
if (!preg_match_all('|('.preg_quote($this->filename(), '|').')(-[0-9]+x[0-9]+)?(\.'.$extensionRegex.'+)$|', $file, $matches)) {
|
2021-09-09 19:18:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($matches[2][0]) {
|
|
|
|
[$width, $height] = explode('x', substr($matches[2][0], 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = $this->versionsDirPath().'/'.$matches[0][0];
|
|
|
|
|
|
|
|
$return->push(collect([
|
|
|
|
'path' => $path,
|
|
|
|
'url' => Storage::url($path),
|
|
|
|
'filename' => $matches[0][0],
|
|
|
|
'base' => $matches[1][0],
|
|
|
|
'size' => $matches[2][0],
|
|
|
|
'ext' => $matches[3][0],
|
|
|
|
'width' => $width ?? null,
|
|
|
|
'height' => $height ?? null,
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2022-02-16 02:49:19 +01:00
|
|
|
protected function storagePath(): string
|
|
|
|
{
|
|
|
|
return "media/{$this->normal()}";
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:18:41 +02:00
|
|
|
}
|