2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
namespace Appwrite\Storage;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
class Storage
|
|
|
|
{
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Devices.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* List of all available storage devices
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public static $devices = array();
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
/**
|
2020-07-03 04:11:07 +12:00
|
|
|
* Set Device.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Add device by name
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param Device $device
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @throws Exception
|
2020-10-27 13:08:29 +13:00
|
|
|
*
|
|
|
|
* @return void
|
2019-05-09 18:54:39 +12:00
|
|
|
*/
|
2020-10-27 13:08:29 +13:00
|
|
|
public static function setDevice($name, Device $device): void
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
|
|
|
self::$devices[$name] = $device;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get Device.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Get device by name
|
|
|
|
*
|
|
|
|
* @param string $name
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return Device
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public static function getDevice($name)
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
if (!\array_key_exists($name, self::$devices)) {
|
2019-09-07 05:04:26 +12:00
|
|
|
throw new Exception('The device "'.$name.'" is not listed');
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$devices[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Exists.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Checks if given storage name is registered or not
|
|
|
|
*
|
|
|
|
* @param string $name
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public static function exists($name)
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
return (bool) \array_key_exists($name, self::$devices);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
2020-02-14 07:55:12 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Human readable data size format from bytes input.
|
|
|
|
*
|
2020-07-20 03:17:57 +12:00
|
|
|
* Based on: https://stackoverflow.com/a/38659168/2299554
|
2020-02-14 07:55:12 +13:00
|
|
|
*
|
|
|
|
* @param int $bytes
|
|
|
|
* @param int $decimals
|
2020-07-20 03:17:57 +12:00
|
|
|
* @param string $system
|
2020-02-14 07:55:12 +13:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-07-20 03:17:57 +12:00
|
|
|
public static function human(int $bytes, $decimals = 2, $system = 'metric')
|
2020-02-14 07:55:12 +13:00
|
|
|
{
|
2020-07-20 03:17:57 +12:00
|
|
|
$mod = ($system === 'binary') ? 1024 : 1000;
|
2020-02-14 07:55:12 +13:00
|
|
|
|
2020-07-20 03:17:57 +12:00
|
|
|
$units = array(
|
|
|
|
'binary' => array(
|
|
|
|
'B',
|
|
|
|
'KiB',
|
|
|
|
'MiB',
|
|
|
|
'GiB',
|
|
|
|
'TiB',
|
|
|
|
'PiB',
|
|
|
|
'EiB',
|
|
|
|
'ZiB',
|
|
|
|
'YiB',
|
|
|
|
),
|
|
|
|
'metric' => array(
|
|
|
|
'B',
|
|
|
|
'kB',
|
|
|
|
'MB',
|
|
|
|
'GB',
|
|
|
|
'TB',
|
|
|
|
'PB',
|
|
|
|
'EB',
|
|
|
|
'ZB',
|
|
|
|
'YB',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2020-10-27 13:08:29 +13:00
|
|
|
$factor = (int)floor((strlen((string)$bytes) - 1) / 3);
|
2020-02-14 07:55:12 +13:00
|
|
|
|
2020-07-20 03:17:57 +12:00
|
|
|
return sprintf("%.{$decimals}f%s", $bytes / pow($mod, $factor), $units[$system][$factor]);
|
2020-02-14 07:55:12 +13:00
|
|
|
}
|
2020-07-20 03:17:57 +12:00
|
|
|
}
|