From 92288eb1321d1eaba04d2c5890b963dbdf4d7f2e Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Fri, 27 Dec 2019 19:01:19 +0200 Subject: [PATCH] Improve storage device type hinting and abstraction --- src/Storage/Device.php | 132 +++-------------------- src/Storage/Devices/Local.php | 191 +++++++++++++++++++++++++++++++++- 2 files changed, 201 insertions(+), 122 deletions(-) diff --git a/src/Storage/Device.php b/src/Storage/Device.php index d111f4ca84..3015069b44 100644 --- a/src/Storage/Device.php +++ b/src/Storage/Device.php @@ -13,7 +13,7 @@ abstract class Device * * @return string */ - abstract public function getName(); + abstract public function getName():string; /** * Get Description. @@ -22,7 +22,7 @@ abstract class Device * * @return string */ - abstract public function getDescription(); + abstract public function getDescription():string; /** * Get Root. @@ -31,7 +31,7 @@ abstract class Device * * @return string */ - abstract public function getRoot(); + abstract public function getRoot():string; /** * Get Path. @@ -42,10 +42,7 @@ abstract class Device * * @return string */ - public function getPath($filename) - { - return $this->getRoot().DIRECTORY_SEPARATOR.$filename; - } + abstract public function getPath($filename):string; /** * Upload. @@ -59,30 +56,8 @@ abstract class Device * * @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'); - } - + abstract public function upload($target, $filename = ''); + /** * Read file by given path. * @@ -90,10 +65,7 @@ abstract class Device * * @return string */ - public function read(string $path):string - { - return file_get_contents($path); - } + abstract public function read(string $path):string; /** * Write file by given path. @@ -103,10 +75,7 @@ abstract class Device * * @return string */ - public function write(string $path, string $data):bool - { - return file_put_contents($path, $data); - } + abstract public function write(string $path, string $data):bool; /** * Delete file in given path, Return true on success and false on failure. @@ -117,36 +86,7 @@ abstract class Device * * @return bool */ - 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; - } + abstract public function delete(string $path):bool; /** * Returns given file path its size. @@ -157,10 +97,7 @@ abstract class Device * * @return int */ - public function getFileSize(string $path):int - { - return filesize($path); - } + abstract public function getFileSize(string $path):int; /** * Returns given file path its mime type. @@ -171,10 +108,7 @@ abstract class Device * * @return string */ - public function getFileMimeType(string $path):string - { - return mime_content_type($path); - } + abstract public function getFileMimeType(string $path):string; /** * Returns given file path its MD5 hash value. @@ -185,10 +119,7 @@ abstract class Device * * @return string */ - public function getFileHash(string $path):string - { - return md5_file($path); - } + abstract public function getFileHash(string $path):string; /** * Get directory size in bytes. @@ -201,34 +132,7 @@ abstract class Device * * @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; - } + abstract public function getDirectorySize(string $path):int; /** * Get Partition Free Space. @@ -237,10 +141,7 @@ abstract class Device * * @return float */ - public function getPartitionFreeSpace():float - { - return disk_free_space($this->getRoot()); - } + abstract public function getPartitionFreeSpace():float; /** * Get Partition Total Space. @@ -249,10 +150,7 @@ abstract class Device * * @return float */ - public function getPartitionTotalSpace():float - { - return disk_total_space($this->getRoot()); - } + abstract public function getPartitionTotalSpace():float; /** * Human readable data size format from bytes input. diff --git a/src/Storage/Devices/Local.php b/src/Storage/Devices/Local.php index ca1e103ccc..44652ed837 100644 --- a/src/Storage/Devices/Local.php +++ b/src/Storage/Devices/Local.php @@ -24,7 +24,7 @@ class Local extends Device /** * @return string */ - public function getName() + public function getName():string { return 'Local Storage'; } @@ -32,7 +32,7 @@ class Local extends Device /** * @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.'; } @@ -40,9 +40,9 @@ class Local extends Device /** * @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 */ - public function getPath($filename) + public function getPath($filename):string { $path = ''; @@ -60,4 +60,185 @@ class Local extends Device 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()); + } }