1 | <?php |
||
2 | |||
3 | namespace Mostafaznv\PdfOptimizer\Laravel\Concerns; |
||
4 | |||
5 | |||
6 | class File |
||
7 | { |
||
8 | private string $path; |
||
9 | private Disk $diskInstance; |
||
10 | |||
11 | |||
12 | public function __construct(string $path, ?string $disk = null) |
||
13 | { |
||
14 | $this->path = $path; |
||
15 | $this->diskInstance = Disk::make($disk); |
||
16 | } |
||
17 | |||
18 | public function __destruct() |
||
19 | { |
||
20 | $this->cleanup(); |
||
21 | } |
||
22 | |||
23 | public static function make(string $path, ?string $disk = null): self |
||
24 | { |
||
25 | return new static($path, $disk); |
||
26 | } |
||
27 | |||
28 | |||
29 | public function getDisk(): Disk |
||
30 | { |
||
31 | return $this->diskInstance; |
||
32 | } |
||
33 | |||
34 | public function getPath(): string |
||
35 | { |
||
36 | return $this->path; |
||
37 | } |
||
38 | |||
39 | public function getLocalPath(): string |
||
40 | { |
||
41 | $path = $this->getPath(); |
||
42 | $disk = $this->getDisk()->getAdapter(); |
||
43 | |||
44 | if ($disk) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
45 | if ($this->getDisk()->isLocalDisk()) { |
||
46 | return $disk->path($path); |
||
47 | } |
||
48 | |||
49 | $temporaryDisk = $this->getDisk()->getTemporaryDisk(); |
||
50 | |||
51 | if (!$temporaryDisk->exists($path)) { |
||
52 | $temporaryDisk->writeStream($path, $disk->readStream($path)); |
||
53 | } |
||
54 | |||
55 | return $temporaryDisk->path($path); |
||
56 | } |
||
57 | |||
58 | return $path; |
||
59 | } |
||
60 | |||
61 | public function cleanup(): self |
||
62 | { |
||
63 | $this->getDisk()->cleanupTemporaryDirectory(); |
||
64 | |||
65 | return $this; |
||
66 | } |
||
67 | } |
||
68 |