bezpapirove/bezpapirove_php_lib
Install via Composer:
composer require bezpapirove/bezpapirove_php_lib
Basic File Storage (v2.x):
use Bezpapirove\BezpapirovePhpLib\File\FileStorageFactory;
use Bezpapirove\BezpapirovePhpLib\File\Uuid;
$config = [
'driver' => 'local', // or 's3'
'path' => storage_path('app/files'),
// S3-specific config (if applicable):
// 'bucket' => 'your-bucket',
// 'key' => 'your-key',
// 'secret' => 'your-secret',
];
$storage = FileStorageFactory::createFromConfig($config);
First Use Case: Upload a File
$uuid = Uuid::generate(); // Generate UUID for the file
$storage->save('/path/to/local/file.txt', $uuid);
FileStorageFactory: Centralized way to create storage adapters (local/S3).Uuid class: Handles UUID generation/validation for file naming.FileStorageInterface: Core methods for CRUD operations on files.// Upload
$uuid = Uuid::generate();
$storage->save($localPath, $uuid);
// Retrieve
$fileContent = $storage->read($uuid);
Leverage the FileStorageInterface to abstract storage backends:
public function processFile(FileStorageInterface $storage, Uuid $uuid) {
if ($storage->exists($uuid)) {
$content = $storage->read($uuid);
// Process content...
$storage->duplicate($uuid, Uuid::generate()); // Backup
}
}
Configure S3 in the factory:
$config = [
'driver' => 's3',
'bucket' => 'my-bucket',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
];
$storage = FileStorageFactory::createFromConfig($config);
For projects using v1.x, use FolderStructure:
use Bezpapirove\BezpapirovePhpLib\File\FolderStructure;
$path = FolderStructure::getFolderStructureFromFileName(
Uuid::fromString('123e4567-e89b-12d3-a456-426614174000'),
2 // Depth
);
FolderStructure::createFolderStructure(storage_path('app'), $path);
FileStorageInterface to a concrete implementation in AppServiceProvider:
$this->app->bind(FileStorageInterface::class, function ($app) {
return FileStorageFactory::createFromConfig(config('filesystem.storage'));
});
Uuid::isValid() to validate UUIDs before operations.FileNotFoundException for missing files).Version Mismatch:
FileStorageFactory and breaks backward compatibility. Avoid mixing versions in the same project.UUID Collisions:
$uuid = Uuid::generate();
$safeUuid = $uuid->toString() . '-' . time();
S3 Configuration:
RuntimeException. Validate configs early:
try {
$storage = FileStorageFactory::createFromConfig($config);
} catch (\RuntimeException $e) {
Log::error('S3 config invalid: ' . $e->getMessage());
}
Local Storage Paths:
/tmp) may fail in production. Always use Laravel’s storage_path() or config-driven paths.Uuid::isValid($uuid)) and if the storage backend is correctly configured.storage/app/files) is writable:
chmod -R 775 storage/app/files
\Aws\setLogger(new \Monolog\Logger('AWS'));
Custom Storage Drivers:
Extend FileStorageInterface and register new drivers in FileStorageFactory:
FileStorageFactory::registerDriver('custom', function ($config) {
return new CustomStorage($config);
});
UUID Generation: Override the default UUID generator (e.g., for shorter IDs):
Uuid::setGenerator(function () {
return Uuid::fromString(Str::uuid());
});
Event Hooks: Listen for file operations (e.g., post-upload processing):
$storage->save($path, $uuid);
event(new FileUploaded($uuid, $path));
config/filesystem.storage array in Laravel. Example:
'storage' => [
'driver' => env('FILESYSTEM_DRIVER', 'local'),
'path' => storage_path('app/files'),
],
.env and reference them in the config:
AWS_ACCESS_KEY_ID=your-key
AWS_SECRET_ACCESS_KEY=your-secret
AWS_DEFAULT_REGION=us-east-1
read() with file_get_contents() for large files to avoid memory issues:
$content = $storage->read($uuid);
file_put_contents($localPath, $content);
exists() checks if files rarely change:
$cacheKey = "file_exists_{$uuid}";
if (!Cache::has($cacheKey) && !$storage->exists($uuid)) {
Cache::put($cacheKey, false, now()->addHours(1));
}
How can I help you explore Laravel packages today?