-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathHandleUploadedFileJob.php
More file actions
76 lines (64 loc) · 2.32 KB
/
Copy pathHandleUploadedFileJob.php
File metadata and controls
76 lines (64 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Code16\Sharp\Http\Jobs;
use Code16\Sharp\Exceptions\Form\SharpFormUpdateException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Storage;
class HandleUploadedFileJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
public function __construct(
public string $uploadedFileName,
public string $disk,
public string $filePath,
public bool $shouldOptimizeImage = true,
public bool $shouldSanitizeSvg = true,
public ?array $transformFilters = null,
public ?string $instanceId = null,
) {}
public function handle(): void
{
$tmpDisk = sharp()->config()->get('uploads.tmp_disk');
$tmpFilePath = sprintf(
'%s/%s',
sharp()->config()->get('uploads.tmp_dir'),
$this->uploadedFileName,
);
if ($this->shouldOptimizeImage) {
OptimizeImageJob::dispatchSync(
disk: $tmpDisk,
filePath: $tmpFilePath,
);
}
if ($this->transformFilters) {
// There are transformation and field was configured to handle transformation on the source image
HandleTransformedFileJob::dispatchSync(
disk: $tmpDisk,
filePath: $tmpFilePath,
transformFilters: $this->transformFilters,
);
}
if ($this->shouldSanitizeSvg && Storage::disk($tmpDisk)->mimeType($tmpFilePath) === 'image/svg+xml') {
SanitizeSvgJob::dispatchSync(
disk: $tmpDisk,
filePath: $tmpFilePath,
);
}
Storage::disk($this->disk)
->put($this->determineFilePath(), Storage::disk($tmpDisk)->get($tmpFilePath));
}
private function determineFilePath(): string
{
if (str_contains($this->filePath, '{id}')) {
if ($this->instanceId === null) {
throw new SharpFormUpdateException('Instance ID is required but not provided for file path template containing {id}');
}
return str_replace('{id}', $this->instanceId, $this->filePath);
}
return $this->filePath;
}
}