-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathStorage.php
More file actions
124 lines (107 loc) · 2.51 KB
/
Storage.php
File metadata and controls
124 lines (107 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Utopia\Storage;
use Exception;
class Storage
{
/**
* Supported devices
*/
public const DEVICE_LOCAL = 'local';
public const DEVICE_S3 = 's3';
public const DEVICE_DO_SPACES = 'dospaces';
public const DEVICE_WASABI = 'wasabi';
public const DEVICE_BACKBLAZE = 'backblaze';
public const DEVICE_LINODE = 'linode';
/**
* Devices.
*
* List of all available storage devices
*
* @var array
*/
public static $devices = [];
/**
* Set Device.
*
* Add device by name
*
* @param string $name
* @param Device $device
* @return void
*
* @throws Exception
*/
public static function setDevice($name, Device $device): void
{
self::$devices[$name] = $device;
}
/**
* Get Device.
*
* Get device by name
*
* @param string $name
* @return Device
*
* @throws Exception
*/
public static function getDevice($name)
{
if (! \array_key_exists($name, self::$devices)) {
throw new Exception('The device "'.$name.'" is not listed');
}
return self::$devices[$name];
}
/**
* Exists.
*
* Checks if given storage name is registered or not
*
* @param string $name
* @return bool
*/
public static function exists($name)
{
return (bool) \array_key_exists($name, self::$devices);
}
/**
* Human readable data size format from bytes input.
*
* Based on: https://stackoverflow.com/a/38659168/2299554
*
* @param int $bytes
* @param int $decimals
* @param string $system
* @return string
*/
public static function human(int $bytes, $decimals = 2, $system = 'metric')
{
$mod = ($system === 'binary') ? 1024 : 1000;
$units = [
'binary' => [
'B',
'KiB',
'MiB',
'GiB',
'TiB',
'PiB',
'EiB',
'ZiB',
'YiB',
],
'metric' => [
'B',
'kB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB',
],
];
$factor = (int) floor((strlen((string) $bytes) - 1) / 3);
return sprintf("%.{$decimals}f%s", $bytes / pow($mod, $factor), $units[$system][$factor]);
}
}