Skip to content

Commit

Permalink
EZP-20321: Refactored MimeTypeDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier committed Mar 9, 2013
1 parent fe9b3db commit 315ad01
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down
38 changes: 38 additions & 0 deletions eZ/Publish/Core/IO/Tests/MimeTypeDetector/FileInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* File containing the FileInfoTest class.
*
* @copyright Copyright (C) 2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/
namespace eZ\Publish\Core\IO\Tests\MimeTypeDetector;

use eZ\Publish\Core\IO\MimeTypeDetector\FileInfo as MimeTypeDetector;

class FileInfoTest extends \PHPUnit_Framework_TestCase
{
/** @var MimeTypeDetector */
protected $mimeTypeDetector;

public function setUp()
{
$this->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'
);
}
}
8 changes: 8 additions & 0 deletions eZ/Publish/Core/IO/Values/BinaryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ class BinaryFile extends ValueObject
* @var string
*/
protected $uri;

/**
* The file's mime type
* Example: text/xml
* @var string
*/
public $mimeType;

}
8 changes: 8 additions & 0 deletions eZ/Publish/Core/IO/Values/BinaryFileCreateStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 8 additions & 0 deletions eZ/Publish/SPI/IO/BinaryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @version //autogentag//
*/

namespace eZ\Publish\SPI\FieldType\BinaryBase;
namespace eZ\Publish\SPI\IO;

interface MimeTypeDetector
{
Expand All @@ -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 );
}

0 comments on commit 315ad01

Please sign in to comment.