2018-09-27 16:32:37 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
bytes: number
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class FileSize extends React.Component<Props> {
|
2018-10-17 15:34:22 +02:00
|
|
|
static format(bytes: number) {
|
2018-09-27 16:32:37 +02:00
|
|
|
if (!bytes) {
|
2018-10-17 15:34:22 +02:00
|
|
|
return "0 B";
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const units = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
|
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
|
|
|
|
2018-10-19 13:01:06 +02:00
|
|
|
const size = i === 0 ? bytes : (bytes / 1024 ** i).toFixed(2);
|
2018-09-27 16:32:37 +02:00
|
|
|
return `${size} ${units[i]}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const formattedBytes = FileSize.format(this.props.bytes);
|
|
|
|
|
return <span>{formattedBytes}</span>;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FileSize;
|