diff --git a/DependencyInjection/IntaroFileUploaderExtension.php b/DependencyInjection/IntaroFileUploaderExtension.php index b0902f0..14e92e8 100644 --- a/DependencyInjection/IntaroFileUploaderExtension.php +++ b/DependencyInjection/IntaroFileUploaderExtension.php @@ -2,17 +2,17 @@ namespace Intaro\FileUploaderBundle\DependencyInjection; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; -use Symfony\Component\DependencyInjection\Loader; -use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; +use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; class IntaroFileUploaderExtension extends Extension implements PrependExtensionInterface { - public function prepend(ContainerBuilder $container) + public function prepend(ContainerBuilder $container): void { $configs = $container->getExtensionConfig($this->getAlias()); $config = $this->processConfiguration(new Configuration(), $configs); @@ -31,21 +31,20 @@ public function prepend(ContainerBuilder $container) '%intaro_file_uploader.class%', [ new Reference("gaufrette.{$name}_filesystem"), - new Reference("router"), $options['path'], - $options['allowed_types'] + $options['allowed_types'], ] )); } } } - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('services.yml'); } @@ -53,7 +52,7 @@ protected function generateGaufretteConfig($config) { $filesystems = []; $adapters = []; - + if (isset($config['uploaders'])) { foreach (array_filter($config['uploaders']) as $uploaderType => $uploaders) { foreach ($uploaders as $name => $options) { @@ -62,18 +61,18 @@ protected function generateGaufretteConfig($config) $options['path'] ); $filesystems[$name] = [ - 'adapter' => $name + 'adapter' => $name, ]; $adapters[$name] = [ - $uploaderType => $options + $uploaderType => $options, ]; } } } - + $gaufretteConfig = [ 'adapters' => $adapters, - 'filesystems' => $filesystems + 'filesystems' => $filesystems, ]; return $gaufretteConfig; diff --git a/Services/FileUploader.php b/Services/FileUploader.php index e5c76a8..23360c9 100644 --- a/Services/FileUploader.php +++ b/Services/FileUploader.php @@ -1,53 +1,35 @@ filesystem = $filesystem; $this->path = $path; $this->allowedTypes = $allowedTypes; - $this->router = $router; $this->translator = \Transliterator::create('Any-Latin;Latin-ASCII;Lower;[\u0080-\u7fff] remove'); } /** - * Upload method - * - * @param UploadedFile $file file to upload - * - * @return string * @throws \InvalidArgumentException */ - public function upload(UploadedFile $file) + public function upload(UploadedFile $file): string { $fileMimeType = $file->getMimeType(); if ($this->allowedTypes && !in_array($fileMimeType, $this->allowedTypes)) { @@ -72,55 +54,7 @@ public function upload(UploadedFile $file) return $filename; } - /** - * Generate new filename based on original name - * - * @param string $originalName - * @return string - */ - public function generateNameByOriginal($originalName) - { - return sprintf( - '%s-%s', - uniqid(), - $this->clearName($originalName) - ); - } - - /** - * Name cleanup - * @param $originalName - * @return string - */ - protected function clearName($originalName) - { - //basic check on URL encoding - if (urldecode($originalName) !== $originalName) { - $originalName = urldecode($originalName); - } - - $originalName = preg_replace( - '/[\+\\/\%\#\?]+/', - '_', - $originalName - ); - - return preg_replace( - '/\s+/', - '-', - $this->translator->transliterate($originalName) - ); - } - - /** - * Upload local file to storage - * - * @access public - * @param mixed $pathname - * @param bool $unlinkAfterUpload (default: true) - * @return string - */ - public function uploadByPath($pathname, $unlinkAfterUpload = true) + public function uploadByPath(string $pathname, bool $unlinkAfterUpload = true): string { $file = new File($pathname); $filename = $file->getBasename(); @@ -150,34 +84,86 @@ public function uploadByPath($pathname, $unlinkAfterUpload = true) return $filename; } - public function remove($name) + public function uploadByContent(string $fileContent, string $filename, string $mimeType): string + { + if ($this->allowedTypes && !in_array($mimeType, $this->allowedTypes)) { + throw new \InvalidArgumentException( + sprintf('Files of type %s are not allowed.', $mimeType) + ); + } + + $adapter = $this->filesystem->getAdapter(); + + if (!($adapter instanceof Local)) { + $adapter->setMetadata( + $filename, + ['contentType' => $mimeType] + ); + } + + $adapter->write($filename, $fileContent); + + return $filename; + } + + public function generateNameByOriginal(string $originalName): string + { + return sprintf( + '%s-%s', + uniqid(), + $this->clearName($originalName) + ); + } + + protected function clearName(string $originalName): string + { + //basic check on URL encoding + if (urldecode($originalName) !== $originalName) { + $originalName = urldecode($originalName); + } + + $originalName = preg_replace( + '/[\+\\/\%\#\?]+/', + '_', + $originalName + ); + + return preg_replace( + '/\s+/', + '-', + $this->translator->transliterate($originalName) + ); + } + + public function remove($name): bool { return $this->filesystem->delete($name); } - public function getUrl($name) + public function getUrl($name): string { - return $this->getPath().$name; + return $this->getPath() . $name; } - public function listFiles() + /** @return string[] */ + public function listFiles(): array { $files = []; $keys = $this->filesystem->listKeys(); $adapter = $this->filesystem->getAdapter(); if ($adapter instanceof AwsS3) { - if (sizeof($keys) > 0) { + if (count($keys) > 0) { foreach ($keys as $file) { $filename = basename($file); if ($filename) { - $files[] = $this->getPath().$filename; + $files[] = $this->getPath() . $filename; } } } } elseif ($adapter instanceof Local) { - if (isset($keys['keys']) && sizeof($keys['keys']) > 0) { + if (isset($keys['keys']) && count($keys['keys']) > 0) { foreach ($keys['keys'] as $file) { - $files[] = $this->getPath().$file; + $files[] = $this->getPath() . $file; } } } @@ -185,24 +171,24 @@ public function listFiles() return $files; } - public function getFilesystem() + public function getFilesystem(): Filesystem { return $this->filesystem; } - public function setFilesystem($filesystem) + public function setFilesystem(Filesystem $filesystem): self { $this->filesystem = $filesystem; return $this; } - public function getPath() + public function getPath(): string { return $this->path; } - public function getAllowedTypes() + public function getAllowedTypes(): ?array { return $this->allowedTypes; } diff --git a/composer.json b/composer.json index 7fb0540..b567a64 100644 --- a/composer.json +++ b/composer.json @@ -11,12 +11,12 @@ } ], "require": { - "knplabs/knp-gaufrette-bundle": "~0.3" + "knplabs/knp-gaufrette-bundle": "~0.3", + "php": "^7.4" }, "autoload": { - "psr-0": { + "psr-4": { "Intaro\\FileUploaderBundle\\": "" } - }, - "target-dir" : "Intaro/FileUploaderBundle" + } }