x-laravel/spatie-media-library-uuid-path 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

x-laravel/spatie-media-library-uuid-path

最新稳定版本:v1.0.0

Composer 安装命令:

composer require x-laravel/spatie-media-library-uuid-path

包简介

A UUID-based path generator for spatie/laravel-medialibrary.

README 文档

README

Tests PHP Laravel License

A UUID-based path generator for spatie/laravel-medialibrary.

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 evenly by turning the first 8 characters of each media UUID into a four-level directory hierarchy:

55/0e/84/00/550e8400-e29b-41d4-a716-446655440000/photo.jpg

Conversions and responsive images are placed in dedicated subdirectories under the UUID folder:

55/0e/84/00/550e8400-e29b-41d4-a716-446655440000/conversions/
55/0e/84/00/550e8400-e29b-41d4-a716-446655440000/responsive-images/

Benefits:

  • Performance: Each directory 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.

Requirements

  • PHP ^8.2
  • 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:

'path_generator' => \XLaravel\SpatieMediaLibraryUuidPath\UuidPathGenerator::class,

'file_remover_class' => \XLaravel\SpatieMediaLibraryUuidPath\UuidFileRemover::class,

Why UuidFileRemover? The default file remover deletes the UUID directory but leaves the empty shard parent directories (55/0e/84/00/) behind. UuidFileRemover cascades 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:

'path_generator' => \XLaravel\SpatieMediaLibraryUuidPath\UuidPathGenerator::class,
'file_remover_class' => \XLaravel\SpatieMediaLibraryUuidPath\UuidFileRemover::class,

The command 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)
--dry-run List orphaned directories without deleting them
--force Skip the production confirmation prompt

Running Tests

# Locally
vendor/bin/phpunit

# Via Docker (specific PHP version)
docker compose --profile php82 run --rm php82
docker compose --profile php83 run --rm php83
docker compose --profile php84 run --rm php84
docker compose --profile php85 run --rm php85

x-laravel/spatie-media-library-uuid-path 适用场景与选型建议

x-laravel/spatie-media-library-uuid-path 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「uuid」 「laravel」 「spatie」 「media-library」 「path-generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 x-laravel/spatie-media-library-uuid-path 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 x-laravel/spatie-media-library-uuid-path 我们能提供哪些服务?
定制开发 / 二次开发

基于 x-laravel/spatie-media-library-uuid-path 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 34
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-22