Dedupler is a Laravel package that automatically prevents duplicate files using SHA-1 hashing and provides a polymorphic API for managing your attachments. If you want to ensure duplicate images are not uploaded multiple times in your application, this package might be able to help you with that:
// Using the Facade /** @var \Illuminate\Http\UploadedFile $file *//** @var \Maxkhim\Dedupler\Models\UniqueFile $uniqueFile */$uniqueFile = Dedupler::storeFromUploadedFile($file); $uniqueFile = Dedupler::storeFromPath($absolutePathToFile); $uniqueFile = Dedupler::storeFromContent($content, 'direct_content_file.ext');
You can also use the package's trait on your model, which associates deduplicated images to your model:
namespace App\Models; use Illuminate\Database\Eloquent\Model;use Maxkhim\Dedupler\Traits\Deduplable; class Post extends Model{ use Deduplable;} $uniqueFile = $post->storeUploadedFile($file);$uniqueFile = $post->storeLocalFile($absolutePathToFile);$uniqueFile = $post->storeContentFile($content, 'direct_content_file.ext'); // Remove a file from a model$post->detachUniqueFile($sha1_hash);
One note about this approach: multiple models might use the same image record if it corresponds to the same hash. Detaching the file from one model does not delete the image record. It only dissociates it from the specific model.
Main Features
- Zero Duplicates - Automatic deduplication using SHA-1 hashing
- Polymorphic Magic - Attach files to any model with ease
- Storage Efficient - Save significant disk space
- Simple API - Intuitive methods for attachment management
- Laravel Native - Seamlessly integrates with Laravel's ecosystem
💻 You can get started with this package on GitHub: maxkhim/laravel-storage-dedupler