filepond-plugin-file-validate-size.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*!
  2. * FilePondPluginFileValidateSize 2.2.8
  3. * Licensed under MIT, https://opensource.org/licenses/MIT/
  4. * Please visit https://pqina.nl/filepond/ for details.
  5. */
  6. /* eslint-disable */
  7. (function(global, factory) {
  8. typeof exports === 'object' && typeof module !== 'undefined'
  9. ? (module.exports = factory())
  10. : typeof define === 'function' && define.amd
  11. ? define(factory)
  12. : ((global = global || self), (global.FilePondPluginFileValidateSize = factory()));
  13. })(this, function() {
  14. 'use strict';
  15. var plugin = function plugin(_ref) {
  16. var addFilter = _ref.addFilter,
  17. utils = _ref.utils;
  18. // get quick reference to Type utils
  19. var Type = utils.Type,
  20. replaceInString = utils.replaceInString,
  21. toNaturalFileSize = utils.toNaturalFileSize;
  22. // filtering if an item is allowed in hopper
  23. addFilter('ALLOW_HOPPER_ITEM', function(file, _ref2) {
  24. var query = _ref2.query;
  25. if (!query('GET_ALLOW_FILE_SIZE_VALIDATION')) {
  26. return true;
  27. }
  28. var sizeMax = query('GET_MAX_FILE_SIZE');
  29. if (sizeMax !== null && file.size > sizeMax) {
  30. return false;
  31. }
  32. var sizeMin = query('GET_MIN_FILE_SIZE');
  33. if (sizeMin !== null && file.size < sizeMin) {
  34. return false;
  35. }
  36. return true;
  37. });
  38. // called for each file that is loaded
  39. // right before it is set to the item state
  40. // should return a promise
  41. addFilter('LOAD_FILE', function(file, _ref3) {
  42. var query = _ref3.query;
  43. return new Promise(function(resolve, reject) {
  44. // if not allowed, all fine, exit
  45. if (!query('GET_ALLOW_FILE_SIZE_VALIDATION')) {
  46. return resolve(file);
  47. }
  48. // check if file should be filtered
  49. var fileFilter = query('GET_FILE_VALIDATE_SIZE_FILTER');
  50. if (fileFilter && !fileFilter(file)) {
  51. return resolve(file);
  52. }
  53. // reject or resolve based on file size
  54. var sizeMax = query('GET_MAX_FILE_SIZE');
  55. if (sizeMax !== null && file.size > sizeMax) {
  56. reject({
  57. status: {
  58. main: query('GET_LABEL_MAX_FILE_SIZE_EXCEEDED'),
  59. sub: replaceInString(query('GET_LABEL_MAX_FILE_SIZE'), {
  60. filesize: toNaturalFileSize(
  61. sizeMax,
  62. '.',
  63. query('GET_FILE_SIZE_BASE'),
  64. query('GET_FILE_SIZE_LABELS', query)
  65. ),
  66. }),
  67. },
  68. });
  69. return;
  70. }
  71. // reject or resolve based on file size
  72. var sizeMin = query('GET_MIN_FILE_SIZE');
  73. if (sizeMin !== null && file.size < sizeMin) {
  74. reject({
  75. status: {
  76. main: query('GET_LABEL_MIN_FILE_SIZE_EXCEEDED'),
  77. sub: replaceInString(query('GET_LABEL_MIN_FILE_SIZE'), {
  78. filesize: toNaturalFileSize(
  79. sizeMin,
  80. '.',
  81. query('GET_FILE_SIZE_BASE'),
  82. query('GET_FILE_SIZE_LABELS', query)
  83. ),
  84. }),
  85. },
  86. });
  87. return;
  88. }
  89. // returns the current option value
  90. var totalSizeMax = query('GET_MAX_TOTAL_FILE_SIZE');
  91. if (totalSizeMax !== null) {
  92. // get the current total file size
  93. var currentTotalSize = query('GET_ACTIVE_ITEMS').reduce(function(total, item) {
  94. return total + item.fileSize;
  95. }, 0);
  96. // get the size of the new file
  97. if (currentTotalSize > totalSizeMax) {
  98. reject({
  99. status: {
  100. main: query('GET_LABEL_MAX_TOTAL_FILE_SIZE_EXCEEDED'),
  101. sub: replaceInString(query('GET_LABEL_MAX_TOTAL_FILE_SIZE'), {
  102. filesize: toNaturalFileSize(
  103. totalSizeMax,
  104. '.',
  105. query('GET_FILE_SIZE_BASE'),
  106. query('GET_FILE_SIZE_LABELS', query)
  107. ),
  108. }),
  109. },
  110. });
  111. return;
  112. }
  113. }
  114. // file is fine, let's pass it back
  115. resolve(file);
  116. });
  117. });
  118. return {
  119. options: {
  120. // Enable or disable file type validation
  121. allowFileSizeValidation: [true, Type.BOOLEAN],
  122. // Max individual file size in bytes
  123. maxFileSize: [null, Type.INT],
  124. // Min individual file size in bytes
  125. minFileSize: [null, Type.INT],
  126. // Max total file size in bytes
  127. maxTotalFileSize: [null, Type.INT],
  128. // Filter the files that need to be validated for size
  129. fileValidateSizeFilter: [null, Type.FUNCTION],
  130. // error labels
  131. labelMinFileSizeExceeded: ['File is too small', Type.STRING],
  132. labelMinFileSize: ['Minimum file size is {filesize}', Type.STRING],
  133. labelMaxFileSizeExceeded: ['File is too large', Type.STRING],
  134. labelMaxFileSize: ['Maximum file size is {filesize}', Type.STRING],
  135. labelMaxTotalFileSizeExceeded: ['Maximum total size exceeded', Type.STRING],
  136. labelMaxTotalFileSize: ['Maximum total file size is {filesize}', Type.STRING],
  137. },
  138. };
  139. };
  140. // fire pluginloaded event if running in browser, this allows registering the plugin when using async script tags
  141. var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
  142. if (isBrowser) {
  143. document.dispatchEvent(new CustomEvent('FilePond:pluginloaded', { detail: plugin }));
  144. }
  145. return plugin;
  146. });