Skip to content

Commit 8b3c2d8

Browse files
author
roadiz-ci
committed
Merge branch hotfix/v2.6.14
1 parent dae0ed4 commit 8b3c2d8

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

src/DownloadedFile.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public static function sanitizeFilename(?string $string): string
4040
return '';
4141
}
4242

43+
// Remove images double extensions
44+
// for compatibility with intervention-request
45+
// and only keep the last one.
46+
// example: my.image.jpg.webp => my_image_jpg.webp
47+
$parts = explode('.', $string);
48+
if (count($parts) > 2) {
49+
$extension = array_pop($parts);
50+
// Keep double extension for zip, gz, xz and bz
51+
if (!\in_array($extension, ['zip', 'gz', 'xz', 'bz', 'bz2', '7z', 'tgz'], true)) {
52+
$filename = implode('_', $parts);
53+
$string = $filename.'.'.$extension;
54+
}
55+
}
56+
4357
return (new UnicodeString($string))
4458
->ascii()
4559
->trim()

tests/DownloadedFileTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RZ\Roadiz\Documents\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use RZ\Roadiz\Documents\DownloadedFile;
9+
10+
class DownloadedFileTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider sanitizeFilenameProvider
14+
*/
15+
public function testSanitizeFilename(string $input, string $expected): void
16+
{
17+
$this->assertEquals($expected, DownloadedFile::sanitizeFilename($input));
18+
}
19+
20+
public function sanitizeFilenameProvider(): array
21+
{
22+
return [
23+
[
24+
'Les-Echos_26022015_Les-entrepreneurs-partent-à-lassaut-du-secteur-bancaire.pdf',
25+
'les_echos_26022015_les_entrepreneurs_partent_a_lassaut_du_secteur_bancaire.pdf',
26+
],
27+
[
28+
'Les-entrepreneurs-partent-à-lassaut-du-secteur-bancaire.pdf',
29+
'les_entrepreneurs_partent_a_lassaut_du_secteur_bancaire.pdf',
30+
],
31+
[
32+
'image.jpg',
33+
'image.jpg',
34+
],
35+
[
36+
'image with spaces.jpg',
37+
'image_with_spaces.jpg',
38+
],
39+
[
40+
'image/with/slashes.jpg',
41+
'image_with_slashes.jpg',
42+
],
43+
[
44+
'image.jpg.webp',
45+
'image_jpg.webp',
46+
],
47+
[
48+
'image.png.avif',
49+
'image_png.avif',
50+
],
51+
[
52+
'image.png.heif',
53+
'image_png.heif',
54+
],
55+
[
56+
'folder/folder.image.jpg.webp',
57+
'folder_folder_image_jpg.webp',
58+
],
59+
[
60+
'folder/archive.tar.gz',
61+
'folder_archive.tar.gz',
62+
],
63+
[
64+
'folder/archive.tar.xz',
65+
'folder_archive.tar.xz',
66+
],
67+
[
68+
'folder/archive.tar.zip',
69+
'folder_archive.tar.zip',
70+
],
71+
[
72+
'folder/archive.tar.bz',
73+
'folder_archive.tar.bz',
74+
],
75+
[
76+
'folder/archive.tar.bz2',
77+
'folder_archive.tar.bz2',
78+
],
79+
[
80+
'folder/archive.tar.tgz',
81+
'folder_archive.tar.tgz',
82+
],
83+
[
84+
'folder/archive.tar.7z',
85+
'folder_archive.tar.7z',
86+
],
87+
];
88+
}
89+
}

0 commit comments

Comments
 (0)