Add abstract storage layer
This commit is contained in:
150
app/Core/ObjectStorage/FileStorage.php
Normal file
150
app/Core/ObjectStorage/FileStorage.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Core\ObjectStorage;
|
||||
|
||||
/**
|
||||
* Local File Storage
|
||||
*
|
||||
* @package ObjectStorage
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class FileStorage implements ObjectStorageInterface
|
||||
{
|
||||
/**
|
||||
* Base path
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $path = '';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
* @param string $path
|
||||
*/
|
||||
public function __construct($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch object contents
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$filename = $this->path.DIRECTORY_SEPARATOR.$key;
|
||||
|
||||
if (! file_exists($filename)) {
|
||||
throw new ObjectStorageException('File not found: '.$filename);
|
||||
}
|
||||
|
||||
return file_get_contents($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save object
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @param string $blob
|
||||
* @return string
|
||||
*/
|
||||
public function put($key, &$blob)
|
||||
{
|
||||
$this->createFolder($key);
|
||||
|
||||
if (file_put_contents($this->path.DIRECTORY_SEPARATOR.$key, $blob) === false) {
|
||||
throw new ObjectStorageException('Unable to write the file: '.$this->path.DIRECTORY_SEPARATOR.$key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Output directly object content
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
*/
|
||||
public function passthru($key)
|
||||
{
|
||||
$filename = $this->path.DIRECTORY_SEPARATOR.$key;
|
||||
|
||||
if (! file_exists($filename)) {
|
||||
throw new ObjectStorageException('File not found: '.$filename);
|
||||
}
|
||||
|
||||
return readfile($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move local file to object storage
|
||||
*
|
||||
* @access public
|
||||
* @param string $filename
|
||||
* @param string $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function moveFile($src_filename, $key)
|
||||
{
|
||||
$this->createFolder($key);
|
||||
$dst_filename = $this->path.DIRECTORY_SEPARATOR.$key;
|
||||
|
||||
if (! rename($src_filename, $dst_filename)) {
|
||||
throw new ObjectStorageException('Unable to move the file: '.$src_filename.' to '.$dst_filename);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move uploaded file to object storage
|
||||
*
|
||||
* @access public
|
||||
* @param string $filename
|
||||
* @param string $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function moveUploadedFile($filename, $key)
|
||||
{
|
||||
$this->createFolder($key);
|
||||
return move_uploaded_file($filename, $this->path.DIRECTORY_SEPARATOR.$key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove object
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function remove($key)
|
||||
{
|
||||
$filename = $this->path.DIRECTORY_SEPARATOR.$key;
|
||||
|
||||
if (file_exists($filename)) {
|
||||
return unlink($filename);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create object folder
|
||||
*
|
||||
* @access private
|
||||
* @param string $key
|
||||
*/
|
||||
private function createFolder($key)
|
||||
{
|
||||
$folder = $this->path.DIRECTORY_SEPARATOR.dirname($key);
|
||||
|
||||
if (! is_dir($folder) && ! mkdir($folder, 0755, true)) {
|
||||
throw new ObjectStorageException('Unable to create folder: '.$folder);
|
||||
}
|
||||
}
|
||||
}
|
||||
9
app/Core/ObjectStorage/ObjectStorageException.php
Normal file
9
app/Core/ObjectStorage/ObjectStorageException.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Core\ObjectStorage;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ObjectStorageException extends Exception
|
||||
{
|
||||
}
|
||||
68
app/Core/ObjectStorage/ObjectStorageInterface.php
Normal file
68
app/Core/ObjectStorage/ObjectStorageInterface.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Core\ObjectStorage;
|
||||
|
||||
/**
|
||||
* Object Storage Interface
|
||||
*
|
||||
* @package ObjectStorage
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
interface ObjectStorageInterface
|
||||
{
|
||||
/**
|
||||
* Fetch object contents
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
public function get($key);
|
||||
|
||||
/**
|
||||
* Save object
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @param string $blob
|
||||
* @return string
|
||||
*/
|
||||
public function put($key, &$blob);
|
||||
|
||||
/**
|
||||
* Output directly object content
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
*/
|
||||
public function passthru($key);
|
||||
|
||||
/**
|
||||
* Move local file to object storage
|
||||
*
|
||||
* @access public
|
||||
* @param string $filename
|
||||
* @param string $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function moveFile($filename, $key);
|
||||
|
||||
/**
|
||||
* Move uploaded file to object storage
|
||||
*
|
||||
* @access public
|
||||
* @param string $filename
|
||||
* @param string $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function moveUploadedFile($filename, $key);
|
||||
|
||||
/**
|
||||
* Remove object
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function remove($key);
|
||||
}
|
||||
@@ -72,4 +72,82 @@ class Tool
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a jpeg thumbnail from an image
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param string $src_file Source file image
|
||||
* @param string $dst_file Destination file image
|
||||
* @param integer $resize_width Desired image width
|
||||
* @param integer $resize_height Desired image height
|
||||
*/
|
||||
public static function generateThumbnail($src_file, $dst_file, $resize_width = 250, $resize_height = 100)
|
||||
{
|
||||
$metadata = getimagesize($src_file);
|
||||
$src_width = $metadata[0];
|
||||
$src_height = $metadata[1];
|
||||
$dst_y = 0;
|
||||
$dst_x = 0;
|
||||
|
||||
if (empty($metadata['mime'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($resize_width == 0 && $resize_height == 0) {
|
||||
$resize_width = 100;
|
||||
$resize_height = 100;
|
||||
}
|
||||
|
||||
if ($resize_width > 0 && $resize_height == 0) {
|
||||
$dst_width = $resize_width;
|
||||
$dst_height = floor($src_height * ($resize_width / $src_width));
|
||||
$dst_image = imagecreatetruecolor($dst_width, $dst_height);
|
||||
}
|
||||
elseif ($resize_width == 0 && $resize_height > 0) {
|
||||
$dst_width = floor($src_width * ($resize_height / $src_height));
|
||||
$dst_height = $resize_height;
|
||||
$dst_image = imagecreatetruecolor($dst_width, $dst_height);
|
||||
}
|
||||
else {
|
||||
|
||||
$src_ratio = $src_width / $src_height;
|
||||
$resize_ratio = $resize_width / $resize_height;
|
||||
|
||||
if ($src_ratio <= $resize_ratio) {
|
||||
$dst_width = $resize_width;
|
||||
$dst_height = floor($src_height * ($resize_width / $src_width));
|
||||
|
||||
$dst_y = ($dst_height - $resize_height) / 2 * (-1);
|
||||
}
|
||||
else {
|
||||
$dst_width = floor($src_width * ($resize_height / $src_height));
|
||||
$dst_height = $resize_height;
|
||||
|
||||
$dst_x = ($dst_width - $resize_width) / 2 * (-1);
|
||||
}
|
||||
|
||||
$dst_image = imagecreatetruecolor($resize_width, $resize_height);
|
||||
}
|
||||
|
||||
switch ($metadata['mime']) {
|
||||
case 'image/jpeg':
|
||||
case 'image/jpg':
|
||||
$src_image = imagecreatefromjpeg($src_file);
|
||||
break;
|
||||
case 'image/png':
|
||||
$src_image = imagecreatefrompng($src_file);
|
||||
break;
|
||||
case 'image/gif':
|
||||
$src_image = imagecreatefromgif($src_file);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
|
||||
imagejpeg($dst_image, $dst_file);
|
||||
imagedestroy($dst_image);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user