uxf / storage
3.56.3
2024-12-18 10:59 UTC
Requires
- php: ^8.3
- ext-json: *
- league/flysystem: ^3.0
- league/flysystem-aws-s3-v3: ^3.0
- ramsey/uuid-doctrine: ^2.0
- uxf/core: 3.56.3
- dev-master
- dev-main
- 3.56.3
- 3.56.2
- 3.56.1
- 3.56.0
- 3.55.9
- 3.55.8
- 3.55.7
- 3.55.5
- 3.55.0
- 3.54.5
- 3.54.3
- 3.54.2
- 3.54.0
- 3.53.7
- 3.53.3
- 3.53.2
- 3.53.1
- 3.53.0
- 3.51.2
- 3.51.0
- 3.50.6
- 3.50.5
- 3.50.3
- 3.50.2
- 3.50.1
- 3.50.0
- 3.49.2
- 3.49.1
- 3.49.0
- 3.48.0
- 3.47.2
- 3.47.0
- 3.46.11
- 3.44.6
- 3.44.5
- 3.44.4
- 3.44.3
- 3.44.2
- 3.44.0
- 3.43.2
- 3.43.0
- 3.42.0
- 3.41.2
- 3.41.1
- 3.41.0
- 3.40.4
- 3.40.3
- 3.40.2
- 3.40.1
- 3.40.0
- 3.39.4
- 3.39.3
- 3.39.2
- 3.39.1
- 3.38.0
- 3.37.1
- 3.37.0
- 3.36.3
- 3.36.2
- 3.36.0
- 3.35.5
- 3.35.4
- 3.35.2
- 3.34.3
- 3.34.0
- 3.32.4
- 3.32.3
- 3.31.1
- 3.31.0
- 3.30.1
- 3.29.2
- 3.29.1
- 3.29.0
- 3.27.5
- 3.27.4
- 3.27.3
- 3.26.1
- 3.26.0
- 3.25.2
- 3.25.0
- 3.24.5
- 3.24.3
- 3.24.2
- 3.24.1
- 3.24.0
- 3.23.8
- 3.23.3
- 3.23.1
- 3.23.0
- 3.22.0
- 3.21.4
- 3.21.3
- 3.21.0
- 3.20.1
- 3.20.0
- 3.19.4
- 3.19.2
- 3.18.0
- 3.17.4
- 3.17.3
- 3.17.1
- 3.17.0
- 3.15.6
- 3.15.5
- 3.13.2
- 3.13.0
- 3.11.3
- 3.11.2
- 3.11.1
- 3.11.0
- 3.10.1
- 3.10.0
- 3.9.2
- 3.8.2
- 3.8.1
- 3.8.0
- 3.7.3
- 3.7.1
- 3.7.0
- 3.6.1
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 1.0.0
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.1
- v0.1.0
- dev-feature/generator-save-options
- dev-x
This package is auto-updated.
Last update: 2024-12-18 10:03:56 UTC
README
Install
$ composer req uxf/storage
Config
// config/packages/uxf.php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('uxf_storage', [
'filesystems' => [
'default' => '%env(STORAGE_DSN)%', // default local
],
]);
};
Storage | DSN | composer |
---|---|---|
Local | local://default/%kernel.project_dir%/public/upload | |
AWS S3 | aws://key:secret@domain.com/bucket | league/flysystem-aws-s3-v3 |
Azure | azure://accountName:accountKey@core.windows.net/container | league/flysystem-azure-blob-storage |
S3 | s3://key:secret@domain.com/bucket | |
S3 http | s3://key:secret@domain.com/bucket?schema=http |
Usage
FileCreator
use League\Flysystem\FilesystemOperator;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use UXF\Core\SystemProvider\Uuid;
use UXF\Storage\Entity\File;
use UXF\Storage\Entity\Image;
use UXF\Storage\Utils\DestinationDirHelper;
use UXF\Storage\Utils\MimeTypeHelper;
class UploadedFileCreator implements FileCreator
{
public function __construct(private readonly FilesystemOperator $defaultFilesystem)
{
}
public function createFile(object $file, ?string $namespace): ?File
{
if (!$file instanceof UploadedFile) {
return null;
}
$uuid = Uuid::uuid4();
$type = $file->getClientMimeType();
$ext = $file->getClientOriginalExtension();
$ext = $ext !== '' ? $ext : (MimeTypeHelper::convertToExtension($type) ?? 'jpg');
$size = $file->getSize();
$raw = $file->getContent();
$destDir = DestinationDirHelper::getDir($uuid, $namespace);
$this->defaultFilesystem->write("$destDir/$uuid.$ext", $file->getContent());
$originalName = $file->getClientOriginalName();
if (MimeTypeHelper::isImage($type)) {
$result = getimagesizefromstring($raw);
assert($result !== false);
[$width, $height] = $result;
return new Image($uuid, $ext, $type, $originalName, $namespace, $size, $width ?? 0, $height ?? 0);
}
return new File($uuid, $ext, $type, $originalName, $namespace, $size);
}
}