Skip to content

Commit 1064ba1

Browse files
committed
Merge branch 'main' into upload-playable-preview
2 parents 02b20a6 + 70d5072 commit 1064ba1

11 files changed

Lines changed: 198 additions & 9 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"ext-mbstring": "*",
2020
"blade-ui-kit/blade-icons": "^1.6",
2121
"code16/laravel-content-renderer": "^1.1",
22+
"enshrined/svg-sanitize": "^0.21.0",
2223
"inertiajs/inertia-laravel": "^2.0",
2324
"intervention/image": "^3.4",
2425
"laravel/framework": "^11.0|^12.0",

demo/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"bacon/bacon-qr-code": "~2.0",
77
"blade-ui-kit/blade-icons": "^1.6",
88
"code16/laravel-content-renderer": "^1.2",
9+
"enshrined/svg-sanitize": "^0.21.0",
910
"guzzlehttp/guzzle": "^7.2",
1011
"inertiajs/inertia-laravel": "^2.0",
1112
"intervention/image": "^3.4",

demo/composer.lock

Lines changed: 47 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Form/Fields/Formatters/UploadFormatter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function fromFront(SharpFormField $field, string $attribute, $value): ?ar
5555
disk: $field->storageDisk(),
5656
filePath: $formatted['file_name'],
5757
shouldOptimizeImage: $field->isImageOptimize(),
58+
shouldSanitizeSvg: $field->isImageSanitizeSvg(),
5859
transformFilters: $field->isImageTransformOriginal()
5960
? ($value['filters'] ?? null)
6061
: null,

src/Form/Fields/SharpFormUploadField.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SharpFormUploadField extends SharpFormField
2323
protected ?Dimensions $imageDimensionConstraints = null;
2424
protected bool $imageCompactThumbnail = false;
2525
protected bool $imageOptimize = false;
26+
protected bool $imageSanitizeSvg = true;
2627
protected ?array $imageCropRatio = null;
2728
protected ?array $imageTransformableFileTypes = null;
2829

@@ -93,6 +94,18 @@ public function isImageOptimize(): bool
9394
return $this->imageOptimize;
9495
}
9596

97+
public function setImageSanitizeSvg(bool $imageSanitizeSvg = true): self
98+
{
99+
$this->imageSanitizeSvg = $imageSanitizeSvg;
100+
101+
return $this;
102+
}
103+
104+
public function isImageSanitizeSvg(): bool
105+
{
106+
return $this->imageSanitizeSvg;
107+
}
108+
96109
public function setImageCompactThumbnail(bool $compactThumbnail = true): self
97110
{
98111
$this->imageCompactThumbnail = $compactThumbnail;

src/Http/Jobs/HandleUploadedFileJob.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function __construct(
2121
public string $disk,
2222
public string $filePath,
2323
public bool $shouldOptimizeImage = true,
24+
public bool $shouldSanitizeSvg = true,
2425
public ?array $transformFilters = null,
2526
public ?string $instanceId = null,
2627
) {}
@@ -45,9 +46,16 @@ public function handle(): void
4546
if ($this->transformFilters) {
4647
// There are transformation and field was configured to handle transformation on the source image
4748
HandleTransformedFileJob::dispatchSync(
48-
$tmpDisk,
49-
$tmpFilePath,
50-
$this->transformFilters
49+
disk: $tmpDisk,
50+
filePath: $tmpFilePath,
51+
transformFilters: $this->transformFilters
52+
);
53+
}
54+
55+
if ($this->shouldSanitizeSvg && Storage::disk($tmpDisk)->mimeType($tmpFilePath) === 'image/svg+xml') {
56+
SanitizeSvgJob::dispatchSync(
57+
disk: $tmpDisk,
58+
filePath: $tmpFilePath
5159
);
5260
}
5361

src/Http/Jobs/SanitizeSvgJob.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Http\Jobs;
4+
5+
use enshrined\svgSanitize\Sanitizer;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Bus\Dispatchable;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Support\Facades\Storage;
11+
12+
class SanitizeSvgJob implements ShouldQueue
13+
{
14+
use Dispatchable;
15+
use InteractsWithQueue;
16+
use Queueable;
17+
18+
public function __construct(
19+
public string $disk,
20+
public string $filePath,
21+
) {}
22+
23+
public function handle(): void
24+
{
25+
$sanitizer = new Sanitizer();
26+
$sanitizer->minify(true);
27+
$sanitizer->removeXMLTag(true);
28+
$sanitizedSvg = $sanitizer->sanitize(
29+
Storage::disk($this->disk)->get($this->filePath)
30+
);
31+
32+
Storage::disk($this->disk)
33+
->put($this->filePath, $sanitizedSvg);
34+
}
35+
}

src/Utils/Uploads/SharpUploadManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ public function queueHandleUploadedFile(
3030
string $disk,
3131
string $filePath,
3232
bool $shouldOptimizeImage = true,
33+
bool $shouldSanitizeSvg = true,
3334
?array $transformFilters = null,
3435
): void {
3536
$this->uploadedFileQueue[] = compact(
3637
'uploadedFileName',
3738
'disk',
3839
'filePath',
3940
'shouldOptimizeImage',
41+
'shouldSanitizeSvg',
4042
'transformFilters',
4143
);
4244
}

tests-e2e/site/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"ext-json": "*",
1111
"blade-ui-kit/blade-icons": "^1.7",
1212
"code16/laravel-content-renderer": "^1.2",
13+
"enshrined/svg-sanitize": "^0.21.0",
1314
"inertiajs/inertia-laravel": "^2.0",
1415
"intervention/image": "^3.9",
1516
"intervention/image-laravel": "^1.3",

tests-e2e/site/composer.lock

Lines changed: 46 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)