diff --git a/src/components/media_upload/media_upload.js b/src/components/media_upload/media_upload.js index 91ae3627..1927a194 100644 --- a/src/components/media_upload/media_upload.js +++ b/src/components/media_upload/media_upload.js @@ -1,5 +1,7 @@ /* eslint-env browser */ import statusPosterService from '../../services/status_poster/status_poster.service.js' +import fileSizeFormatService from '../../services/file_size_format/file_size_format.js' + const mediaUpload = { mounted () { @@ -22,7 +24,7 @@ const mediaUpload = { const self = this const store = this.$store if (file.size > store.state.instance.uploadlimit) { - self.$emit('upload-failed', 'upload_error_file_too_big', {filesize: file.size, allowedsize: store.state.instance.uploadlimit}) + self.$emit('upload-failed', 'upload_error_file_too_big', {filesize: fileSizeFormatService.fileSizeFormat(file.size, self.$t), allowedsize: fileSizeFormatService.fileSizeFormat(store.state.instance.uploadlimit, self.$t)}) return } const formData = new FormData() diff --git a/src/i18n/en.json b/src/i18n/en.json index 17c28c61..885974e4 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -229,5 +229,12 @@ "reply": "Reply", "favorite": "Favorite", "user_settings": "User Settings" + }, + "file_size_units": { + "B": "B", + "KB": "KB", + "MB": "MB", + "GB": "GB", + "TB": "TB" } } diff --git a/src/services/file_size_format/.file_size_format.js.swp b/src/services/file_size_format/.file_size_format.js.swp new file mode 100644 index 00000000..ec2e601a Binary files /dev/null and b/src/services/file_size_format/.file_size_format.js.swp differ diff --git a/src/services/file_size_format/file_size_format.js b/src/services/file_size_format/file_size_format.js new file mode 100644 index 00000000..5d22b473 --- /dev/null +++ b/src/services/file_size_format/file_size_format.js @@ -0,0 +1,17 @@ +const fileSizeFormat = (num, t) => { + var exponent + var unit + var units = [t('file_size_units.B'), t('file_size_units.KB'), t('file_size_units.MB'), t('file_size_units.GB'), t('file_size_units.TB')] + if (num < 1) { + return num + ' ' + units[0] + } + + exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1) + num = (num / Math.pow(1000, exponent)).toFixed(2) * 1 + unit = units[exponent] + return num + ' ' + unit +} +const fileSizeFormatService = { + fileSizeFormat +} +export default fileSizeFormatService