1
0
Fork 0
mirror of synced 2024-06-03 03:14:50 +12:00
appwrite/src/Appwrite/Storage/Storage.php

113 lines
2.2 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
namespace Appwrite\Storage;
2019-05-09 18:54:39 +12:00
use Exception;
class Storage
{
/**
* Devices.
2019-05-09 18:54:39 +12:00
*
* List of all available storage devices
*
* @var array
*/
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-05-09 18:54:39 +12:00
* @throws Exception
*
* @return void
2019-05-09 18:54:39 +12:00
*/
public static function setDevice($name, Device $device): void
2019-05-09 18:54:39 +12:00
{
self::$devices[$name] = $device;
}
/**
* Get Device.
2019-05-09 18:54:39 +12:00
*
* Get device by name
*
* @param string $name
*
2019-05-09 18:54:39 +12:00
* @return Device
*
2019-05-09 18:54:39 +12:00
* @throws Exception
*/
public static function getDevice($name)
2019-05-09 18:54:39 +12:00
{
if (!\array_key_exists($name, self::$devices)) {
throw new Exception('The device "'.$name.'" is not listed');
2019-05-09 18:54:39 +12:00
}
return self::$devices[$name];
}
/**
* Exists.
2019-05-09 18:54:39 +12:00
*
* Checks if given storage name is registered or not
*
* @param string $name
*
2019-05-09 18:54:39 +12:00
* @return bool
*/
public static function exists($name)
2019-05-09 18:54:39 +12:00
{
return (bool) \array_key_exists($name, self::$devices);
2019-05-09 18:54:39 +12: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
*
* @param int $bytes
* @param int $decimals
2020-07-20 03:17:57 +12:00
* @param string $system
*
* @return string
*/
2020-07-20 03:17:57 +12:00
public static function human(int $bytes, $decimals = 2, $system = 'metric')
{
2020-07-20 03:17:57 +12:00
$mod = ($system === 'binary') ? 1024 : 1000;
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',
),
);
$factor = (int)floor((strlen((string)$bytes) - 1) / 3);
2020-07-20 03:17:57 +12:00
return sprintf("%.{$decimals}f%s", $bytes / pow($mod, $factor), $units[$system][$factor]);
}
2020-07-20 03:17:57 +12:00
}