utils.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.toArray = toArray;
  4. exports.readFile = readFile;
  5. exports.isOversize = isOversize;
  6. exports.isImageUrl = isImageUrl;
  7. exports.isImageFile = isImageFile;
  8. var _utils = require("../utils");
  9. function toArray(item) {
  10. if (Array.isArray(item)) {
  11. return item;
  12. }
  13. return [item];
  14. }
  15. function readFile(file, resultType) {
  16. return new Promise(function (resolve) {
  17. if (resultType === 'file') {
  18. resolve(null);
  19. return;
  20. }
  21. var reader = new FileReader();
  22. reader.onload = function (event) {
  23. resolve(event.target.result);
  24. };
  25. if (resultType === 'dataUrl') {
  26. reader.readAsDataURL(file);
  27. } else if (resultType === 'text') {
  28. reader.readAsText(file);
  29. }
  30. });
  31. }
  32. function isOversize(files, maxSize) {
  33. return toArray(files).some(function (file) {
  34. if (file) {
  35. if ((0, _utils.isFunction)(maxSize)) {
  36. return maxSize(file);
  37. }
  38. return file.size > maxSize;
  39. }
  40. return false;
  41. });
  42. }
  43. var IMAGE_REGEXP = /\.(jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg)/i;
  44. function isImageUrl(url) {
  45. return IMAGE_REGEXP.test(url);
  46. }
  47. function isImageFile(item) {
  48. // some special urls cannot be recognized
  49. // user can add `isImage` flag to mark it as an image url
  50. if (item.isImage) {
  51. return true;
  52. }
  53. if (item.file && item.file.type) {
  54. return item.file.type.indexOf('image') === 0;
  55. }
  56. if (item.url) {
  57. return isImageUrl(item.url);
  58. }
  59. if (item.content) {
  60. return item.content.indexOf('data:image') === 0;
  61. }
  62. return false;
  63. }