2019-10-19 16:38:07 +02:00
|
|
|
import React from 'react';
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
bytes: number;
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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) {
|
2019-10-19 16:38:07 +02:00
|
|
|
return '0 B';
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
const units = ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
|
2019-09-05 08:09:49 +02:00
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(1000));
|
2018-09-27 16:32:37 +02:00
|
|
|
|
2019-09-05 08:09:49 +02:00
|
|
|
const size = i === 0 ? bytes : (bytes / 1000 ** 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;
|