Improve storage device type hinting and abstraction
This commit is contained in:
parent
d89e591476
commit
92288eb132
2 changed files with 201 additions and 122 deletions
|
@ -13,7 +13,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
abstract public function getName();
|
abstract public function getName():string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Description.
|
* Get Description.
|
||||||
|
@ -22,7 +22,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
abstract public function getDescription();
|
abstract public function getDescription():string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Root.
|
* Get Root.
|
||||||
|
@ -31,7 +31,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
abstract public function getRoot();
|
abstract public function getRoot():string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Path.
|
* Get Path.
|
||||||
|
@ -42,10 +42,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getPath($filename)
|
abstract public function getPath($filename):string;
|
||||||
{
|
|
||||||
return $this->getRoot().DIRECTORY_SEPARATOR.$filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Upload.
|
* Upload.
|
||||||
|
@ -59,29 +56,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return string|bool saved destination on success or false on failures
|
* @return string|bool saved destination on success or false on failures
|
||||||
*/
|
*/
|
||||||
public function upload($target, $filename = '')
|
abstract public function upload($target, $filename = '');
|
||||||
{
|
|
||||||
$filename = (empty($filename)) ? $target : $filename;
|
|
||||||
$filename = uniqid().'.'.pathinfo($filename, PATHINFO_EXTENSION);
|
|
||||||
|
|
||||||
$path = $this->getPath($filename);
|
|
||||||
|
|
||||||
if (!is_uploaded_file($target)) {
|
|
||||||
throw new Exception('File is not a valid uploaded file');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!file_exists(dirname($path))) { // Checks if directory path to file exists
|
|
||||||
if (!@mkdir(dirname($path), 0755, true)) {
|
|
||||||
throw new Exception('Can\'t create directory '.dirname($path));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (move_uploaded_file($target, $path)) {
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception('Upload failed');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read file by given path.
|
* Read file by given path.
|
||||||
|
@ -90,10 +65,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function read(string $path):string
|
abstract public function read(string $path):string;
|
||||||
{
|
|
||||||
return file_get_contents($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write file by given path.
|
* Write file by given path.
|
||||||
|
@ -103,10 +75,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function write(string $path, string $data):bool
|
abstract public function write(string $path, string $data):bool;
|
||||||
{
|
|
||||||
return file_put_contents($path, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete file in given path, Return true on success and false on failure.
|
* Delete file in given path, Return true on success and false on failure.
|
||||||
|
@ -117,36 +86,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function delete(string $path):bool
|
abstract public function delete(string $path):bool;
|
||||||
{
|
|
||||||
return unlink($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete all file and directories in given path, Return true on success and false on failure.
|
|
||||||
*
|
|
||||||
* @see https://paulund.co.uk/php-delete-directory-and-files-in-directory
|
|
||||||
*
|
|
||||||
* @param string $path
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function deleteDir($target):bool
|
|
||||||
{
|
|
||||||
if (is_dir($target)) {
|
|
||||||
$files = glob($target.'*', GLOB_MARK); // GLOB_MARK adds a slash to directories returned
|
|
||||||
|
|
||||||
foreach ($files as $file) {
|
|
||||||
$this->deleteDir($file);
|
|
||||||
}
|
|
||||||
|
|
||||||
rmdir($target);
|
|
||||||
} elseif (is_file($target)) {
|
|
||||||
unlink($target);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns given file path its size.
|
* Returns given file path its size.
|
||||||
|
@ -157,10 +97,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getFileSize(string $path):int
|
abstract public function getFileSize(string $path):int;
|
||||||
{
|
|
||||||
return filesize($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns given file path its mime type.
|
* Returns given file path its mime type.
|
||||||
|
@ -171,10 +108,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getFileMimeType(string $path):string
|
abstract public function getFileMimeType(string $path):string;
|
||||||
{
|
|
||||||
return mime_content_type($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns given file path its MD5 hash value.
|
* Returns given file path its MD5 hash value.
|
||||||
|
@ -185,10 +119,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getFileHash(string $path):string
|
abstract public function getFileHash(string $path):string;
|
||||||
{
|
|
||||||
return md5_file($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get directory size in bytes.
|
* Get directory size in bytes.
|
||||||
|
@ -201,34 +132,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getDirectorySize(string $path):int
|
abstract public function getDirectorySize(string $path):int;
|
||||||
{
|
|
||||||
$size = 0;
|
|
||||||
|
|
||||||
$directory = opendir($path);
|
|
||||||
|
|
||||||
if (!$directory) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (($file = readdir($directory)) !== false) {
|
|
||||||
// Skip file pointers
|
|
||||||
if ($file[0] == '.') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Go recursive down, or add the file size
|
|
||||||
if (is_dir($path.$file)) {
|
|
||||||
$size += $this->getDirectorySize($path.$file.DIRECTORY_SEPARATOR);
|
|
||||||
} else {
|
|
||||||
$size += filesize($path.$file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
closedir($directory);
|
|
||||||
|
|
||||||
return $size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Partition Free Space.
|
* Get Partition Free Space.
|
||||||
|
@ -237,10 +141,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return float
|
* @return float
|
||||||
*/
|
*/
|
||||||
public function getPartitionFreeSpace():float
|
abstract public function getPartitionFreeSpace():float;
|
||||||
{
|
|
||||||
return disk_free_space($this->getRoot());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Partition Total Space.
|
* Get Partition Total Space.
|
||||||
|
@ -249,10 +150,7 @@ abstract class Device
|
||||||
*
|
*
|
||||||
* @return float
|
* @return float
|
||||||
*/
|
*/
|
||||||
public function getPartitionTotalSpace():float
|
abstract public function getPartitionTotalSpace():float;
|
||||||
{
|
|
||||||
return disk_total_space($this->getRoot());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Human readable data size format from bytes input.
|
* Human readable data size format from bytes input.
|
||||||
|
|
|
@ -24,7 +24,7 @@ class Local extends Device
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName():string
|
||||||
{
|
{
|
||||||
return 'Local Storage';
|
return 'Local Storage';
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ class Local extends Device
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getDescription()
|
public function getDescription():string
|
||||||
{
|
{
|
||||||
return 'Adapter for Local storage that is in the physical or virtual machine or mounted to it.';
|
return 'Adapter for Local storage that is in the physical or virtual machine or mounted to it.';
|
||||||
}
|
}
|
||||||
|
@ -40,9 +40,9 @@ class Local extends Device
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getRoot()
|
public function getRoot():string
|
||||||
{
|
{
|
||||||
return '/storage/uploads/'.$this->root;
|
return $this->root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,7 +50,7 @@ class Local extends Device
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getPath($filename)
|
public function getPath($filename):string
|
||||||
{
|
{
|
||||||
$path = '';
|
$path = '';
|
||||||
|
|
||||||
|
@ -60,4 +60,185 @@ class Local extends Device
|
||||||
|
|
||||||
return $this->getRoot().$path.DIRECTORY_SEPARATOR.$filename;
|
return $this->getRoot().$path.DIRECTORY_SEPARATOR.$filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload.
|
||||||
|
*
|
||||||
|
* Upload a file to desired destination in the selected disk.
|
||||||
|
*
|
||||||
|
* @param string $target
|
||||||
|
* @param string $filename
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*
|
||||||
|
* @return string|bool saved destination on success or false on failures
|
||||||
|
*/
|
||||||
|
public function upload($target, $filename = '')
|
||||||
|
{
|
||||||
|
$filename = (empty($filename)) ? $target : $filename;
|
||||||
|
$filename = uniqid().'.'.pathinfo($filename, PATHINFO_EXTENSION);
|
||||||
|
|
||||||
|
$path = $this->getPath($filename);
|
||||||
|
|
||||||
|
if (!is_uploaded_file($target)) {
|
||||||
|
throw new Exception('File is not a valid uploaded file');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file_exists(dirname($path))) { // Checks if directory path to file exists
|
||||||
|
if (!@mkdir(dirname($path), 0755, true)) {
|
||||||
|
throw new Exception('Can\'t create directory '.dirname($path));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move_uploaded_file($target, $path)) {
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception('Upload failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read file by given path.
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function read(string $path):string
|
||||||
|
{
|
||||||
|
return file_get_contents($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write file by given path.
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param string $data
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function write(string $path, string $data):bool
|
||||||
|
{
|
||||||
|
return file_put_contents($path, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete file in given path, Return true on success and false on failure.
|
||||||
|
*
|
||||||
|
* @see http://php.net/manual/en/function.filesize.php
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function delete(string $path):bool
|
||||||
|
{
|
||||||
|
return unlink($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns given file path its size.
|
||||||
|
*
|
||||||
|
* @see http://php.net/manual/en/function.filesize.php
|
||||||
|
*
|
||||||
|
* @param $path
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getFileSize(string $path):int
|
||||||
|
{
|
||||||
|
return filesize($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns given file path its mime type.
|
||||||
|
*
|
||||||
|
* @see http://php.net/manual/en/function.mime-content-type.php
|
||||||
|
*
|
||||||
|
* @param $path
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFileMimeType(string $path):string
|
||||||
|
{
|
||||||
|
return mime_content_type($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns given file path its MD5 hash value.
|
||||||
|
*
|
||||||
|
* @see http://php.net/manual/en/function.md5-file.php
|
||||||
|
*
|
||||||
|
* @param $path
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFileHash(string $path):string
|
||||||
|
{
|
||||||
|
return md5_file($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get directory size in bytes.
|
||||||
|
*
|
||||||
|
* Return -1 on error
|
||||||
|
*
|
||||||
|
* Based on http://www.jonasjohn.de/snippets/php/dir-size.htm
|
||||||
|
*
|
||||||
|
* @param $path
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getDirectorySize(string $path):int
|
||||||
|
{
|
||||||
|
$size = 0;
|
||||||
|
|
||||||
|
$directory = opendir($path);
|
||||||
|
|
||||||
|
if (!$directory) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (($file = readdir($directory)) !== false) {
|
||||||
|
// Skip file pointers
|
||||||
|
if ($file[0] == '.') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Go recursive down, or add the file size
|
||||||
|
if (is_dir($path.$file)) {
|
||||||
|
$size += $this->getDirectorySize($path.$file.DIRECTORY_SEPARATOR);
|
||||||
|
} else {
|
||||||
|
$size += filesize($path.$file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir($directory);
|
||||||
|
|
||||||
|
return $size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Partition Free Space.
|
||||||
|
*
|
||||||
|
* disk_free_space — Returns available space on filesystem or disk partition
|
||||||
|
*
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function getPartitionFreeSpace():float
|
||||||
|
{
|
||||||
|
return disk_free_space($this->getRoot());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Partition Total Space.
|
||||||
|
*
|
||||||
|
* disk_total_space — Returns the total size of a filesystem or disk partition
|
||||||
|
*
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function getPartitionTotalSpace():float
|
||||||
|
{
|
||||||
|
return disk_total_space($this->getRoot());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue