Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
37 / 37 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| MediaFile | |
100.00% |
37 / 37 |
|
100.00% |
9 / 9 |
13 | |
100.00% |
1 / 1 |
| all | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| files | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getContentLength | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getMimeType | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
| discoverMediaAssetFiles | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getMediaAssetFiles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMediaFileExtensions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Support\Filesystem; |
| 6 | |
| 7 | use Hyde\Facades\Filesystem; |
| 8 | use Hyde\Hyde; |
| 9 | use Hyde\Facades\Config; |
| 10 | use Hyde\Framework\Exceptions\FileNotFoundException; |
| 11 | use Illuminate\Support\Str; |
| 12 | |
| 13 | use function extension_loaded; |
| 14 | use function file_exists; |
| 15 | use function array_merge; |
| 16 | use function array_keys; |
| 17 | use function filesize; |
| 18 | use function pathinfo; |
| 19 | use function collect; |
| 20 | use function is_file; |
| 21 | |
| 22 | /** |
| 23 | * File abstraction for a project media file. |
| 24 | */ |
| 25 | class MediaFile extends ProjectFile |
| 26 | { |
| 27 | /** @var array<string> The default extensions for media types */ |
| 28 | final public const EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'webp', 'gif', 'ico', 'css', 'js']; |
| 29 | |
| 30 | /** @return array<string, \Hyde\Support\Filesystem\MediaFile> The array keys are the filenames relative to the _media/ directory */ |
| 31 | public static function all(): array |
| 32 | { |
| 33 | return static::discoverMediaAssetFiles(); |
| 34 | } |
| 35 | |
| 36 | /** @return array<string> Array of filenames relative to the _media/ directory */ |
| 37 | public static function files(): array |
| 38 | { |
| 39 | return array_keys(static::all()); |
| 40 | } |
| 41 | |
| 42 | public function getIdentifier(): string |
| 43 | { |
| 44 | return Str::after($this->getPath(), Hyde::getMediaDirectory().'/'); |
| 45 | } |
| 46 | |
| 47 | public function toArray(): array |
| 48 | { |
| 49 | return array_merge(parent::toArray(), [ |
| 50 | 'length' => $this->getContentLength(), |
| 51 | 'mimeType' => $this->getMimeType(), |
| 52 | ]); |
| 53 | } |
| 54 | |
| 55 | public function getContentLength(): int |
| 56 | { |
| 57 | if (! is_file($this->getAbsolutePath())) { |
| 58 | throw new FileNotFoundException($this->path); |
| 59 | } |
| 60 | |
| 61 | return filesize($this->getAbsolutePath()); |
| 62 | } |
| 63 | |
| 64 | public function getMimeType(): string |
| 65 | { |
| 66 | $extension = pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION); |
| 67 | |
| 68 | // See if we can find a mime type for the extension instead of |
| 69 | // having to rely on a PHP extension and filesystem lookups. |
| 70 | $lookup = [ |
| 71 | 'txt' => 'text/plain', |
| 72 | 'md' => 'text/markdown', |
| 73 | 'html' => 'text/html', |
| 74 | 'css' => 'text/css', |
| 75 | 'svg' => 'image/svg+xml', |
| 76 | 'png' => 'image/png', |
| 77 | 'jpg' => 'image/jpeg', |
| 78 | 'jpeg' => 'image/jpeg', |
| 79 | 'gif' => 'image/gif', |
| 80 | 'json' => 'application/json', |
| 81 | 'js' => 'application/javascript', |
| 82 | 'xml' => 'application/xml', |
| 83 | ]; |
| 84 | |
| 85 | if (isset($lookup[$extension])) { |
| 86 | return $lookup[$extension]; |
| 87 | } |
| 88 | |
| 89 | if (extension_loaded('fileinfo') && file_exists($this->getAbsolutePath())) { |
| 90 | return mime_content_type($this->getAbsolutePath()); |
| 91 | } |
| 92 | |
| 93 | return 'text/plain'; |
| 94 | } |
| 95 | |
| 96 | protected static function discoverMediaAssetFiles(): array |
| 97 | { |
| 98 | return collect(static::getMediaAssetFiles())->mapWithKeys(function (string $path): array { |
| 99 | $file = static::make($path); |
| 100 | |
| 101 | return [$file->getIdentifier() => $file]; |
| 102 | })->all(); |
| 103 | } |
| 104 | |
| 105 | /** @return array<string> */ |
| 106 | protected static function getMediaAssetFiles(): array |
| 107 | { |
| 108 | return Filesystem::findFiles(Hyde::getMediaDirectory(), static::getMediaFileExtensions(), true)->all(); |
| 109 | } |
| 110 | |
| 111 | /** @return array<string>|string */ |
| 112 | protected static function getMediaFileExtensions(): array|string |
| 113 | { |
| 114 | /** @var array<string>|string $config */ |
| 115 | $config = Config::get('hyde.media_extensions', self::EXTENSIONS); |
| 116 | |
| 117 | return $config; |
| 118 | } |
| 119 | } |