Class: S3Uploader::MainController
- Inherits:
-
Volt::ModelController
- Object
- Volt::ModelController
- S3Uploader::MainController
- Defined in:
- app/s3_uploader/controllers/main_controller.rb
Instance Attribute Summary collapse
-
#uploading_promise ⇒ Object
Returns the value of attribute uploading_promise.
Instance Method Summary collapse
- #bucket_name ⇒ Object
- #error(msg) ⇒ Object
- #set_progress(progress, error = nil, done = false) ⇒ Object
- #upload(event) ⇒ Object
- #upload_file(file) ⇒ Object
- #upload_with_url(file, signed_url) ⇒ Object
-
#uploaded ⇒ Object
Resolve the uploading promise.
Instance Attribute Details
#uploading_promise ⇒ Object
Returns the value of attribute uploading_promise.
7 8 9 |
# File 'app/s3_uploader/controllers/main_controller.rb', line 7 def uploading_promise @uploading_promise end |
Instance Method Details
#bucket_name ⇒ Object
11 12 13 14 |
# File 'app/s3_uploader/controllers/main_controller.rb', line 11 def bucket_name file_field_name = attrs.value_last_method attrs.value_parent.send(file_field_name + "_bucket") end |
#error(msg) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'app/s3_uploader/controllers/main_controller.rb', line 84 def error(msg) msg = msg.to_s promsise = self.uploading_promise self.uploading_promise = nil set_progress(0, msg, true) promsise.reject(msg) end |
#set_progress(progress, error = nil, done = false) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'app/s3_uploader/controllers/main_controller.rb', line 93 def set_progress(progress, error=nil, done=false) self.progress = progress self. = error if attrs.respond_to?(:value) attrs.value = [progress, done ? s3_url : nil, @uploading_promise] end end |
#upload(event) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/s3_uploader/controllers/main_controller.rb', line 16 def upload(event) target = event.target set_progress(0) file = nil ` var files = target.files; var output = []; for (var i = 0, f; file = files[i]; i++) { #{upload_file(file)} } ` end |
#upload_file(file) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/s3_uploader/controllers/main_controller.rb', line 31 def upload_file(file) self.uploading_promise = Promise.new puts "UP FILE: #{uploading_promise.inspect}" S3UploadTasks.sign(bucket_name, `file.name`, `file.type`).then do |private_and_public| self.s3_url = private_and_public[1] upload_with_url(file, private_and_public[0]) end.fail do |err| set_progress(0, 'Could not contact signing script. Status = ' + err.to_s) end end |
#upload_with_url(file, signed_url) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/s3_uploader/controllers/main_controller.rb', line 43 def upload_with_url(file, signed_url) # create PUT request to S3 ` var xhr = new XMLHttpRequest(); xhr.open('PUT', signed_url); xhr.setRequestHeader('Content-Type', file.type); xhr.onload = function() { if (xhr.status == 200) { #{uploaded} } }; xhr.onerror = function(e) { self.$error(e.error || 'Upload Error'); }; xhr.upload.onprogress = function(e) { console.log('onprogress'); if (e.lengthComputable) { #{percentLoaded = `Math.round((e.loaded / e.total) * 100)`}; #{set_progress(percentLoaded, nil)} } }; xhr.send(file); ` nil end |
#uploaded ⇒ Object
Resolve the uploading promise
76 77 78 79 80 81 82 |
# File 'app/s3_uploader/controllers/main_controller.rb', line 76 def uploaded promise = self.uploading_promise self.uploading_promise = nil set_progress(100, nil, true) promise.resolve(nil) end |