// import { i18n } from "@/lang/index" // 媒体名称 export const mediaTypes = { // image: i18n.t("common.photo"), // video: i18n.t("common.video"), // audio: i18n.t("common.voice"), } // 媒体扩展类型 export const mediaMimes = { image: ['jpg', 'png', 'jpeg', 'bmp', 'gif'], audio: ['mp3', 'aac', 'ogg', 'wav' /* , "m4a" */], video: ['mp4', 'mov', 'quicktime', 'webm' /* "rmvb", "wmv" */] //ios:mov } // 媒体大小显示(MB) export const mediaMaxSize = { image: 10, video: 20, audio: 5 } /** * 获取媒体扩展类型 * @param {Stirng} filename 文件名称 */ export const getMime = filename => { if (!filename || filename.indexOf('.') === -1) { return '' } return filename .split('.') .pop() .toLowerCase() } /** * 在路径中获取文件名 * @param {*} path */ export const getFilename = path => { const segment = (path || '').split('/') return segment[segment.length - 1] } /** * 检测媒体文件是否超过预设限制 * @param {String} type 媒体类型 * @param {Number} size 文件大小 */ export const checkSizeLimit = (type, size) => { size = size / 1024 / 1024 return size <= mediaMaxSize[type] } export const checkSizeLimitFree = (size, limit) => { size = size / 1024 / 1024 return size <= limit } /** * 检测媒体类型 * @param {String} type 媒体类型 * @param {String} filename 文件名称 */ export const checkMediaMime = (type, filename) => { const mime = getMime(filename) const find = mediaMimes[type] if (!find) { return false } return find.indexOf(mime) !== -1 } export const checkMediaMimeByAccept = (accept, filename) => { let mime = getMime(filename) let type = accept if (type && type.indexOf('jpg') == -1 && type.indexOf('jpeg') != -1) { type += ',image/jpg' } return (type || '').indexOf(mime) != -1 } export const base64ToBlob = base64 => { let arr = base64.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } return new Blob([u8arr], { type: mime }) } export const base64ToDataURL = base64 => { return window.URL.createObjectURL(base64ToBlob(base64)) } export const blobToDataURL = blob => { return window.URL.createObjectURL(blob) } export const blobToBase64 = function(blob) { return new Promise(resolve => { var reader = new FileReader() reader.onload = function() { resolve(reader.result) } reader.readAsDataURL(blob) }) } export function convertBlob2File(blob, name) { return new File([blob], name, { type: blob.type }) }