file.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // 媒体名称
  2. export const mediaTypes = {
  3. "image": '图片',
  4. "video": "视频",
  5. "audio": "音频"
  6. }
  7. // 媒体扩展类型
  8. export const mediaMimes = {
  9. "image": ["jpg", "png", "jpeg", "bmp", "gif"],
  10. "audio": ["mp3", "aac", "ogg", "wav" /* , "m4a" */],
  11. "video": ["mp4", "mov", "quicktime" /* ,"webm", "rmvb", "wmv" */] //ios:mov
  12. }
  13. // 媒体大小显示(MB)
  14. export const mediaMaxSize = {
  15. "image": 10,
  16. "video": 20,
  17. "audio": 5
  18. }
  19. /**
  20. * 获取媒体扩展类型
  21. * @param {Stirng} filename 文件名称
  22. */
  23. export const getMime = filename => {
  24. if (!filename || filename.indexOf('.') === -1) {
  25. return ''
  26. }
  27. return filename.split('.').pop().toLowerCase()
  28. }
  29. /**
  30. * 在路径中获取文件名
  31. * @param {*} path
  32. */
  33. export const getFilename = path => {
  34. const segment = (path || '').split('/')
  35. return segment[segment.length - 1]
  36. }
  37. /**
  38. * 检测媒体文件是否超过预设限制
  39. * @param {String} type 媒体类型
  40. * @param {Number} size 文件大小
  41. */
  42. export const checkSizeLimit = (type, size) => {
  43. size = size / 1024 / 1024
  44. return size <= mediaMaxSize[type]
  45. }
  46. export const checkSizeLimitFree = (size, limit) => {
  47. size = size / 1024 / 1024
  48. return size <= limit
  49. }
  50. /**
  51. * 检测媒体类型
  52. * @param {String} type 媒体类型
  53. * @param {String} filename 文件名称
  54. */
  55. export const checkMediaMime = (type, filename) => {
  56. const mime = getMime(filename)
  57. const find = mediaMimes[type]
  58. if (!find) {
  59. return false
  60. }
  61. return find.indexOf(mime) !== -1
  62. }
  63. export const base64ToBlob = base64 => {
  64. let arr = base64.split(','),
  65. mime = arr[0].match(/:(.*?);/)[1],
  66. bstr = atob(arr[1]),
  67. n = bstr.length,
  68. u8arr = new Uint8Array(n);
  69. while (n--) {
  70. u8arr[n] = bstr.charCodeAt(n);
  71. }
  72. return new Blob([u8arr], { type: mime });
  73. }
  74. export const base64ToDataURL = base64 => {
  75. return window.URL.createObjectURL(base64ToBlob(base64));
  76. }
  77. export const blobToBase64 = function (blob) {
  78. return new Promise(resolve => {
  79. var reader = new FileReader();
  80. reader.onload = function () {
  81. resolve(reader.result);
  82. };
  83. reader.readAsDataURL(blob);
  84. })
  85. };
  86. /**
  87. * 获取图片文件尺寸
  88. * @param {*} file
  89. */
  90. export const getImgWH = (file) => {
  91. return new Promise((resolve) => {
  92. var reader = new FileReader()
  93. //读取图片文件
  94. reader.readAsDataURL(file)
  95. reader.onload = function (e) {
  96. //初始化JavaScript图片对象
  97. var image = new Image()
  98. //FileReader获得Base64字符串
  99. image.src = e.target.result
  100. image.onload = function () {
  101. //获得图片高宽
  102. var height = this.height
  103. var width = this.width
  104. resolve({
  105. height,
  106. width
  107. })
  108. }
  109. }
  110. })
  111. }
  112. /**
  113. * 转化字节单位
  114. * @param {*} file
  115. */
  116. export const changeByteUnit = (x) => {
  117. const units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  118. let l = 0, n = parseInt(x, 10) || 0;
  119. while (n >= 1024 && ++l) {
  120. n = n / 1024;
  121. }
  122. return (n.toFixed(n < 10 && l > 0 ? 2 : 0) + units[l]);
  123. }