|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Zhiyi\Plus\FileStorage\Filesystems\Local; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use Exception; |
| 9 | +use Zhiyi\Plus\Models\User; |
| 10 | +use Zhiyi\Plus\FileStorage\ImageDimension; |
| 11 | +use Zhiyi\Plus\FileStorage\Pay\PayInterface; |
| 12 | +use Zhiyi\Plus\FileStorage\ResourceInterface; |
| 13 | +use Zhiyi\Plus\FileStorage\FileMetaInterface; |
| 14 | +use Zhiyi\Plus\FileStorage\Traits\HasImageTrait; |
| 15 | +use Zhiyi\Plus\FileStorage\ImageDimensionInterface; |
| 16 | +use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract; |
| 17 | + |
| 18 | +class FileMeta implements FileMetaInterface |
| 19 | +{ |
| 20 | + use HasImageTrait; |
| 21 | + |
| 22 | + protected $filesystem; |
| 23 | + protected $resource; |
| 24 | + protected $dimension; |
| 25 | + |
| 26 | + public function __construct(FilesystemContract $filesystem, ResourceInterface $resource) |
| 27 | + { |
| 28 | + $this->filesystem = $filesystem; |
| 29 | + $this->resource = $resource; |
| 30 | + } |
| 31 | + |
| 32 | + protected function useCustomTypes(): ?Closure |
| 33 | + { |
| 34 | + return function () { |
| 35 | + return [ |
| 36 | + 'image/jpeg', |
| 37 | + 'image/png', |
| 38 | + 'image/gif', |
| 39 | + 'image/bmp', |
| 40 | + 'image/webp', |
| 41 | + ]; |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Has the file is image. |
| 47 | + * @return bool |
| 48 | + */ |
| 49 | + public function hasImage(): bool |
| 50 | + { |
| 51 | + return $this->hasImageType( |
| 52 | + $this->getMimeType() |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get image file dimension. |
| 58 | + * @return \Zhiyi\Plus\FileStorage\ImageDimensionInterface |
| 59 | + */ |
| 60 | + public function getImageDimension(): ImageDimensionInterface |
| 61 | + { |
| 62 | + if (! $this->hasImage()) { |
| 63 | + throw new Exception('调用的资源并非图片或者是不支持的图片资源'); |
| 64 | + } elseif ($this->dimension instanceof ImageDimensionInterface) { |
| 65 | + return $this->dimension; |
| 66 | + }; |
| 67 | + |
| 68 | + $realPath = $this->filesystem->path( |
| 69 | + $this->resource->getPath() |
| 70 | + ); |
| 71 | + list($width, $height) = getimagesize($realPath); |
| 72 | + |
| 73 | + return $this->dimension = new ImageDimension((float) $width, (float) $height); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the file size (Byte). |
| 78 | + * @return int |
| 79 | + */ |
| 80 | + public function getSize(): int |
| 81 | + { |
| 82 | + return $this->filesystem->getSize( |
| 83 | + $this->resource->getPath() |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the resource mime type. |
| 89 | + * @return string |
| 90 | + */ |
| 91 | + public function getMimeType(): string |
| 92 | + { |
| 93 | + return $this->filesystem->mimeType( |
| 94 | + $this->resource->getPath() |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get the resource pay info. |
| 100 | + * @param \Zhiyi\Plus\Models\User $user |
| 101 | + * @return \Zhiyi\Plus\FileStorage\Pay\PayInterface |
| 102 | + */ |
| 103 | + public function getPay(User $user): ?PayInterface |
| 104 | + { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get the storage vendor name. |
| 110 | + * @return string |
| 111 | + */ |
| 112 | + public function getVendorName(): string |
| 113 | + { |
| 114 | + return 'local'; |
| 115 | + } |
| 116 | + |
| 117 | + public function url(): string |
| 118 | + { |
| 119 | + return route('storage:local-get', [ |
| 120 | + 'channel' => $this->resource->getChannel(), |
| 121 | + 'path' => base64_encode($this->resource->getPath()), |
| 122 | + ]); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get the instance as an array. |
| 127 | + * |
| 128 | + * @return array |
| 129 | + */ |
| 130 | + public function toArray(): array |
| 131 | + { |
| 132 | + return [1, 2]; |
| 133 | + } |
| 134 | +} |
0 commit comments