Skip to content

Commit

Permalink
File Name Sanitizer / Slugger (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
xDeSwa authored Feb 1, 2024
1 parent aee162f commit a11b6c3
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
10 changes: 10 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public function getConfigTreeBuilder(): TreeBuilder {
->end()
->booleanNode('override')->defaultValue(false)->end()
->end()
->children()
->arrayNode('filename_sanitizer')
->children()
->scalarNode('transformer')->end()
->booleanNode('slugger')->end()
->scalarNode('prepend')->end()
->scalarNode('append')->end()
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
47 changes: 46 additions & 1 deletion Helpers/FileManagerUploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
namespace Artgris\Bundle\FileManagerBundle\Helpers;


use Symfony\Component\String\Slugger\AsciiSlugger;

class FileManagerUploadHandler extends UploadHandler {

protected function get_unique_filename($file_path, $name, $size, $type, $error,
$index, $content_range) {
$index, $content_range) {

$name = $this->set_filename_sanitizer($name);

if ($this->options['override']) {
return $name;
}
Expand All @@ -29,4 +34,44 @@ protected function get_unique_filename($file_path, $name, $size, $type, $error,
return $name;
}

protected function set_filename_sanitizer($name): string
{

if(!isset($this->options['filename_sanitizer']['slugger']) || $this->options['filename_sanitizer']['slugger'] !== true){
return $name;
}

$file_extension = pathinfo($name, PATHINFO_EXTENSION);
$new_file_name = str_replace('.' . $file_extension, '', $name);

$slugger = new AsciiSlugger();

if (isset($this->options['filename_sanitizer']['prepend'])) {
$new_file_name = $slugger->slug($new_file_name)->prepend($this->options['filename_sanitizer']['prepend']);
}

if (isset($this->options['filename_sanitizer']['append'])) {
$new_file_name = $slugger->slug($new_file_name)->append($this->options['filename_sanitizer']['append']);
}

if (isset($this->options['filename_sanitizer']['transformer'])) {
$new_file_name = match ($this->options['filename_sanitizer']['transformer']) {
'uppercase' => $slugger->slug($new_file_name)->upper(),
'lowercase' => $slugger->slug($new_file_name)->lower(),
'camel' => $slugger->slug($new_file_name)->camel(),
'snake' => $slugger->slug($new_file_name)->snake(),
'cameltitle' => $slugger->slug($new_file_name)->camel()->title(),
'title' => $slugger->slug($new_file_name)->title(),
'titleall' => $slugger->slug($new_file_name)->title(true),
'folded' => $slugger->slug($new_file_name)->folded(),
default => $slugger->slug($new_file_name),
};
} else {
$new_file_name = $slugger->slug($new_file_name);
}


return $new_file_name . "." . $file_extension;
}

}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ FileManager is a simple Multilingual File Manager Bundle for Symfony
* Upload, delete (multiple), rename, download and sort files
* Create, rename and delete folders
* Manage **Public** and **Private** folders
* File Names **Sanitizer** / **Slugger** ([Look Documentation](Resources/doc/book/1-basic-configuration.md))
* **Multilingual** (English, French, Catalan, German, Spanish, Dutch, Portuguese, Romanian, Russian, Turkish)
* **Fully responsive design** (bootstrap)
* Multilple view modes (list, thumbnail, with tree or not)
Expand Down
31 changes: 31 additions & 0 deletions Resources/doc/book/1-basic-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,37 @@ artgris_file_manager:
override: true
```

#### File Name Sanitizer / Slugger
Change File Name with Slugger on upload file ( uses [Symfony AsciiSlugger Interface](https://symfony.com/doc/current/components/string.html) )
> It is recommended to override 'true'

**Be Sure installed Symfony/String**
> composer require symfony/string

```yml
artgris_file_manager:
conf:
public:
dir: '%kernel.project_dir%/public/uploads'
upload:
override: true
# image_versions: {'thumbnail': {crop: true, max_width: 10, max_height: 10}}
filename_sanitizer:
slugger: true
transformer: 'none' # none, uppercase, lowercase, camel, snake, cameltitle, title, titleall, folded
prepend: '' # File name rename to : image.jpg -> 2024_image.jpg
append: '' # # File name rename to : image.jpg -> image_2025.jpg
```
-------------------------------------------------------------------------------


| Option | Type | Value | Example |
|:--------------|:---------:|:---------:|:-----------------------------------------------------|
| `slugger` | `Boolean` | | Fav logo śćąóź∆.jpg **rename to** Fav-logo-scaoz.jpg |
| `transformer` | `String` | lowercase | **fav-logo-scaoz.jpg** |
| `prepend` | `String` | 2024- | **2024-fav-logo-scaoz.jpg** |
| `append` | `String` | -2025 | **fav-logo-scaoz-2025.jpg** |

>If prepend and append parameters will be blank, it wont add any strings.

[Chapter 2 - Service Configuration](2-service-configuration.md) →
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"symfony/form": "^6.0||^7.0",
"symfony/framework-bundle": "^6.0||^7.0",
"symfony/mime": "^6.0||^7.0",
"symfony/string": "^6.0||^7.0",
"symfony/translation": "^6.0||^7.0",
"symfony/twig-bridge": "^6.0||^7.0",
"symfony/twig-bundle": "^6.0||^7.0",
Expand Down

0 comments on commit a11b6c3

Please sign in to comment.