Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

on_begin callback added

  • Loading branch information...
commit f3b8e495adce86b3a87088638263890441f6e129 1 parent cec4a1f
@maxgillett authored
View
6 Gemfile.lock
@@ -1,9 +1,9 @@
PATH
remote: .
specs:
- s3_multipart (0.0.1)
- uuid
- xml-simple
+ s3_multipart (0.0.2)
+ uuid (>= 2.3.6)
+ xml-simple (>= 1.1.2)
GEM
remote: https://rubygems.org/
View
0  LICENSE
No changes.
View
20 LICENSE.txt
@@ -0,0 +1,20 @@
+Copyright (c) Max Gillett
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
3  app/controllers/s3_multipart/uploads_controller.rb
@@ -5,6 +5,7 @@ def create
response = Upload.initiate(params)
upload = Upload.create(key: response["key"], upload_id: response["upload_id"], name: response["name"])
response["id"] = upload["id"]
+ upload.on_begin if upload.respond_to?(:on_begin)
rescue
response = {error: 'There was an error initiating the upload'}
ensure
@@ -45,7 +46,7 @@ def complete_upload
response = Upload.complete(params)
upload = Upload.find_by_upload_id(params[:upload_id])
upload.update_attributes(location: response[:location])
- upload.on_complete
+ upload.on_complete if upload.respond_to?(:on_complete)
rescue
response = {error: 'There was an error completing the upload'}
ensure
View
1  app/models/s3_multipart/upload.rb
@@ -3,6 +3,7 @@ class Upload < ::ActiveRecord::Base
class << self
include S3Multipart::Uploader
attr_accessor :on_complete_callback
+ attr_accessor :on_begin_callback
end
attr_accessible :key, :upload_id, :name, :location
View
31 lib/s3_multipart.rb
@@ -15,6 +15,7 @@ class << self
# config.s3_secret_key = ''
# config.bucket_name = ''
# end
+
def configure(&block)
S3Multipart::Uploader::Config.configure(block)
end
@@ -27,18 +28,28 @@ def remove_unfinished_uploads(seconds=60*60*24*10)
module ActionControllerHelpers
- #
- # Stores a code block in the Upload class that will be executed (and passed the
- # completed upload object) when the on_complete method is called on an
- # instance of the Upload class
- #
- def attach_uploader(&block)
- S3Multipart::Upload.class_eval do
- self.on_complete_callback = block
- def on_complete
- Upload.on_complete_callback.call(self)
+ module AttachUploader
+ def self.on_begin(&block)
+ S3Multipart::Upload.class_eval do
+ self.on_begin_callback = block
+ def on_begin
+ Upload.on_begin_callback.call(self)
+ end
end
end
+
+ def self.on_complete(&block)
+ S3Multipart::Upload.class_eval do
+ self.on_complete_callback = block
+ def on_complete
+ Upload.on_complete_callback.call(self)
+ end
+ end
+ end
+ end
+
+ def attach_uploader
+ return AttachUploader
end
end
View
2  lib/s3_multipart/version.rb
@@ -1,3 +1,3 @@
module S3Multipart
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
View
BIN  s3_multipart-0.0.1.gem
Binary file not shown
View
6 s3_multipart.gemspec
@@ -8,12 +8,12 @@ Gem::Specification.new do |s|
s.version = S3Multipart::VERSION
s.authors = ["Max Gillett"]
s.email = ["max.gillett@gmail.com"]
- s.homepage = ""
+ s.homepage = "https://github.com/maxgillett/s3_multipart"
s.summary = %q{Upload directly to S3 using multipart uploading}
s.description = %q{See github for installation and configuration }
- s.add_dependency "uuid"
- s.add_dependency "xml-simple"
+ s.add_dependency "uuid", ">= 2.3.6"
+ s.add_dependency "xml-simple", ">= 1.1.2"
s.add_development_dependency 'combustion', '~> 0.3.3'
s.add_development_dependency "rails"
View
3  spec/internal/app/assets/javascripts/application.js
@@ -19,6 +19,9 @@ $(function() {
window.SS3MP = new window.S3MP({
bucket: 'bitcast-bucket',
fileSelector: "#uploader",
+ onStart: function(num) {
+ console.log("File "+num+" has started uploading")
+ },
onComplete: function(num) {
console.log("File "+num+" successfully uploaded")
},
View
10 spec/internal/app/controllers/pages_controller.rb
@@ -1,9 +1,13 @@
class PagesController < ApplicationController
def upload
- attach_uploader do |upload|
- upload.update_attributes(name: "modified")
+ upload = attach_uploader
+ upload.on_begin do |upload|
+ upload.update_attributes(name: "begun")
+ end
+ upload.on_complete do |upload|
+ upload.update_attributes(name: "completed")
end
end
-
+
end
View
BIN  spec/internal/db/combustion_test.sqlite3
Binary file not shown
View
10 vendor/assets/javascripts/s3_multipart/s3_multipart.js
@@ -23,6 +23,9 @@
options = options || {
fileSelector: null,
bucket: null,
+ onStart: function(num) {
+ console.log("File "+num+" has started uploading")
+ },
onComplete: function(num) {
console.log("File "+num+" successfully uploaded")
},
@@ -74,6 +77,7 @@
uploadObj.parts[j].activate();
}
S3MP.handler.startProgressTimer(key);
+ S3MP.onStart(key); // This probably needs to go somewhere else.
}
}
return beginUpload;
@@ -148,10 +152,12 @@
this.clearProgressTimer(key);
// Tell the server to put together the pieces
- S3MP.completeMultipart(uploadObj, function() {
+ S3MP.completeMultipart(uploadObj, function(obj) {
// Notify the client that the upload has succeeded when we
// get confirmation from the server
- S3MP.onComplete(key);
+ if (obj.location) {
+ S3MP.onComplete(key);
+ }
});
},
Please sign in to comment.
Something went wrong with that request. Please try again.