文件相关
/**
* @description 获取文件后缀名
* @param {String} fileName
*/
export function getFileExt(fileName) {
return fileName.substring(fileName.lastIndexOf('.') + 1)
}
/**
* @description 根据文件后缀名 获取文件icon
* @param {string} ext 文件后缀
* @return {string} icon名
*/
export function getFileIcon(ext) {
ext = ext.toLocaleLowerCase()
if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'pic'].includes(ext)) {
return 'img'
} else if (['zip', 'rar', '7z'].includes(ext)) {
return 'zip'
} else if (['avi', 'mp4', 'rmvb', 'flv', 'mov', 'm2v', 'mkv'].includes(ext)) {
return 'video'
} else if (['mp3', 'wav', 'wmv', 'wma'].includes(ext)) {
return 'audio'
} else if (['xls', 'xlsx'].includes(ext)) {
return 'xls'
} else if (['doc', 'docx'].includes(ext)) {
return 'word'
} else if (ext === 'pdf') {
return 'pdf'
} else if (['ppt', 'pptx'].includes(ext)) {
return 'ppt'
} else if (ext === 'txt') {
return 'txt'
} else {
return 'none'
}
}
/**
* @description 格式化文件大小
* @param {Number} bytes
* @param {Number} decimals
* @return {*}
*/
export function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 B'
const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
}