feat(utils): 增加了转换存储空间大小的函数

byte -> b/kb/mb/gb/tb/pb

Signed-off-by: 李东云 <dongyun.li@luxcreo.ai>
This commit is contained in:
李东云
2022-07-05 15:04:48 +08:00
parent 848b87a54f
commit 46d7f357bd

View File

@@ -8,6 +8,8 @@ use Generator;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\ResponseInterface;
/**
@@ -93,7 +95,7 @@ class UtilsService
$link_list = explode(',', $link);
$link_list = array_map('trim', $link_list);
}
array_push($link_list, "<$urlReference>; " . http_build_query($params, '', '; '));
$link_list[] = "<$urlReference>; " . http_build_query($params, '', '; ');
return $response->withHeader(
'Link',
@@ -216,8 +218,35 @@ class UtilsService
$list = $list[$childrenName];
$end_time = microtime(true);
$this->logger->debug("快速无极分类循环{$current_times}次,数组元素数量$max_times");
$cost_time = ($end_time - $start_time) * 1000;
$this->logger->debug("快速无极分类用时{$cost_time}ms");
}
/**
* 将内存/硬盘大小转化为带单位大小
*
* @param float|int|string $size 空间大小单位Byte
* @param bool $unitToUpper 单位转换为大写(默认 false
* @param int $precision 小数点后保留的位数(默认 2
*
* @return array
*/
#[
ArrayShape(['size' => 'float', 'unit' => 'string']),
Pure
]
public function convertMemorySize(
float|int|string $size,
bool $unitToUpper = false,
int $precision = 2
): array {
$unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
$i = floor(log($size, 1024));
return [
'size' => round($size / pow(1024, $i), $precision),
'unit' => $unitToUpper ? strtoupper($unit[$i]) : $unit[$i],
];
}
}