utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. export function file2Obj(file) {
  3. return _extends(_extends({}, file), {
  4. lastModified: file.lastModified,
  5. lastModifiedDate: file.lastModifiedDate,
  6. name: file.name,
  7. size: file.size,
  8. type: file.type,
  9. uid: file.uid,
  10. percent: 0,
  11. originFileObj: file
  12. });
  13. }
  14. /** Upload fileList. Replace file if exist or just push into it. */
  15. export function updateFileList(file, fileList) {
  16. const nextFileList = [...fileList];
  17. const fileIndex = nextFileList.findIndex(_ref => {
  18. let {
  19. uid
  20. } = _ref;
  21. return uid === file.uid;
  22. });
  23. if (fileIndex === -1) {
  24. nextFileList.push(file);
  25. } else {
  26. nextFileList[fileIndex] = file;
  27. }
  28. return nextFileList;
  29. }
  30. export function getFileItem(file, fileList) {
  31. const matchKey = file.uid !== undefined ? 'uid' : 'name';
  32. return fileList.filter(item => item[matchKey] === file[matchKey])[0];
  33. }
  34. export function removeFileItem(file, fileList) {
  35. const matchKey = file.uid !== undefined ? 'uid' : 'name';
  36. const removed = fileList.filter(item => item[matchKey] !== file[matchKey]);
  37. if (removed.length === fileList.length) {
  38. return null;
  39. }
  40. return removed;
  41. }
  42. // ==================== Default Image Preview ====================
  43. const extname = function () {
  44. let url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  45. const temp = url.split('/');
  46. const filename = temp[temp.length - 1];
  47. const filenameWithoutSuffix = filename.split(/#|\?/)[0];
  48. return (/\.[^./\\]*$/.exec(filenameWithoutSuffix) || [''])[0];
  49. };
  50. const isImageFileType = type => type.indexOf('image/') === 0;
  51. export const isImageUrl = file => {
  52. if (file.type && !file.thumbUrl) {
  53. return isImageFileType(file.type);
  54. }
  55. const url = file.thumbUrl || file.url || '';
  56. const extension = extname(url);
  57. if (/^data:image\//.test(url) || /(webp|svg|png|gif|jpg|jpeg|jfif|bmp|dpg|ico)$/i.test(extension)) {
  58. return true;
  59. }
  60. if (/^data:/.test(url)) {
  61. // other file types of base64
  62. return false;
  63. }
  64. if (extension) {
  65. // other file types which have extension
  66. return false;
  67. }
  68. return true;
  69. };
  70. const MEASURE_SIZE = 200;
  71. export function previewImage(file) {
  72. return new Promise(resolve => {
  73. if (!file.type || !isImageFileType(file.type)) {
  74. resolve('');
  75. return;
  76. }
  77. const canvas = document.createElement('canvas');
  78. canvas.width = MEASURE_SIZE;
  79. canvas.height = MEASURE_SIZE;
  80. canvas.style.cssText = `position: fixed; left: 0; top: 0; width: ${MEASURE_SIZE}px; height: ${MEASURE_SIZE}px; z-index: 9999; display: none;`;
  81. document.body.appendChild(canvas);
  82. const ctx = canvas.getContext('2d');
  83. const img = new Image();
  84. img.onload = () => {
  85. const {
  86. width,
  87. height
  88. } = img;
  89. let drawWidth = MEASURE_SIZE;
  90. let drawHeight = MEASURE_SIZE;
  91. let offsetX = 0;
  92. let offsetY = 0;
  93. if (width > height) {
  94. drawHeight = height * (MEASURE_SIZE / width);
  95. offsetY = -(drawHeight - drawWidth) / 2;
  96. } else {
  97. drawWidth = width * (MEASURE_SIZE / height);
  98. offsetX = -(drawWidth - drawHeight) / 2;
  99. }
  100. ctx.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
  101. const dataURL = canvas.toDataURL();
  102. document.body.removeChild(canvas);
  103. resolve(dataURL);
  104. };
  105. img.crossOrigin = 'anonymous';
  106. if (file.type.startsWith('image/svg+xml')) {
  107. const reader = new FileReader();
  108. reader.addEventListener('load', () => {
  109. if (reader.result) img.src = reader.result;
  110. });
  111. reader.readAsDataURL(file);
  112. } else {
  113. img.src = window.URL.createObjectURL(file);
  114. }
  115. });
  116. }