mattermost/e2e-tests/cypress/tests/utils/file.js
2023-03-28 18:10:00 +02:00

27 lines
1.1 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// Converts a file size in bytes into a human-readable string of the form '123MB'.
export function fileSizeToString(bytes) {
// it's unlikely that we'll have files bigger than this
if (bytes > 1024 ** 4) {
// check if file is smaller than 10 to display fractions
if (bytes < (1024 ** 4) * 10) {
return (Math.round((bytes / (1024 ** 4)) * 10) / 10) + 'TB';
}
return Math.round(bytes / (1024 ** 4)) + 'TB';
} else if (bytes > 1024 ** 3) {
if (bytes < (1024 ** 3) * 10) {
return (Math.round((bytes / (1024 ** 3)) * 10) / 10) + 'GB';
}
return Math.round(bytes / (1024 ** 3)) + 'GB';
} else if (bytes > 1024 ** 2) {
if (bytes < (1024 ** 2) * 10) {
return (Math.round((bytes / (1024 ** 2)) * 10) / 10) + 'MB';
}
return Math.round(bytes / (1024 ** 2)) + 'MB';
} else if (bytes > 1024) {
return Math.round(bytes / 1024) + 'KB';
}
return bytes + 'B';
}