utils.js 1.4 KB

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