mostafaznv /
pdf-optimizer
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Mostafaznv\PdfOptimizer\Laravel\Concerns; |
||
| 4 | |||
| 5 | use Illuminate\Filesystem\Filesystem; |
||
| 6 | use Illuminate\Filesystem\FilesystemAdapter; |
||
| 7 | use Illuminate\Support\Str; |
||
| 8 | //use League\Flysystem\Local\LocalFilesystemAdapter; |
||
| 9 | |||
| 10 | |||
| 11 | class Disk |
||
| 12 | { |
||
| 13 | private ?string $disk; |
||
| 14 | private string $temporaryDirectory; |
||
| 15 | |||
| 16 | |||
| 17 | public function __construct(?string $disk = null) |
||
| 18 | { |
||
| 19 | $this->disk = $disk; |
||
| 20 | $this->temporaryDirectory = pdf_temp_dir() . '/pdf-optimizer/' . Str::uuid()->toString(); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function __destruct() |
||
| 24 | { |
||
| 25 | $this->cleanupTemporaryDirectory(); |
||
| 26 | } |
||
| 27 | |||
| 28 | public static function make(?string $disk = null): self |
||
| 29 | { |
||
| 30 | return new static($disk); |
||
| 31 | } |
||
| 32 | |||
| 33 | |||
| 34 | public function getAdapter(): ?FilesystemAdapter |
||
| 35 | { |
||
| 36 | return $this->disk ? app('filesystem')->disk($this->disk) : null; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 37 | } |
||
| 38 | |||
| 39 | public function getTemporaryDisk(): FilesystemAdapter |
||
| 40 | { |
||
| 41 | if (!is_dir($this->temporaryDirectory)) { |
||
| 42 | mkdir($this->temporaryDirectory, 0777, true); |
||
| 43 | } |
||
| 44 | |||
| 45 | return app('filesystem')->createLocalDriver([ |
||
| 46 | 'root' => $this->temporaryDirectory, |
||
| 47 | ]); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function cleanupTemporaryDirectory(): self |
||
| 51 | { |
||
| 52 | $filesystem = new Filesystem(); |
||
| 53 | $filesystem->deleteDirectory($this->temporaryDirectory); |
||
| 54 | |||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function isLocalDisk(): bool |
||
| 59 | { |
||
| 60 | if ($this->getAdapter()) { |
||
| 61 | // $this->getAdapter() instanceof LocalFilesystemAdapter |
||
| 62 | return config("filesystems.disks.$this->disk.driver") == 'local'; |
||
| 63 | } |
||
| 64 | |||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | } |
||
| 69 |