|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mostafaznv\PdfOptimizer\Laravel\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Mostafaznv\PdfOptimizer\Actions\OptimizePdfAction; |
|
6
|
|
|
use Mostafaznv\PdfOptimizer\Concerns\ExportsScript; |
|
7
|
|
|
use Mostafaznv\PdfOptimizer\Concerns\PdfOptimizerProperties; |
|
8
|
|
|
use Mostafaznv\PdfOptimizer\DTOs\OptimizeResult; |
|
9
|
|
|
use Mostafaznv\PdfOptimizer\DTOs\PdfOptimizerJobData; |
|
10
|
|
|
use Mostafaznv\PdfOptimizer\DTOs\QueueData; |
|
11
|
|
|
use Mostafaznv\PdfOptimizer\Jobs\PdfOptimizerJob; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class Export |
|
16
|
|
|
{ |
|
17
|
|
|
use PdfOptimizerProperties, ExportsScript; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
private readonly File $file; |
|
20
|
|
|
private string $gsBinary; |
|
21
|
|
|
private ?string $disk = null; |
|
22
|
|
|
private ?LoggerInterface $logger = null; |
|
23
|
|
|
private QueueData $queue; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public function __construct(File $file, string $gsBinary = 'gs') |
|
27
|
|
|
{ |
|
28
|
|
|
$this->file = $file; |
|
|
|
|
|
|
29
|
|
|
$this->gsBinary = $gsBinary; |
|
30
|
|
|
$this->timeout = config('pdf-optimizer.timeout'); |
|
31
|
|
|
|
|
32
|
|
|
$queue = config('pdf-optimizer.queue'); |
|
33
|
|
|
|
|
34
|
|
|
$this->queue = QueueData::make( |
|
35
|
|
|
$queue['enabled'], $queue['name'], $queue['connection'], $queue['timeout'] |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
public function toDisk(string $disk): self |
|
41
|
|
|
{ |
|
42
|
|
|
$this->disk = $disk; |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function setGsBinary(string $binary): self |
|
48
|
|
|
{ |
|
49
|
|
|
$this->gsBinary = $binary; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function onQueue(bool $enabled = true, string $name = 'default', ?string $connection = null, int $timeout = 900): self |
|
55
|
|
|
{ |
|
56
|
|
|
$this->queue = QueueData::make($enabled, $name, $connection, $timeout); |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function logger(LoggerInterface $logger): self |
|
62
|
|
|
{ |
|
63
|
|
|
$this->logger = $logger; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function optimize(string $pathToOptimizedFile): OptimizeResult |
|
69
|
|
|
{ |
|
70
|
|
|
$disk = Disk::make($this->disk); |
|
71
|
|
|
|
|
72
|
|
|
$commands = $this->command(); |
|
73
|
|
|
$input = $this->file->getPath(); |
|
74
|
|
|
|
|
75
|
|
|
if ($this->queue->enabled) { |
|
76
|
|
|
return $this->optimizeOnQueue( |
|
77
|
|
|
$commands, $input, $pathToOptimizedFile, $disk |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return OptimizePdfAction::init($this->file, $disk) |
|
82
|
|
|
->logger($this->logger) |
|
83
|
|
|
->setTimeout($this->timeout) |
|
84
|
|
|
->execute( |
|
85
|
|
|
$commands, $input, $pathToOptimizedFile |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
private function optimizeOnQueue(array $commands, string $input, string $output, Disk $disk): OptimizeResult |
|
91
|
|
|
{ |
|
92
|
|
|
$data = PdfOptimizerJobData::make( |
|
93
|
|
|
commands: $commands, |
|
94
|
|
|
input: $input, |
|
95
|
|
|
output: $output, |
|
96
|
|
|
file: $this->file, |
|
97
|
|
|
disk: $disk, |
|
98
|
|
|
logger: $this->logger, |
|
99
|
|
|
timeout: $this->queue->timeout |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
PdfOptimizerJob::dispatch($data) |
|
103
|
|
|
->onQueue($this->queue->name) |
|
104
|
|
|
->onConnection($this->queue->connection); |
|
105
|
|
|
|
|
106
|
|
|
return OptimizeResult::make(true, true, $data->id); |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|