|
| 1 | +--- |
| 2 | +title: Using a custom file removal strategy |
| 3 | +weight: 5 |
| 4 | +--- |
| 5 | + |
| 6 | +By default, files will be stored inside a directory that uses the `id` of its `Media`-object as a name. Given this default file and folder structure, the `DefaultFileRemover` option simply gets the root folder name and deletes it. |
| 7 | + |
| 8 | +In cases where you need to use a custom directory structure, the `DefaultFileRemover` can cause problems. For example, if you have a directory structure like this: |
| 9 | + |
| 10 | + |
| 11 | +``` |
| 12 | +media |
| 13 | +---- 2023/09 |
| 14 | +------ file.jpg |
| 15 | +------ second.jpg |
| 16 | +------ conversions |
| 17 | +--------- file-small.jpg |
| 18 | +--------- file-medium.jpg |
| 19 | +--------- file-big.jpg |
| 20 | +--------- second-small.jpg |
| 21 | +--------- second-medium.jpg |
| 22 | +--------- second-big.jpg |
| 23 | +... |
| 24 | +``` |
| 25 | + |
| 26 | +Using the `DefaultFileRemover` will delete the entire `2023` directory, which is not what you want. So we would use a custom file remover to delete only the files that are no longer needed. |
| 27 | + |
| 28 | + |
| 29 | +### Extending file remover functionality |
| 30 | + |
| 31 | + |
| 32 | +Let's take a look at the interface: |
| 33 | + |
| 34 | +```php |
| 35 | +<?php |
| 36 | + |
| 37 | +namespace Spatie\MediaLibrary\Support\FileRemover; |
| 38 | + |
| 39 | +use Illuminate\Contracts\Filesystem\Factory; |
| 40 | +use Spatie\MediaLibrary\MediaCollections\Filesystem; |
| 41 | +use Spatie\MediaLibrary\MediaCollections\Models\Media; |
| 42 | + |
| 43 | +interface FileRemover |
| 44 | +{ |
| 45 | + public function __construct(Filesystem $mediaFileSystem, Factory $filesystem); |
| 46 | + |
| 47 | + /* |
| 48 | + * Remove all files relating to the media model. |
| 49 | + */ |
| 50 | + public function removeAllFiles(Media $media): void; |
| 51 | + |
| 52 | + /* |
| 53 | + * Remove responsive files relating to the media model. |
| 54 | + */ |
| 55 | + public function removeResponsiveImages(Media $media, string $conversionName): void; |
| 56 | + |
| 57 | + /* |
| 58 | + * Remove a file relating to the media model. |
| 59 | + */ |
| 60 | + public function removeFile(string $path, string $disk): void; |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +``` |
| 65 | +You may use create your own custom file remover by implementing the `FileRemover` interface. |
| 66 | + |
| 67 | +### Here to help |
| 68 | + |
| 69 | +There is also now a second option available within media library for file remover functionality. Based on the above directory structure, we can use `FileBaseFileRemover`. |
| 70 | + |
| 71 | +```php |
| 72 | + // config/media-library.php |
| 73 | + |
| 74 | + /* |
| 75 | + * The class that contains the strategy for determining how to remove files. |
| 76 | + */ |
| 77 | + 'file_remover_class' => Spatie\MediaLibrary\Support\FileRemover\FileBaseFileRemover::class, |
| 78 | +``` |
| 79 | + |
| 80 | +This strategy works by locating the exact path of the image and conversions, and explicitly removing those files only, instead of purging a base directory. |
0 commit comments