x-laravel/spatie-medialibrary-uuid-path-generator
最新稳定版本:v1.0.0
Composer 安装命令:
composer require x-laravel/spatie-medialibrary-uuid-path-generator
包简介
A UUID-based path generator for spatie/laravel-medialibrary.
README 文档
README
A UUID-based path generator for spatie/laravel-medialibrary.
Unofficial plugin. Not affiliated with Spatie.
The Problem
spatie/laravel-medialibrary's default DefaultPathGenerator stores media files in a flat structure based on the primary key (ID):
1/photo.jpg
2/photo.jpg
3/photo.jpg
...
This works fine for small applications, but causes serious issues as the number of media files grows:
- File system performance degradation: File systems like ext4 and NTFS slow down directory listings when thousands of subdirectories exist under a single parent.
- Predictable URLs: The sequential ID-based structure makes media file URLs trivially easy to enumerate.
- Operational overhead: Bulk-moving, backing up, or migrating files to a CDN becomes harder with a flat layout.
The Solution
This package distributes files by turning the leading characters of each media UUID into a sharded directory hierarchy. You pick the shard depth that fits your catalog size:
# Level 2 (recommended default — see "Picking a shard depth" below)
55/0e/550e8400-e29b-41d4-a716-446655440000/photo.jpg
Conversions and responsive images are placed in dedicated subdirectories under the UUID folder:
55/0e/550e8400-e29b-41d4-a716-446655440000/conversions/
55/0e/550e8400-e29b-41d4-a716-446655440000/responsive-images/
Benefits:
- Performance: Each shard level holds at most 256 subdirectories, spreading the file system load evenly.
- Security: The UUID-based random structure makes file paths unpredictable and resistant to enumeration.
- Uniqueness: Every media file gets its own UUID directory, eliminating any risk of path collisions.
Picking a shard depth
The package ships four path generators. They share the same layout — xx/.../xx/<uuid>/ — and differ only in how many two-character shard levels they prepend. Pick the smallest depth that still keeps leaf directories under control for your catalog size:
| Generator | Layout | Max leaf directories | Suited to | |
|---|---|---|---|---|
UuidLevel1PathGenerator |
xx/<uuid>/ |
256 | Small catalogs (≲ 250 k files) | |
UuidLevel2PathGenerator |
xx/xx/<uuid>/ |
65 536 | Medium catalogs (low millions) | recommended default |
UuidLevel3PathGenerator |
xx/xx/xx/<uuid>/ |
~16.7 M | Large catalogs / busy object stores | |
UuidLevel4PathGenerator |
xx/xx/xx/xx/<uuid>/ |
~4.3 B | Very large pools or remote disks where flat LIST is expensive |
When in doubt, start with Level2 — it handles up to ~10 million files comfortably and keeps cascade cleanup, LIST traversal, and path readability all in a sensible range. Migrating to a deeper layout later is cheaper than overshooting now and dragging around millions of mostly-empty intermediate directories.
Requirements
- PHP ^8.3
- spatie/laravel-medialibrary ^11.0
Installation
composer require x-laravel/spatie-media-library-uuid-path
Setup
Publish the spatie/laravel-medialibrary config (if you haven't already) and set the path_generator option:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"
In config/media-library.php, pick the depth you want and wire up the matching path generator together with the cascade-aware file remover:
'path_generator' => XLaravel\Spatie\MediaLibrary\UuidPathGenerator\PathGenerators\UuidLevel2PathGenerator::class, 'file_remover_class' => XLaravel\Spatie\MediaLibrary\UuidPathGenerator\UuidFileRemover::class,
Swap UuidLevel2PathGenerator for UuidLevel1PathGenerator, UuidLevel3PathGenerator, or UuidLevel4PathGenerator if you need a different shard depth. The file remover and the artisan commands shipped with this package introspect this config and automatically follow the depth you chose — there is no separate setting to keep in sync.
Why
UuidFileRemover? The default file remover deletes the UUID directory but leaves the empty shard parent directories (55/0e/84/00/) behind.UuidFileRemovercascades upward and removes each shard level when it becomes empty.
Migrating from DefaultPathGenerator
If your project already has media files stored with spatie's default ID-based structure (1/photo.jpg, 2/photo.jpg), you can migrate them to the UUID path structure without any downtime:
1. While your config still points to DefaultPathGenerator, run:
php artisan media:migrate-to-uuid
2. Once it completes, switch the config to this package (pick the depth that suits your catalog — see Picking a shard depth):
'path_generator' => XLaravel\Spatie\MediaLibrary\UuidPathGenerator\PathGenerators\UuidLevel2PathGenerator::class, 'file_remover_class' => XLaravel\Spatie\MediaLibrary\UuidPathGenerator\UuidFileRemover::class,
The command reads media-library.path_generator to decide where files land, so set it to the depth you want before running the migration. It moves all files (including conversions and responsive images), deletes the old ID directories, and is safe to re-run — it skips media whose old directory no longer exists.
Options:
| Option | Description |
|---|---|
disk |
Disk to migrate (defaults to media-library.disk_name config) |
--dry-run |
Preview what would be moved without touching any files |
--force |
Skip the production confirmation prompt |
Cleaning Orphaned Directories
spatie's built-in media-library:clean command identifies orphaned directories using an is_numeric() check, which only works for the default ID-based path structure. This package ships a UUID-aware replacement:
php artisan media:clean-uuid
Options:
| Option | Description |
|---|---|
disk |
Disk to clean (defaults to media-library.disk_name config) |
--shard= |
Limit the scan to the given first-level shards (e.g. --shard=55 --shard=ab). Repeatable. Useful for splitting a large sweep into chunks or rerunning a partial scan on a remote disk. Defaults to every shard (00..ff). |
--dry-run |
List orphaned directories without deleting them |
--force |
Skip the production confirmation prompt |
Testing
# Build first (once per PHP version) DOCKER_BUILDKIT=0 docker compose --profile php83 build # Run tests docker compose --profile php83 up docker compose --profile php84 up docker compose --profile php85 up
License
This package is open-sourced software licensed under the MIT license.
x-laravel/spatie-medialibrary-uuid-path-generator 适用场景与选型建议
x-laravel/spatie-medialibrary-uuid-path-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「uuid」 「laravel」 「spatie」 「medialibrary」 「path-generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 x-laravel/spatie-medialibrary-uuid-path-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 x-laravel/spatie-medialibrary-uuid-path-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 x-laravel/spatie-medialibrary-uuid-path-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
DrUUID RFC 4122 library for PHP
Trait to generate uuid when creating models
Easily optimize images using PHP
Store your application settings
SilverStripe module for adding an unique id (UUID) to a DataObject
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 40
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-10