Last active
December 6, 2017 21:44
-
-
Save Boorj/7e28a9604d250929fcd047e2c89d6fce to your computer and use it in GitHub Desktop.
Uploaded filename sanitizer for Bolt (updated)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Bolt\Provider; | |
use Cocur\Slugify\Slugify; | |
use Silex\Application; | |
use Silex\ServiceProviderInterface; | |
class SanitizerProvider implements ServiceProviderInterface | |
{ | |
protected $replacement; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function register(Application $app) | |
{ | |
$app['upload.sanitizer'] = $app->share(function ($app) | |
{ | |
$pattern = $app['config']->get('general/upload/pattern', '[^A-Za-z0-9\.]+'); | |
$lowercase = $app['config']->get('general/upload/lowercase', true); | |
$regexp = "/$pattern/"; | |
/** @var Slugify $sanitizer */ | |
$sanitizer = new Slugify($regexp, ['lowercase' => $lowercase]); | |
return $sanitizer; | |
}); | |
$app['upload.sanitizer.replacement'] = $app->share(function ($app) | |
{ | |
$replacement = $app['config']->get('general/upload/replacement', '-'); | |
return $replacement; | |
}); | |
$app['upload'] = $app->share( | |
$app->extend( | |
'upload', | |
function (\Sirius\Upload\Handler $uploadHandler) use ($app) | |
{ | |
$uploadHandler->setSanitizerCallback( | |
function ($filename) use ($app) { | |
$cleanName = $app['upload.sanitizer']->slugify($filename, $app['upload.sanitizer.replacement']); | |
return $cleanName; | |
} | |
); | |
return $uploadHandler; | |
} | |
) | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function boot(Application $app) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment