Installation Add the package via Composer:
composer require cvepdb-cms/module-files
Publish the module's configuration and migrations:
php artisan vendor:publish --provider="Cvepdb\Files\FilesServiceProvider" --tag="config"
php artisan vendor:publish --provider="Cvepdb\Files\FilesServiceProvider" --tag="migrations"
Run migrations:
php artisan migrate
Basic Usage Register the module in your CMS's module manager (if applicable) or manually include its routes:
// In a service provider (e.g., AppServiceProvider)
Route::middleware(['web'])
->prefix('files')
->group(base_path('vendor/cvepdb-cms/module-files/src/routes/web.php'));
First File Upload Use the provided facade or service to handle uploads:
use Cvepdb\Files\Facades\Files;
$file = Files::upload('path/to/file.pdf', 'user_id_here');
Check the documentation for supported methods (e.g., download(), delete()).
Uploading Files
Files facade or inject the FilesService into controllers:
public function store(Request $request) {
$file = $request->file('document');
$uploaded = Files::upload($file, auth()->id(), ['type' => 'report']);
}
config/files.php:
'disk' => 's3', // or 'local', 'custom_disk'
Accessing Files
$url = Files::getDownloadUrl($fileId);
return Files::download($fileId, 'custom_filename.pdf');
File Metadata
metadata parameter:
Files::upload($file, auth()->id(), [
'metadata' => ['description' => 'Quarterly report', 'tags' => ['finance']]
]);
$metadata = Files::getMetadata($fileId);
Event Handling
FileUploaded, FileDeleted) in EventServiceProvider:
protected $listen = [
'Cvepdb\Files\Events\FileUploaded' => [
'App\Listeners\LogFileUpload',
],
];
FilesController as a blueprint for custom API endpoints.file_type, max_size) in Form Requests:
use Cvepdb\Files\Rules\FileType;
public function rules() {
return [
'file' => ['required', new FileType(['pdf', 'docx'])],
];
}
Disk Configuration
local disk, ensure the storage_path('app/files') directory is writable:
chmod -R 775 storage/app/files
s3, verify credentials in .env and test connectivity:
php artisan storage:link
File Ownership
FilesService or extend the File model:
// In a service provider
$this->app->bind('Cvepdb\Files\Contracts\FilesService', function ($app) {
return new CustomFilesService($app);
});
Large Files
'chunked_uploads' => false, // in config/files.php
Files::upload($file, $userId)->afterUpload(function ($file) {
ProcessLargeFile::dispatch($file);
});
Route Conflicts
/files. If conflicts arise, override the route prefix in config/files.php:
'route_prefix' => 'media',
config/files.php:
'debug' => env('APP_DEBUG', false),
FileUploaded in Tinker:
php artisan tinker
>>> event(new \Cvepdb\Files\Events\FileUploaded($file));
Custom Storage Drivers
Extend the FilesService to support new storage backends:
namespace App\Services;
use Cvepdb\Files\Contracts\FilesService as BaseFilesService;
class CustomFilesService extends BaseFilesService {
public function upload($file, $userId, array $options = []) {
// Custom logic (e.g., encrypt files before upload)
return parent::upload($file, $userId, $options);
}
}
File Processing
Hook into the FileUploaded event to process files (e.g., OCR, thumbnails):
public function handle(FileUploaded $event) {
$file = $event->file;
// Generate thumbnail
Thumbnail::create($file->path, "thumb_{$file->id}.jpg");
}
Model Bindings
Bind the File model to routes for easy access:
Route::get('/files/{file}', function (Cvepdb\Files\Models\File $file) {
return $file->path;
});
Policy Integration
Attach policies to the File model for authorization:
use Cvepdb\Files\Models\File;
use Illuminate\Auth\Access\HandlesAuthorization;
class FilePolicy {
use HandlesAuthorization;
public function download(User $user, File $file) {
return $user->id === $file->user_id;
}
}
How can I help you explore Laravel packages today?