From 315ad01073f93a922441fbd053d486fc06833423 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Sat, 9 Mar 2013 05:08:06 +0100 Subject: [PATCH] EZP-20321: Refactored MimeTypeDetector --- .../MimeTypeDetector/FileInfo.php} | 20 +++++----- .../Tests/MimeTypeDetector/FileInfoTest.php | 38 +++++++++++++++++++ eZ/Publish/Core/IO/Values/BinaryFile.php | 8 ++++ .../Core/IO/Values/BinaryFileCreateStruct.php | 8 ++++ eZ/Publish/SPI/IO/BinaryFile.php | 8 ++++ .../BinaryBase => IO}/MimeTypeDetector.php | 13 ++++++- 6 files changed, 82 insertions(+), 13 deletions(-) rename eZ/Publish/Core/{FieldType/BinaryBase/MimeTypeDetector/FileInfoDetector.php => IO/MimeTypeDetector/FileInfo.php} (76%) create mode 100644 eZ/Publish/Core/IO/Tests/MimeTypeDetector/FileInfoTest.php rename eZ/Publish/SPI/{FieldType/BinaryBase => IO}/MimeTypeDetector.php (62%) diff --git a/eZ/Publish/Core/FieldType/BinaryBase/MimeTypeDetector/FileInfoDetector.php b/eZ/Publish/Core/IO/MimeTypeDetector/FileInfo.php similarity index 76% rename from eZ/Publish/Core/FieldType/BinaryBase/MimeTypeDetector/FileInfoDetector.php rename to eZ/Publish/Core/IO/MimeTypeDetector/FileInfo.php index 2995049d778..ecdb42cee7e 100644 --- a/eZ/Publish/Core/FieldType/BinaryBase/MimeTypeDetector/FileInfoDetector.php +++ b/eZ/Publish/Core/IO/MimeTypeDetector/FileInfo.php @@ -7,11 +7,11 @@ * @version //autogentag// */ -namespace eZ\Publish\Core\FieldType\BinaryBase\MimeTypeDetector; +namespace eZ\Publish\Core\IO\MimeTypeDetector; -use eZ\Publish\SPI\FieldType\BinaryBase\MimeTypeDetector; +use eZ\Publish\SPI\IO\MimeTypeDetector; -class FileInfoDetector implements MimeTypeDetector +class FileInfo implements MimeTypeDetector { /** * Magic FileInfo object @@ -33,18 +33,16 @@ public function __construct() } } - /** - * Returns the MIME type of the file identified by $path - * - * @param string $path - * - * @return string - */ - public function getMimeType( $path ) + public function getFromPath( $path ) { return $this->getFileInfo()->file( $path ); } + public function getFromBuffer( $path ) + { + return $this->getFileInfo()->buffer( $path ); + } + /** * Creates a new (or re-uses) finfo object and returns it * diff --git a/eZ/Publish/Core/IO/Tests/MimeTypeDetector/FileInfoTest.php b/eZ/Publish/Core/IO/Tests/MimeTypeDetector/FileInfoTest.php new file mode 100644 index 00000000000..17c64d43cc3 --- /dev/null +++ b/eZ/Publish/Core/IO/Tests/MimeTypeDetector/FileInfoTest.php @@ -0,0 +1,38 @@ +mimeTypeDetector = new MimeTypeDetector; + } + + public function testGetFromPath() + { + self::assertEquals( + $this->mimeTypeDetector->getFromPath( __FILE__ ), + 'text/x-php' + ); + } + + public function testGetFromBuffer() + { + self::assertEquals( + $this->mimeTypeDetector->getFromBuffer( file_get_contents( __FILE__ ) ), + 'text/x-php' + ); + } +} diff --git a/eZ/Publish/Core/IO/Values/BinaryFile.php b/eZ/Publish/Core/IO/Values/BinaryFile.php index 632ce87361b..a8cff4fc116 100644 --- a/eZ/Publish/Core/IO/Values/BinaryFile.php +++ b/eZ/Publish/Core/IO/Values/BinaryFile.php @@ -39,4 +39,12 @@ class BinaryFile extends ValueObject * @var string */ protected $uri; + + /** + * The file's mime type + * Example: text/xml + * @var string + */ + public $mimeType; + } diff --git a/eZ/Publish/Core/IO/Values/BinaryFileCreateStruct.php b/eZ/Publish/Core/IO/Values/BinaryFileCreateStruct.php index a698cd8a473..18bdecd8751 100644 --- a/eZ/Publish/Core/IO/Values/BinaryFileCreateStruct.php +++ b/eZ/Publish/Core/IO/Values/BinaryFileCreateStruct.php @@ -33,4 +33,12 @@ class BinaryFileCreateStruct extends ValueObject * @var resource */ public $inputStream; + + /** + * The file's mime type + * If not provided, will be auto-detected by the IOService + * Example: text/xml + * @var string + */ + public $mimeType; } diff --git a/eZ/Publish/SPI/IO/BinaryFile.php b/eZ/Publish/SPI/IO/BinaryFile.php index 357197cca42..25eb3f1f06e 100644 --- a/eZ/Publish/SPI/IO/BinaryFile.php +++ b/eZ/Publish/SPI/IO/BinaryFile.php @@ -37,4 +37,12 @@ class BinaryFile * @var string */ public $uri; + + /** + * The file's mime type + * If not provided, will be auto-detected by the IOService + * Example: text/xml + * @var string + */ + public $mimeType; } diff --git a/eZ/Publish/SPI/FieldType/BinaryBase/MimeTypeDetector.php b/eZ/Publish/SPI/IO/MimeTypeDetector.php similarity index 62% rename from eZ/Publish/SPI/FieldType/BinaryBase/MimeTypeDetector.php rename to eZ/Publish/SPI/IO/MimeTypeDetector.php index f014a6a680d..404997b1f8d 100644 --- a/eZ/Publish/SPI/FieldType/BinaryBase/MimeTypeDetector.php +++ b/eZ/Publish/SPI/IO/MimeTypeDetector.php @@ -7,7 +7,7 @@ * @version //autogentag// */ -namespace eZ\Publish\SPI\FieldType\BinaryBase; +namespace eZ\Publish\SPI\IO; interface MimeTypeDetector { @@ -18,5 +18,14 @@ interface MimeTypeDetector * * @return string */ - public function getMimeType( $path ); + public function getFromPath( $path ); + + /** + * Returns the MIME type of the data in $buffer + * + * @param string $buffer + * + * @return string + */ + public function getFromBuffer( $buffer ); }