active_storage_drag_and_drop 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77831f698945cda19e6fcfe3a3dac981cb187c8132828626e2b3885f9b9f26ab
4
- data.tar.gz: e1e23d401dff7989da878494ddc812d41911a0da8598e5f56ecc708ceaebfb53
3
+ metadata.gz: 9a079fe553c09b15a8944c7019a74835357838464fecc48c3d2321c8427c9113
4
+ data.tar.gz: 89fdd70988c3bef9cbd506aff7aff780d4399c58efd930f9c7aa7edbe00d15c5
5
5
  SHA512:
6
- metadata.gz: 10f40a62e0dcefe74574a63bb204b76c4ae769f2f001599b508fa33949c05e653ed53cc5efc93eaf071d3b2d30ed5a15f40ebca2c12435522a0476c60dc919a4
7
- data.tar.gz: b528ac93389424f60d5862b2c33986ee940c464e1d8e2c9ffd29630dc34da8d7d3d5fefe0ccd7183b71b25c96e782a19a50201762b5292be96dc7336bc7c2f9f
6
+ metadata.gz: 39147a94d61ea43f6f19779b89ca6a740a24d28d11facbc2f377615091be1bc871ae88a840ec70c80be5f3173db47bcf2d8c9fe96f04adbecf1262961a7b3ed6
7
+ data.tar.gz: 218dfed1043e5d88e1b261d05f5b3a01cc25d241b74f108a30cb00cec45ce8b8db67a43144992fe28b031864a6ac47af0bb3176a5a254a6296cbbf986a61a517
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_storage_drag_and_drop (0.3.1)
4
+ active_storage_drag_and_drop (0.3.2)
5
5
  rails (~> 5.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -50,6 +50,10 @@ Options for the nested file field can be passed as key value pairs:
50
50
  ```ruby
51
51
  form.drag_and_drop_file_field :images, accept: 'image/png, image/jpeg, image/gif, image/tiff'
52
52
  ```
53
+ An additional `size_limit` option can be passed which provides validation on the maximum acceptable filesize in bytes:
54
+ ```ruby
55
+ form.drag_and_drop_file_field :images, size_limit: 5_000_000 # 5MB upper limit on file size
56
+ ```
53
57
  In your controller you can permit the params like so:
54
58
  ```ruby
55
59
  params.permit(:message).require(images: [])
@@ -152,7 +152,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) *
152
152
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
153
153
 
154
154
  "use strict";
155
- eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"uploaders\", function() { return uploaders; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"UploadQueueProcessor\", function() { return UploadQueueProcessor; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"createUploader\", function() { return createUploader; });\n/* harmony import */ var _helpers__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./helpers */ \"./app/javascript/active_storage_drag_and_drop/helpers.js\");\n/* harmony import */ var _direct_upload_controller__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./direct_upload_controller */ \"./app/javascript/active_storage_drag_and_drop/direct_upload_controller.js\");\n\n\nconst uploaders = [];\n\nconst eventFamily = 'dnd-uploads';\n\nclass UploadQueueProcessor {\n constructor(form) {\n this.form = form;\n this.current_uploaders = [];\n uploaders.forEach(uploader => {\n if (form == uploader.form) {\n this.current_uploaders.push(uploader);\n }\n });\n }\n\n start(callback) {\n const startNextUploader = () => {\n const nextUploader = this.current_uploaders.shift();\n if (nextUploader) {\n nextUploader.start(error => {\n if (error) {\n callback(error);\n this.dispatch(\"end\");\n } else {\n startNextUploader();\n }\n });\n } else {\n callback();\n this.dispatch(\"end\");\n }\n };\n\n this.dispatch(\"start\");\n startNextUploader();\n }\n\n dispatch(name, detail = {}) {\n return Object(_helpers__WEBPACK_IMPORTED_MODULE_0__[\"dispatchEvent\"])(this.form, `${eventFamily}:${name}`, { detail });\n }\n}\n\nfunction createUploader(input, file) {\n // your form needs the file_field direct_upload: true, which\n // provides data-direct-upload-url\n console.log(input.accept === '');\n if (input.accept === '' || input.accept.split(', ').includes(file.type)) {\n uploaders.push(new _direct_upload_controller__WEBPACK_IMPORTED_MODULE_1__[\"DragAndDropUploadController\"](input, file));\n } else {\n const error = Error('Invalid filetype');\n const event = Object(_helpers__WEBPACK_IMPORTED_MODULE_0__[\"dispatchEvent\"])(input, `${eventFamily}:error`, { error });\n if (!event.defaultPrevented) {\n alert(error);\n }\n return event;\n }\n}\n\n//# sourceURL=webpack://ActiveStorage/./app/javascript/active_storage_drag_and_drop/upload_queue_processor.js?");
155
+ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"uploaders\", function() { return uploaders; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"UploadQueueProcessor\", function() { return UploadQueueProcessor; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"createUploader\", function() { return createUploader; });\n/* harmony import */ var _helpers__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./helpers */ \"./app/javascript/active_storage_drag_and_drop/helpers.js\");\n/* harmony import */ var _direct_upload_controller__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./direct_upload_controller */ \"./app/javascript/active_storage_drag_and_drop/direct_upload_controller.js\");\n\n\nconst uploaders = [];\n\nconst eventFamily = 'dnd-uploads';\n\nclass ValidationError extends Error {\n constructor(...args) {\n super(...args);\n Error.captureStackTrace(this, ValidationError);\n }\n}\n\nclass UploadQueueProcessor {\n constructor(form) {\n this.form = form;\n this.current_uploaders = [];\n uploaders.forEach(uploader => {\n if (form == uploader.form) {\n this.current_uploaders.push(uploader);\n }\n });\n }\n\n start(callback) {\n const startNextUploader = () => {\n const nextUploader = this.current_uploaders.shift();\n if (nextUploader) {\n nextUploader.start(error => {\n if (error) {\n callback(error);\n this.dispatch(\"end\");\n } else {\n startNextUploader();\n }\n });\n } else {\n callback();\n this.dispatch(\"end\");\n }\n };\n\n this.dispatch(\"start\");\n startNextUploader();\n }\n\n dispatch(name, detail = {}) {\n return Object(_helpers__WEBPACK_IMPORTED_MODULE_0__[\"dispatchEvent\"])(this.form, `${eventFamily}:${name}`, { detail });\n }\n}\n\nfunction createUploader(input, file) {\n // your form needs the file_field direct_upload: true, which\n // provides data-direct-upload-url\n const error = validateUploader(input, file);\n if (error) {\n const event = Object(_helpers__WEBPACK_IMPORTED_MODULE_0__[\"dispatchEvent\"])(input, `${eventFamily}:error`, { error });\n if (!event.defaultPrevented) {\n alert(error);\n }\n return event;\n }\n uploaders.push(new _direct_upload_controller__WEBPACK_IMPORTED_MODULE_1__[\"DragAndDropUploadController\"](input, file));\n}\n\nfunction validateUploader(input, file) {\n const sizeLimit = input.getAttribute('size_limit');\n if (input.accept !== '' && !input.accept.split(', ').includes(file.type)) {\n return new ValidationError('Invalid filetype');\n } else if (sizeLimit && file.size > sizeLimit) {\n return new ValidationError(`File too large. Can be no larger than ${humanFileSize(sizeLimit)}`);\n }\n}\n\nfunction humanFileSize(bytes) {\n var thresh = 1000;\n if (Math.abs(bytes) < thresh) {\n return bytes + ' B';\n }\n var units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];\n var u = -1;\n do {\n bytes /= thresh;\n ++u;\n } while (Math.abs(bytes) >= thresh && u < units.length - 1);\n return bytes.toFixed(1) + ' ' + units[u];\n}\n\n//# sourceURL=webpack://ActiveStorage/./app/javascript/active_storage_drag_and_drop/upload_queue_processor.js?");
156
156
 
157
157
  /***/ }),
158
158
 
@@ -4,6 +4,13 @@ export const uploaders = []
4
4
 
5
5
  const eventFamily = 'dnd-uploads'
6
6
 
7
+ class ValidationError extends Error {
8
+ constructor (...args) {
9
+ super(...args)
10
+ Error.captureStackTrace(this, ValidationError)
11
+ }
12
+ }
13
+
7
14
  export class UploadQueueProcessor {
8
15
  constructor(form) {
9
16
  this.form = form
@@ -45,15 +52,36 @@ export class UploadQueueProcessor {
45
52
  export function createUploader(input, file) {
46
53
  // your form needs the file_field direct_upload: true, which
47
54
  // provides data-direct-upload-url
48
- console.log(input.accept === '')
49
- if (input.accept === '' || input.accept.split(', ').includes(file.type)) {
50
- uploaders.push(new DragAndDropUploadController(input, file))
51
- } else {
52
- const error = Error('Invalid filetype')
55
+ const error = validateUploader(input, file)
56
+ if (error) {
53
57
  const event = dispatchEvent(input, `${eventFamily}:error`, { error })
54
58
  if (!event.defaultPrevented) {
55
59
  alert(error)
56
60
  }
57
61
  return event
58
62
  }
63
+ uploaders.push(new DragAndDropUploadController(input, file))
64
+ }
65
+
66
+ function validateUploader (input, file) {
67
+ const sizeLimit = input.getAttribute('size_limit')
68
+ if (input.accept !== '' && !input.accept.split(', ').includes(file.type)) {
69
+ return new ValidationError('Invalid filetype')
70
+ } else if (sizeLimit && file.size > sizeLimit) {
71
+ return new ValidationError(`File too large. Can be no larger than ${humanFileSize(sizeLimit)}`)
72
+ }
73
+ }
74
+
75
+ function humanFileSize (bytes) {
76
+ var thresh = 1000
77
+ if (Math.abs(bytes) < thresh) {
78
+ return bytes + ' B'
79
+ }
80
+ var units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
81
+ var u = -1
82
+ do {
83
+ bytes /= thresh
84
+ ++u
85
+ } while (Math.abs(bytes) >= thresh && u < units.length - 1)
86
+ return bytes.toFixed(1) + ' ' + units[u]
59
87
  }
@@ -1,3 +1,3 @@
1
1
  module ActiveStorageDragAndDrop
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage_drag_and_drop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dave O'Keeffe
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-10-24 00:00:00.000000000 Z
12
+ date: 2018-10-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler