diff --git a/src/Helpers/media_library_helpers.php b/src/Helpers/media_library_helpers.php index b64cab531..0d441e9c9 100644 --- a/src/Helpers/media_library_helpers.php +++ b/src/Helpers/media_library_helpers.php @@ -3,6 +3,7 @@ use Illuminate\Support\Facades\Storage; use Aws\S3\S3Client; use Aws\S3\PostObjectV4; +use Illuminate\Support\Str; if (!function_exists('s3Endpoint')) { /** @@ -60,13 +61,11 @@ function bytesToHuman($bytes) /** * @param string $str * @return bool|string + * @deprecated Use Str::ascii instead */ function replaceAccents($str) { - if (function_exists('mb_convert_encoding')) { - return mb_convert_encoding($str, 'ASCII', 'UTF-8'); - } - return iconv('UTF-8', 'ASCII//TRANSLIT', $str); + return Str::ascii($str); } } @@ -77,7 +76,7 @@ function replaceAccents($str) */ function sanitizeFilename($filename) { - $sanitizedFilename = replaceAccents($filename); + $sanitizedFilename = Str::ascii($filename); $invalid = array( ' ' => '-', @@ -87,8 +86,8 @@ function sanitizeFilename($filename) $sanitizedFilename = str_replace(array_keys($invalid), array_values($invalid), $sanitizedFilename); - $sanitizedFilename = preg_replace('#[^A-Za-z0-9-\. ]#', '', $sanitizedFilename); // Remove all non-alphanumeric except . - $sanitizedFilename = preg_replace('#\.(?=.*\.)#', '', $sanitizedFilename); // Remove all but last . + $sanitizedFilename = preg_replace('#[^A-Za-z0-9-. ]#', '', $sanitizedFilename); // Remove all non-alphanumeric except . + $sanitizedFilename = preg_replace('#\.(?=.*\.)#', '-', $sanitizedFilename); // Remove all but last . $sanitizedFilename = preg_replace('#-+#', '-', $sanitizedFilename); // Replace any more than one - in a row $sanitizedFilename = str_replace('-.', '.', $sanitizedFilename); // Remove last - if at the end $sanitizedFilename = strtolower($sanitizedFilename); // Lowercase diff --git a/src/Models/Media.php b/src/Models/Media.php index cfef3e8a8..2286d0522 100755 --- a/src/Models/Media.php +++ b/src/Models/Media.php @@ -49,10 +49,10 @@ public function altTextFrom($filename) { $filename = pathinfo($filename, PATHINFO_FILENAME); if (Str::endsWith($filename, '@2x')) { - $filename = substr($filename, 0, -2); + $filename = substr($filename, 0, -3); } - return ucwords(preg_replace('/[^a-zA-Z0-9]/', ' ', sanitizeFilename($filename))); + return Str::ucfirst(preg_replace('/[-_]/', ' ', $filename)); } public function canDeleteSafely() diff --git a/tests/unit/Helpers/MediaLibraryHelpersTest.php b/tests/unit/Helpers/MediaLibraryHelpersTest.php new file mode 100644 index 000000000..dd0455311 --- /dev/null +++ b/tests/unit/Helpers/MediaLibraryHelpersTest.php @@ -0,0 +1,19 @@ +assertEquals('aeeiou', replaceAccents('àéèïôû')); + } + + public function testSanitizeFilename() + { + $this->assertEquals('happy-paques-xo-png.jpg', sanitizeFilename('Happy_Pâques - XO.png.jpg')); + } + +} diff --git a/tests/unit/Models/MediaTest.php b/tests/unit/Models/MediaTest.php new file mode 100644 index 000000000..f8fcd0bde --- /dev/null +++ b/tests/unit/Models/MediaTest.php @@ -0,0 +1,18 @@ +assertEquals("Happy Holidays", $m->altTextFrom('Happy_Holidays.jpg')); + $this->assertEquals("Happy Holidays", $m->altTextFrom('Happy_Holidays@2x.jpg')); + $this->assertEquals("J'aime la pièce", $m->altTextFrom('J\'aime-la-pièce.jpg')); + } +}