From dc81eea51d793cc87df2a8f5a96d75a8a213214a Mon Sep 17 00:00:00 2001 From: Maxime Renou Date: Thu, 21 Feb 2019 12:28:32 +0100 Subject: [PATCH] Awesome features (you'll see) --- StorageBundle/Adaptors/S3Storage.php | 11 +++- StorageBundle/Adaptors/StorageAdaptor.php | 13 ++++ StorageBundle/Annotations/Storage.php | 31 ++++++++++ StorageBundle/Exceptions/InvalidFileException.php | 8 +++ .../Exceptions/InvalidStorageConfiguration.php | 8 +++ StorageBundle/Exceptions/MimeTypeException.php | 8 +++ .../Exceptions/MissingStorageAnnotation.php | 8 +++ StorageBundle/Exceptions/UnknownStorage.php | 8 +++ StorageBundle/Storage.php | 69 ++++++++++++++++++++++ composer.json | 3 +- 10 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 StorageBundle/Adaptors/StorageAdaptor.php create mode 100644 StorageBundle/Annotations/Storage.php create mode 100644 StorageBundle/Exceptions/InvalidFileException.php create mode 100644 StorageBundle/Exceptions/InvalidStorageConfiguration.php create mode 100644 StorageBundle/Exceptions/MimeTypeException.php create mode 100644 StorageBundle/Exceptions/MissingStorageAnnotation.php create mode 100644 StorageBundle/Exceptions/UnknownStorage.php diff --git a/StorageBundle/Adaptors/S3Storage.php b/StorageBundle/Adaptors/S3Storage.php index 79ca6c6..1ea29a0 100644 --- a/StorageBundle/Adaptors/S3Storage.php +++ b/StorageBundle/Adaptors/S3Storage.php @@ -3,6 +3,7 @@ namespace Bluesquare\StorageBundle\Adaptors; use Aws\S3\S3Client; +use Bluesquare\StorageBundle\Exceptions\InvalidStorageConfiguration; use Symfony\Component\Config\Definition\Exception\Exception; /** @@ -10,7 +11,7 @@ use Symfony\Component\Config\Definition\Exception\Exception; * Usage: $storage = new S3Storage('my_storage_name', $config); * @author Maxime Renou */ -class S3Storage +class S3Storage implements StorageAdaptor { const MODE_PRIVATE = 'private'; const MODE_PUBLIC = 'public-read'; @@ -22,7 +23,7 @@ class S3Storage public function __construct ($storage_name, $config) { if (!($this->configIsNormed($config))) - throw new Exception("Error from config file :("); + throw new InvalidStorageConfiguration("Invalid $storage_name storage configuration"); $this->config = $config; $this->bucket = $config['bucket']; @@ -38,6 +39,12 @@ class S3Storage ]); } + public function mode($name) + { + if ($name == 'public') return self::MODE_PUBLIC; + return self::MODE_PRIVATE; + } + private function configIsNormed($config) { return ( diff --git a/StorageBundle/Adaptors/StorageAdaptor.php b/StorageBundle/Adaptors/StorageAdaptor.php new file mode 100644 index 0000000..37fcb35 --- /dev/null +++ b/StorageBundle/Adaptors/StorageAdaptor.php @@ -0,0 +1,13 @@ +getPropertyAnnotations($reflection); + + $storage_annotation = null; + + foreach ($annotations as $annotation) + { + if ($annotation instanceof StorageAnnotation) { + $storage_annotation = $annotation; + } + } + + if (is_null($storage_annotation)) + { + throw new MissingStorageAnnotation("Missing Storage annotation for $attribute in ".get_class($entity)); + } + + $file_hash = hash('sha256', time().$attribute.uniqid()); + $storage = $this->get($storage_annotation->name); + + if (is_null($storage_annotation)) + { + throw new UnknownStorage("Unknown storage for $attribute in ".get_class($entity)); + } + + $prefix = is_null($storage_annotation->prefix) || empty($storage_annotation->prefix) ? '' : trim($storage_annotation->prefix, '/').'/'; + + $mode = $storage->mode($storage_annotation->mode); + + if ($file instanceof UploadedFile) { + if (!is_null($storage_annotation->mime)) { + $valid = true; + if (count(explode('/', $storage_annotation->mime)) > 1) { + $valid = strtolower($storage_annotation->mime) == $file->getMimeType(); + } + else { + $valid = strtolower($storage_annotation->mime) == explode('/', $file->getMimeType())[0]; + } + if (!$valid) { + throw new MimeTypeException("Invalid mime type"); + } + } + $storage->store($file->getRealPath(), "$prefix$file_hash", $mode); + } + elseif (is_string($file) && file_exists($file)) { + $storage->store($file, "$prefix$file_hash", $mode); + } + else { + throw new InvalidFileException("Invalid file arguement"); + } + + $camel = lcfirst(Container::camelize($attribute)); + + $entity->{"set$camel"}($file_hash); + + return $file_hash; + } } diff --git a/composer.json b/composer.json index 333eefc..3d4ddac 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "type": "symfony-bundle", "require": { "php": ">=7.1", - "aws/aws-sdk-php": "^3.81" + "aws/aws-sdk-php": "^3.81", + "doctrine/event-manager": "*" }, "autoload": { "psr-4": {