diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-01-31 18:50:59 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-02-01 08:25:01 +0100 |
commit | 0d43ef06f5dbd05edfe61e9c78461e15312834ea (patch) | |
tree | e11ee21cdca73769064e4a19bc65a48711cecaaa /core/src/Util | |
parent | 9de02d326799285896e163cb7b6b1b47a1a5bbb9 (diff) | |
download | nextcloud-server-0d43ef06f5dbd05edfe61e9c78461e15312834ea.tar.gz nextcloud-server-0d43ef06f5dbd05edfe61e9c78461e15312834ea.zip |
Add OC.Util to the server bundle
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'core/src/Util')
-rw-r--r-- | core/src/Util/human-file-size.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/core/src/Util/human-file-size.js b/core/src/Util/human-file-size.js new file mode 100644 index 00000000000..e671a86d053 --- /dev/null +++ b/core/src/Util/human-file-size.js @@ -0,0 +1,51 @@ +/* + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * Returns a human readable file size + * @param {number} size Size in bytes + * @param {boolean} skipSmallSizes return '< 1 kB' for small files + * @return {string} + */ +export default function humanFileSize (size, skipSmallSizes) { + var humanList = ['B', 'KB', 'MB', 'GB', 'TB']; + // Calculate Log with base 1024: size = 1024 ** order + var order = size > 0 ? Math.floor(Math.log(size) / Math.log(1024)) : 0; + // Stay in range of the byte sizes that are defined + order = Math.min(humanList.length - 1, order); + var readableFormat = humanList[order]; + var relativeSize = (size / Math.pow(1024, order)).toFixed(1); + if (skipSmallSizes === true && order === 0) { + if (relativeSize !== "0.0") { + return '< 1 KB'; + } else { + return '0 KB'; + } + } + if (order < 2) { + relativeSize = parseFloat(relativeSize).toFixed(0); + } else if (relativeSize.substr(relativeSize.length - 2, 2) === '.0') { + relativeSize = relativeSize.substr(0, relativeSize.length - 2); + } else { + relativeSize = parseFloat(relativeSize).toLocaleString(OC.getCanonicalLocale()); + } + return relativeSize + ' ' + readableFormat; +} |