Add file size formating

This commit is contained in:
Rinpatch 2018-12-10 09:50:04 +03:00
parent 4fde987e34
commit c69a8dc197
4 changed files with 27 additions and 1 deletions

View file

@ -1,5 +1,7 @@
/* eslint-env browser */ /* eslint-env browser */
import statusPosterService from '../../services/status_poster/status_poster.service.js' import statusPosterService from '../../services/status_poster/status_poster.service.js'
import fileSizeFormatService from '../../services/file_size_format/file_size_format.js'
const mediaUpload = { const mediaUpload = {
mounted () { mounted () {
@ -22,7 +24,7 @@ const mediaUpload = {
const self = this const self = this
const store = this.$store const store = this.$store
if (file.size > store.state.instance.uploadlimit) { 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 return
} }
const formData = new FormData() const formData = new FormData()

View file

@ -229,5 +229,12 @@
"reply": "Reply", "reply": "Reply",
"favorite": "Favorite", "favorite": "Favorite",
"user_settings": "User Settings" "user_settings": "User Settings"
},
"file_size_units": {
"B": "B",
"KB": "KB",
"MB": "MB",
"GB": "GB",
"TB": "TB"
} }
} }

Binary file not shown.

View file

@ -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