2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Storage;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
abstract class Device
|
|
|
|
{
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get Name.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Get storage device name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getName();
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get Description.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Get storage device description and purpose.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getDescription();
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get Root.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Get storage device root path
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getRoot();
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get Path.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Each device hold a complex directory structure that is being build in this method.
|
|
|
|
*
|
|
|
|
* @param $filename
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPath($filename)
|
|
|
|
{
|
2019-09-07 05:04:26 +12:00
|
|
|
return $this->getRoot().DIRECTORY_SEPARATOR.$filename;
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Upload.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Upload a file to desired destination in the selected disk.
|
|
|
|
*
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param string $target
|
|
|
|
* @param string $filename
|
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @throws \Exception
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string|bool saved destination on success or false on failures
|
|
|
|
*/
|
|
|
|
public function upload($target, $filename = '')
|
|
|
|
{
|
2019-09-07 05:04:26 +12:00
|
|
|
$filename = (empty($filename)) ? $target : $filename;
|
|
|
|
$filename = uniqid().'.'.pathinfo($filename, PATHINFO_EXTENSION);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
$path = $this->getPath($filename);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!is_uploaded_file($target)) {
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new Exception('File is not a valid uploaded file');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists(dirname($path))) { // Checks if directory path to file exists
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!mkdir(dirname($path), 0755, true)) {
|
|
|
|
throw new Exception('Can\'t create directory '.dirname($path));
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (move_uploaded_file($target, $path)) {
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Exception('Upload failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Read file by given path.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* @param string $path
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function read(string $path):string
|
|
|
|
{
|
|
|
|
return file_get_contents($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Write file by given path.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $data
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-08-23 08:54:05 +12:00
|
|
|
public function write(string $path, string $data):bool
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
|
|
|
return file_put_contents($path, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Delete file in given path, Return true on success and false on failure.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* @see http://php.net/manual/en/function.filesize.php
|
|
|
|
*
|
|
|
|
* @param string $path
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function delete(string $path):bool
|
|
|
|
{
|
|
|
|
return unlink($path);
|
|
|
|
}
|
|
|
|
|
2019-08-23 08:54:05 +12:00
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Delete all file and directories in given path, Return true on success and false on failure.
|
2019-08-23 08:54:05 +12:00
|
|
|
*
|
|
|
|
* @see https://paulund.co.uk/php-delete-directory-and-files-in-directory
|
|
|
|
*
|
|
|
|
* @param string $path
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-08-23 08:54:05 +12:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function deleteDir($target):bool
|
|
|
|
{
|
|
|
|
if (is_dir($target)) {
|
2019-09-07 05:04:26 +12:00
|
|
|
$files = glob($target.'*', GLOB_MARK); // GLOB_MARK adds a slash to directories returned
|
|
|
|
|
2019-08-23 08:54:05 +12:00
|
|
|
foreach ($files as $file) {
|
2019-09-07 05:04:26 +12:00
|
|
|
$this->deleteDir($file);
|
2019-08-23 08:54:05 +12:00
|
|
|
}
|
2019-09-07 05:04:26 +12:00
|
|
|
|
2019-08-23 08:54:05 +12:00
|
|
|
rmdir($target);
|
2019-09-07 05:04:26 +12:00
|
|
|
} elseif (is_file($target)) {
|
|
|
|
unlink($target);
|
2019-08-23 08:54:05 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Returns given file path its size.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* @see http://php.net/manual/en/function.filesize.php
|
|
|
|
*
|
|
|
|
* @param $path
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getFileSize(string $path):int
|
|
|
|
{
|
|
|
|
return filesize($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Returns given file path its mime type.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* @see http://php.net/manual/en/function.mime-content-type.php
|
|
|
|
*
|
|
|
|
* @param $path
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileMimeType(string $path):string
|
|
|
|
{
|
|
|
|
return mime_content_type($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Returns given file path its MD5 hash value.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* @see http://php.net/manual/en/function.md5-file.php
|
|
|
|
*
|
|
|
|
* @param $path
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileHash(string $path):string
|
|
|
|
{
|
|
|
|
return md5_file($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get directory size in bytes.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Return -1 on error
|
|
|
|
*
|
|
|
|
* Based on http://www.jonasjohn.de/snippets/php/dir-size.htm
|
|
|
|
*
|
|
|
|
* @param $path
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getDirectorySize(string $path):int
|
|
|
|
{
|
|
|
|
$size = 0;
|
|
|
|
|
|
|
|
$directory = opendir($path);
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!$directory) {
|
2019-05-09 18:54:39 +12:00
|
|
|
return -1;
|
2019-09-07 05:04:26 +12:00
|
|
|
}
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
while (($file = readdir($directory)) !== false) {
|
|
|
|
// Skip file pointers
|
2019-09-07 05:04:26 +12:00
|
|
|
if ($file[0] == '.') {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
// Go recursive down, or add the file size
|
2019-09-07 05:04:26 +12:00
|
|
|
if (is_dir($path.$file)) {
|
|
|
|
$size += $this->getDirectorySize($path.$file.DIRECTORY_SEPARATOR);
|
|
|
|
} else {
|
|
|
|
$size += filesize($path.$file);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir($directory);
|
|
|
|
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get Partition Free Space.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* disk_free_space — Returns available space on filesystem or disk partition
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getPartitionFreeSpace():float
|
|
|
|
{
|
|
|
|
return disk_free_space($this->getRoot());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get Partition Total Space.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* 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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Human readable data size format from bytes input.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* As published on https://gist.github.com/liunian/9338301 (first comment)
|
|
|
|
*
|
|
|
|
* @param int $bytes
|
|
|
|
* @param int $decimals
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function human($bytes, $decimals = 2)
|
|
|
|
{
|
2019-09-07 05:04:26 +12:00
|
|
|
$units = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
|
|
|
|
$step = 1024;
|
|
|
|
$i = 0;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
while (($bytes / $step) > 0.9) {
|
|
|
|
$bytes = $bytes / $step;
|
2019-09-07 05:04:26 +12:00
|
|
|
++$i;
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
return round($bytes, $decimals).$units[$i];
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
2019-09-07 05:04:26 +12:00
|
|
|
}
|