canaltp/media-manager
Laravel media manager package for organizing uploaded files with a simple, developer-friendly API. Helps store, retrieve, and manage media assets (images, documents, etc.) in your app, with streamlined integration into common Laravel workflows.
Installation
composer require canaltp/media-manager
Publish the package assets and configuration:
php artisan vendor:publish --provider="CanalTP\MediaManager\Component\MediaManagerServiceProvider" --tag="config"
php artisan vendor:publish --provider="CanalTP\MediaManager\Component\MediaManagerServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="CanalTP\MediaManager\Component\MediaManagerServiceProvider" --tag="public"
Run migrations:
php artisan migrate
Configuration
Review config/media-manager.php to set:
default_disk)allowed_mime_types)max_file_size)upload_paths)First Use Case: Uploading a File
use CanalTP\MediaManager\Component\Contracts\MediaManagerContract;
use Illuminate\Support\Facades\Storage;
$mediaManager = app(MediaManagerContract::class);
$file = request()->file('file');
$media = $mediaManager->upload($file, [
'disk' => 'public',
'path' => 'uploads',
'name' => 'custom_filename.jpg',
]);
// $media now contains metadata like URL, path, size, etc.
File Uploads
$media = $mediaManager->upload($file, ['disk' => 'public']);
$media = $mediaManager->upload($file, [
'path' => 'custom/folder',
'name' => 'user_' . auth()->id() . '_' . time() . '.jpg'
]);
MediaManager:
$request->validate([
'file' => 'required|mimes:' . implode(',', config('media-manager.allowed_mime_types')),
'file.*' => 'max:' . config('media-manager.max_file_size')
]);
File Management
$media = $mediaManager->get('path/to/file.jpg');
$mediaManager->delete($media->path);
$mediaList = $mediaManager->list('uploads');
Integration with Eloquent
morphToMany or a custom trait:
// Example trait
use CanalTP\MediaManager\Component\Traits\HasMedia;
class Post extends Model
{
use HasMedia;
}
Then attach media:
$post->addMedia($media)->toMedia('images');
Thumbnails and Processing
spatie/laravel-medialibrary or intervention/image alongside:
$mediaManager->process($media->path, function ($image) {
$image->resize(300, 200);
return $image;
});
Custom Storage Disks
Configure additional disks in config/filesystems.php and reference them:
$media = $mediaManager->upload($file, ['disk' => 's3']);
Event Handling
Listen for media events (e.g., MediaUploaded):
// In EventServiceProvider
protected $listen = [
'CanalTP\MediaManager\Component\Events\MediaUploaded' => [
'App\Listeners\LogMediaUpload',
],
];
API Responses Serialize media for APIs:
return response()->json([
'media' => $media->toArray(),
'url' => $media->getUrl(),
]);
Batch Operations Process multiple files:
foreach ($request->file('files') as $file) {
$mediaManager->upload($file, ['path' => 'batch_uploads']);
}
Permission Issues
chmod -R 775 storage/app/public
MIME Type Restrictions
allowed_mime_types in config. Use fileinfo for dynamic checks:
$mime = mime_content_type($file->getPathname());
if (!in_array($mime, config('media-manager.allowed_mime_types'))) {
throw new \Exception('Invalid file type');
}
File Naming Collisions
Str::uuid() or timestamps to avoid overwrites:
$name = Str::uuid() . '.' . $file->getClientOriginalExtension();
Missing Migrations
media table is missing, republish migrations:
php artisan vendor:publish --tag="migrations" --force
Log Uploads
Enable debug mode in config/media-manager.php:
'debug' => env('MEDIA_MANAGER_DEBUG', false),
Check logs for errors during uploads.
Storage Disk Verification Test disk connectivity:
Storage::disk('public')->put('test.txt', 'test');
Event Debugging
Use Laravel's events.log or dd() in event listeners:
public function handle(MediaUploaded $event) {
dd($event->media);
}
Custom Media Models
Extend the Media model:
class CustomMedia extends \CanalTP\MediaManager\Component\Models\Media
{
protected $casts = [
'custom_field' => 'boolean',
];
}
Update the config to use your model.
Add Metadata Extend the upload method to include custom metadata:
$media = $mediaManager->upload($file, [
'metadata' => [
'author_id' => auth()->id(),
'tags' => ['image', 'profile'],
],
]);
Custom Storage Drivers
Implement CanalTP\MediaManager\Component\Contracts\StorageDriver for new backends (e.g., FTP, Dropbox).
Validation Rules Create custom rules for file validation:
use CanalTP\MediaManager\Component\Rules\ValidMediaType;
$request->validate([
'file' => ['required', new ValidMediaType],
]);
Chunked Uploads For large files, use client-side chunking (e.g., Dropzone.js) and implement server-side reassembly.
Queue Uploads Offload processing to queues:
UploadMediaJob::dispatch($file, $options)->onQueue('media');
Optimize Thumbnails Cache thumbnails to avoid reprocessing:
if (!$media->thumbnailExists()) {
$media->generateThumbnail();
}
Lazy Loading
Use with() to eager-load media relationships:
$post->load('media');
How can I help you explore Laravel packages today?