file.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // import { i18n } from "@/lang/index"
  2. // 媒体名称
  3. export const mediaTypes = {
  4. // image: i18n.t("common.photo"),
  5. // video: i18n.t("common.video"),
  6. // audio: i18n.t("common.voice"),
  7. }
  8. // 媒体扩展类型
  9. export const mediaMimes = {
  10. image: ['jpg', 'png', 'jpeg', 'bmp', 'gif'],
  11. audio: ['mp3', 'aac', 'ogg', 'wav' /* , "m4a" */],
  12. video: ['mp4', 'mov', 'quicktime', 'webm' /* "rmvb", "wmv" */] //ios:mov
  13. }
  14. // 媒体大小显示(MB)
  15. export const mediaMaxSize = {
  16. image: 10,
  17. video: 20,
  18. audio: 5
  19. }
  20. /**
  21. * 获取媒体扩展类型
  22. * @param {Stirng} filename 文件名称
  23. */
  24. export const getMime = filename => {
  25. if (!filename || filename.indexOf('.') === -1) {
  26. return ''
  27. }
  28. return filename
  29. .split('.')
  30. .pop()
  31. .toLowerCase()
  32. }
  33. /**
  34. * 在路径中获取文件名
  35. * @param {*} path
  36. */
  37. export const getFilename = path => {
  38. const segment = (path || '').split('/')
  39. return segment[segment.length - 1]
  40. }
  41. /**
  42. * 检测媒体文件是否超过预设限制
  43. * @param {String} type 媒体类型
  44. * @param {Number} size 文件大小
  45. */
  46. export const checkSizeLimit = (type, size) => {
  47. size = size / 1024 / 1024
  48. return size <= mediaMaxSize[type]
  49. }
  50. export const checkSizeLimitFree = (size, limit) => {
  51. size = size / 1024 / 1024
  52. return size <= limit
  53. }
  54. /**
  55. * 检测媒体类型
  56. * @param {String} type 媒体类型
  57. * @param {String} filename 文件名称
  58. */
  59. export const checkMediaMime = (type, filename) => {
  60. const mime = getMime(filename)
  61. const find = mediaMimes[type]
  62. if (!find) {
  63. return false
  64. }
  65. return find.indexOf(mime) !== -1
  66. }
  67. export const checkMediaMimeByAccept = (accept, filename) => {
  68. let mime = getMime(filename)
  69. let type = accept
  70. if (type && type.indexOf('jpg') == -1 && type.indexOf('jpeg') != -1) {
  71. type += ',image/jpg'
  72. }
  73. return (type || '').indexOf(mime) != -1
  74. }
  75. export const base64ToBlob = base64 => {
  76. let arr = base64.split(','),
  77. mime = arr[0].match(/:(.*?);/)[1],
  78. bstr = atob(arr[1]),
  79. n = bstr.length,
  80. u8arr = new Uint8Array(n)
  81. while (n--) {
  82. u8arr[n] = bstr.charCodeAt(n)
  83. }
  84. return new Blob([u8arr], { type: mime })
  85. }
  86. export const base64ToDataURL = base64 => {
  87. return window.URL.createObjectURL(base64ToBlob(base64))
  88. }
  89. export const blobToDataURL = blob => {
  90. return window.URL.createObjectURL(blob)
  91. }
  92. export const blobToBase64 = function(blob) {
  93. return new Promise(resolve => {
  94. var reader = new FileReader()
  95. reader.onload = function() {
  96. resolve(reader.result)
  97. }
  98. reader.readAsDataURL(blob)
  99. })
  100. }
  101. export function convertBlob2File(blob, name) {
  102. return new File([blob], name, {
  103. type: blob.type
  104. })
  105. }