Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/Helpers/media_library_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
/**
Expand Down Expand Up @@ -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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to remove this after adding tests é would be converted into ? instead of e which could result in empty filenames

}
return iconv('UTF-8', 'ASCII//TRANSLIT', $str);
return Str::ascii($str);
}
}

Expand All @@ -77,7 +76,7 @@ function replaceAccents($str)
*/
function sanitizeFilename($filename)
{
$sanitizedFilename = replaceAccents($filename);
$sanitizedFilename = Str::ascii($filename);

$invalid = array(
' ' => '-',
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/Helpers/MediaLibraryHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace A17\Twill\Tests\Unit\Helpers;

use A17\Twill\Tests\Unit\TestCase;

class MediaLibraryHelpersTest extends TestCase
{
public function testReplaceAccents()
{
$this->assertEquals('aeeiou', replaceAccents('àéèïôû'));
}

public function testSanitizeFilename()
{
$this->assertEquals('happy-paques-xo-png.jpg', sanitizeFilename('Happy_Pâques - XO.png.jpg'));
}

}
18 changes: 18 additions & 0 deletions tests/unit/Models/MediaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace A17\Twill\Tests\Unit\Models;

use A17\Twill\Models\Media;
use A17\Twill\Tests\Unit\TestCase;

class MediaTest extends TestCase
{
public function testAltText()
{
$m = new Media();

$this->assertEquals("Happy Holidays", $m->altTextFrom('Happy_Holidays.jpg'));
$this->assertEquals("Happy Holidays", $m->altTextFrom('[email protected]'));
$this->assertEquals("J'aime la pièce", $m->altTextFrom('J\'aime-la-pièce.jpg'));
}
}