s3_multipart 0.0.4 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +11 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +8 -1
- data/README.md +150 -42
- data/Rakefile +10 -0
- data/app/controllers/s3_multipart/application_controller.rb +6 -3
- data/app/controllers/s3_multipart/uploads_controller.rb +30 -29
- data/app/models/s3_multipart/upload.rb +13 -5
- data/grunt.js +44 -0
- data/javascripts/footer.js +5 -0
- data/javascripts/header.js +14 -0
- data/javascripts/libs/underscore.js +1 -0
- data/javascripts/s3mp.js +329 -0
- data/javascripts/upload.js +76 -0
- data/javascripts/uploadpart.js +32 -0
- data/lib/generators/s3_multipart/install_generator.rb +30 -0
- data/lib/generators/s3_multipart/templates/add_uploader_column_to_model.rb +7 -0
- data/lib/generators/s3_multipart/templates/aws.yml +4 -0
- data/lib/generators/s3_multipart/templates/configuration_initializer.rb +8 -0
- data/lib/generators/s3_multipart/templates/uploader.rb +29 -0
- data/{db/migrate/20110727184726_create_s3_multipart_uploads.rb → lib/generators/s3_multipart/templates/uploads_table_migration.rb} +2 -0
- data/lib/generators/s3_multipart/uploader_generator.rb +33 -0
- data/lib/s3_multipart/action_view_helpers/form_helper.rb +2 -1
- data/lib/s3_multipart/config.rb +12 -0
- data/lib/s3_multipart/http/net_http.rb +6 -6
- data/lib/s3_multipart/railtie.rb +12 -0
- data/lib/s3_multipart/transfer_helpers.rb +92 -0
- data/lib/s3_multipart/uploader/callbacks.rb +17 -0
- data/lib/s3_multipart/uploader/validations.rb +9 -0
- data/lib/s3_multipart/uploader.rb +27 -68
- data/lib/s3_multipart/version.rb +1 -1
- data/lib/s3_multipart.rb +4 -42
- data/package.json +9 -0
- data/s3_multipart.gemspec +6 -3
- data/spec/integration/uploads_controller_spec.rb +2 -1
- data/spec/internal/app/assets/javascripts/application.js +148 -63
- data/spec/internal/app/assets/stylesheets/application.css.scss +384 -0
- data/spec/internal/app/assets/stylesheets/font-awesome.scss +499 -0
- data/spec/internal/app/controllers/application_controller.rb +4 -1
- data/spec/internal/app/controllers/pages_controller.rb +3 -7
- data/spec/internal/app/models/user.rb +4 -0
- data/spec/internal/app/models/video.rb +5 -0
- data/spec/internal/app/uploaders/multipart/video_uploader.rb +32 -0
- data/spec/internal/app/views/pages/upload.html.erb +38 -4
- data/spec/internal/config/routes.rb +1 -1
- data/spec/internal/db/schema.rb +22 -14
- data/spec/internal/public/fonts/FontAwesome.otf +0 -0
- data/spec/internal/public/fonts/fontawesome-webfont.eot +0 -0
- data/spec/internal/public/fonts/fontawesome-webfont.ttf +0 -0
- data/spec/internal/public/fonts/fontawesome-webfont.woff +0 -0
- data/spec/internal/tmp/cache/sass/077f66d4d9153cfd737b81ab3f4c5d5858b0db3b/_grid-background.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_appearance.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_background-clip.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_background-origin.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_background-size.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_border-radius.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_box-shadow.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_box-sizing.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_box.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_columns.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_filter.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_font-face.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_hyphenation.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_images.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_inline-block.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_opacity.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_regions.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_shared.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_text-shadow.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_transform.scssc +0 -0
- data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_transition.scssc +0 -0
- data/spec/internal/tmp/cache/sass/351072894b53bf1a2d2af4bd57c177c805cf063e/_contrast.scssc +0 -0
- data/spec/internal/tmp/cache/sass/49ecd6d86aba13e15e515be708bbfd8bb67a011a/_css3.scssc +0 -0
- data/spec/internal/tmp/cache/sass/49ecd6d86aba13e15e515be708bbfd8bb67a011a/_support.scssc +0 -0
- data/spec/internal/tmp/cache/sass/49ecd6d86aba13e15e515be708bbfd8bb67a011a/_typography.scssc +0 -0
- data/spec/internal/tmp/cache/sass/49ecd6d86aba13e15e515be708bbfd8bb67a011a/_utilities.scssc +0 -0
- data/spec/internal/tmp/cache/sass/633bc5f017268b81a60f921876c980c4adcebb74/_base.scssc +0 -0
- data/spec/internal/tmp/cache/sass/633bc5f017268b81a60f921876c980c4adcebb74/_sprite-img.scssc +0 -0
- data/spec/internal/tmp/cache/sass/68dbce8ac3037e0cd797c9b5c6aa131096361144/_utilities.scssc +0 -0
- data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_clearfix.scssc +0 -0
- data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_float.scssc +0 -0
- data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_hacks.scssc +0 -0
- data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_min.scssc +0 -0
- data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_reset.scssc +0 -0
- data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_tag-cloud.scssc +0 -0
- data/spec/internal/tmp/cache/sass/775792c99f07f64e98296a8dc8c9d2c74a28fd8a/_reset.scssc +0 -0
- data/spec/internal/tmp/cache/sass/8b4f1545a124f2498699911561d1ec6302c9de56/_ellipsis.scssc +0 -0
- data/spec/internal/tmp/cache/sass/8b4f1545a124f2498699911561d1ec6302c9de56/_force-wrap.scssc +0 -0
- data/spec/internal/tmp/cache/sass/8b4f1545a124f2498699911561d1ec6302c9de56/_nowrap.scssc +0 -0
- data/spec/internal/tmp/cache/sass/8b4f1545a124f2498699911561d1ec6302c9de56/_replacement.scssc +0 -0
- data/spec/internal/tmp/cache/sass/abd68615441476dbd8e9fdc1606210305b3177fd/_bullets.scssc +0 -0
- data/spec/internal/tmp/cache/sass/abd68615441476dbd8e9fdc1606210305b3177fd/_horizontal-list.scssc +0 -0
- data/spec/internal/tmp/cache/sass/abd68615441476dbd8e9fdc1606210305b3177fd/_inline-block-list.scssc +0 -0
- data/spec/internal/tmp/cache/sass/abd68615441476dbd8e9fdc1606210305b3177fd/_inline-list.scssc +0 -0
- data/spec/internal/tmp/cache/sass/b1bb3e2fbcb5c6a71ea2273c6dcb7c8967d2a058/_alternating-rows-and-columns.scssc +0 -0
- data/spec/internal/tmp/cache/sass/b1bb3e2fbcb5c6a71ea2273c6dcb7c8967d2a058/_borders.scssc +0 -0
- data/spec/internal/tmp/cache/sass/b1bb3e2fbcb5c6a71ea2273c6dcb7c8967d2a058/_scaffolding.scssc +0 -0
- data/spec/internal/tmp/cache/sass/bc7f11d6e8f07a22f545deb35760cfc7830f3369/application.css.scssc +0 -0
- data/spec/internal/tmp/cache/sass/bc7f11d6e8f07a22f545deb35760cfc7830f3369/font-awesome.scssc +0 -0
- data/spec/internal/tmp/cache/sass/c9342b40bf8bc7bf64ce587860fb065cbf6d2ca4/_color.scssc +0 -0
- data/spec/internal/tmp/cache/sass/c9342b40bf8bc7bf64ce587860fb065cbf6d2ca4/_general.scssc +0 -0
- data/spec/internal/tmp/cache/sass/c9342b40bf8bc7bf64ce587860fb065cbf6d2ca4/_sprites.scssc +0 -0
- data/spec/internal/tmp/cache/sass/c9342b40bf8bc7bf64ce587860fb065cbf6d2ca4/_tables.scssc +0 -0
- data/spec/internal/tmp/cache/sass/d1da036a47062a9f69ce0327962c00aa77d2ea44/_hover-link.scssc +0 -0
- data/spec/internal/tmp/cache/sass/d1da036a47062a9f69ce0327962c00aa77d2ea44/_link-colors.scssc +0 -0
- data/spec/internal/tmp/cache/sass/d1da036a47062a9f69ce0327962c00aa77d2ea44/_unstyled-link.scssc +0 -0
- data/spec/internal/tmp/cache/sass/d559b7bf0c5e0c0d23f2f34bfc21b4817fb38463/_links.scssc +0 -0
- data/spec/internal/tmp/cache/sass/d559b7bf0c5e0c0d23f2f34bfc21b4817fb38463/_lists.scssc +0 -0
- data/spec/internal/tmp/cache/sass/d559b7bf0c5e0c0d23f2f34bfc21b4817fb38463/_text.scssc +0 -0
- data/spec/internal/tmp/cache/sass/d559b7bf0c5e0c0d23f2f34bfc21b4817fb38463/_vertical_rhythm.scssc +0 -0
- data/spec/internal/tmp/cache/sass/e2e5b9bb57a9d1b018b9f546bc8b16f6b2a627a9/_compass.scssc +0 -0
- data/spec/internal/tmp/cache/sass/e654955d4502954a409bf21071c0d98eb4ac2e9f/_utilities.scssc +0 -0
- data/spec/javascripts/UploadSpec.js +75 -0
- data/spec/javascripts/helpers/SpecHelper.js +9 -0
- data/spec/javascripts/support/jasmine.yml +84 -0
- data/spec/unit/upload_controller_spec.rb +31 -0
- data/spec/{requests/uploader_spec.rb → unit/upload_spec.rb} +9 -10
- data/spec/unit/uploader_module_spec.rb +31 -0
- data/vendor/assets/javascripts/s3_multipart/lib.js +456 -0
- data/vendor/assets/javascripts/s3_multipart/lib.min.js +1 -0
- metadata +103 -12
- data/.rspec +0 -1
- data/lib/s3_multipart/uploader/config.rb +0 -15
- data/spec/internal/app/assets/stylesheets/application.css +0 -0
- data/spec/internal/db/combustion_test.sqlite3 +0 -0
- data/vendor/assets/javascripts/s3_multipart/index.js +0 -1
- data/vendor/assets/javascripts/s3_multipart/s3_multipart.js +0 -478
data/javascripts/s3mp.js
ADDED
@@ -0,0 +1,329 @@
|
|
1
|
+
// S3MP Constructor
|
2
|
+
function S3MP(options) {
|
3
|
+
var files
|
4
|
+
, progress_timer = []
|
5
|
+
, S3MP = this;
|
6
|
+
|
7
|
+
_.extend(this, options);
|
8
|
+
|
9
|
+
this.uploadList = [];
|
10
|
+
|
11
|
+
// Handles all of the success/failure events, and
|
12
|
+
// progress notifiers
|
13
|
+
this.handler = {
|
14
|
+
|
15
|
+
// Activate an appropriate number of parts (number = pipes)
|
16
|
+
// when all of the parts have been successfully initialized
|
17
|
+
beginUpload: function() {
|
18
|
+
var i = [];
|
19
|
+
function beginUpload(pipes, uploadObj) {
|
20
|
+
var key = uploadObj.key
|
21
|
+
, num_parts = uploadObj.parts.length
|
22
|
+
|
23
|
+
if (typeof i[key] === "undefined") {
|
24
|
+
i[key] = 0;
|
25
|
+
}
|
26
|
+
|
27
|
+
i[key]++;
|
28
|
+
|
29
|
+
if (i[key] === num_parts) {
|
30
|
+
for (var j=0; j<pipes; j++) {
|
31
|
+
uploadObj.parts[j].activate();
|
32
|
+
}
|
33
|
+
S3MP.handler.startProgressTimer(key);
|
34
|
+
S3MP.onStart(uploadObj); // This probably needs to go somewhere else.
|
35
|
+
}
|
36
|
+
}
|
37
|
+
return beginUpload;
|
38
|
+
}(),
|
39
|
+
|
40
|
+
// called when an upload is paused or the network connection cuts out
|
41
|
+
onError: function(uploadObj, part) {
|
42
|
+
// To-do
|
43
|
+
},
|
44
|
+
|
45
|
+
// called when a single part has successfully uploaded
|
46
|
+
onPartSuccess: function(uploadObj, finished_part) {
|
47
|
+
var parts, i, ETag;
|
48
|
+
|
49
|
+
parts = uploadObj.parts;
|
50
|
+
|
51
|
+
// Append the ETag (in the response header) to the ETags array
|
52
|
+
ETag = finished_part.xhr.getResponseHeader("ETag");
|
53
|
+
uploadObj.Etags.push({ ETag: ETag.replace(/\"/g, ''), partNum: finished_part.num });
|
54
|
+
|
55
|
+
// Increase the uploaded count and delete the finished part
|
56
|
+
uploadObj.uploaded += finished_part.size;
|
57
|
+
uploadObj.inprogress[finished_part.num] = 0;
|
58
|
+
i = _.indexOf(parts, finished_part);
|
59
|
+
parts.splice(i,1);
|
60
|
+
|
61
|
+
// activate one of the remaining parts
|
62
|
+
if (parts.length) {
|
63
|
+
i = _.findIndex(parts, function(el, index, collection) {
|
64
|
+
if (el.status !== "active") {
|
65
|
+
return true;
|
66
|
+
}
|
67
|
+
});
|
68
|
+
if (i !== -1){
|
69
|
+
parts[i].activate();
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
// If no parts remain then the upload has finished
|
74
|
+
if (!parts.length) {
|
75
|
+
this.onComplete(uploadObj);
|
76
|
+
}
|
77
|
+
},
|
78
|
+
|
79
|
+
// called when all parts have successfully uploaded
|
80
|
+
onComplete: function(uploadObj) {
|
81
|
+
var key = _.indexOf(S3MP.uploadList, uploadObj);
|
82
|
+
|
83
|
+
// Stop the onprogress timer
|
84
|
+
this.clearProgressTimer(key);
|
85
|
+
|
86
|
+
// Tell the server to put together the pieces
|
87
|
+
S3MP.completeMultipart(uploadObj, function(obj) {
|
88
|
+
// Notify the client that the upload has succeeded when we
|
89
|
+
// get confirmation from the server
|
90
|
+
if (obj.location) {
|
91
|
+
S3MP.onComplete(uploadObj);
|
92
|
+
}
|
93
|
+
});
|
94
|
+
|
95
|
+
},
|
96
|
+
|
97
|
+
// Called by progress_timer
|
98
|
+
onProgress: function(key, size, done, percent, speed) {
|
99
|
+
S3MP.onProgress(key, size, done, percent, speed);
|
100
|
+
},
|
101
|
+
|
102
|
+
startProgressTimer: function() {
|
103
|
+
var last_upload_chunk = [];
|
104
|
+
var fn = function(key) {
|
105
|
+
progress_timer[key] = global.setInterval(function() {
|
106
|
+
var upload, size, done, percent, speed;
|
107
|
+
|
108
|
+
if (typeof last_upload_chunk[key] === "undefined") {
|
109
|
+
last_upload_chunk[key] = 0;
|
110
|
+
}
|
111
|
+
|
112
|
+
upload = S3MP.uploadList[key];
|
113
|
+
size = upload.size;
|
114
|
+
done = upload.uploaded;
|
115
|
+
|
116
|
+
_.each(upload.inprogress,function(val) {
|
117
|
+
done += val;
|
118
|
+
});
|
119
|
+
|
120
|
+
percent = done/size * 100;
|
121
|
+
speed = done - last_upload_chunk[key];
|
122
|
+
last_upload_chunk[key] = done;
|
123
|
+
|
124
|
+
upload.handler.onProgress(key, size, done, percent, speed);
|
125
|
+
}, 1000);
|
126
|
+
};
|
127
|
+
return fn;
|
128
|
+
}(),
|
129
|
+
|
130
|
+
clearProgressTimer: function(key) {
|
131
|
+
global.clearInterval(progress_timer[key]);
|
132
|
+
}
|
133
|
+
|
134
|
+
};
|
135
|
+
|
136
|
+
// List of files may come from a FileList object or an array of files
|
137
|
+
if (this.fileSelector) {
|
138
|
+
files = $(this.fileSelector).get(0).files; // FileList object
|
139
|
+
} else {
|
140
|
+
files = this.fileList; // array specified in configuration
|
141
|
+
}
|
142
|
+
|
143
|
+
_.each(files, function(file, key) {
|
144
|
+
//Do validation for each file before creating a new upload object
|
145
|
+
// if (!file.type.match(/video/)) {
|
146
|
+
// return false;
|
147
|
+
// }
|
148
|
+
if (file.size < 5000000) {
|
149
|
+
return S3MP.onError({name: "FileSizeError", message: "File size is too small"})
|
150
|
+
}
|
151
|
+
|
152
|
+
var upload = new Upload(file, S3MP, key);
|
153
|
+
S3MP.uploadList.push(upload);
|
154
|
+
upload.init();
|
155
|
+
});
|
156
|
+
|
157
|
+
};
|
158
|
+
|
159
|
+
S3MP.prototype.initiateMultipart = function(upload, cb) {
|
160
|
+
var url, body, xhr;
|
161
|
+
|
162
|
+
url = '/s3_multipart/uploads';
|
163
|
+
body = JSON.stringify({ object_name : upload.name,
|
164
|
+
content_type : upload.type,
|
165
|
+
uploader : $(this.fileInputElement).data("uploader")
|
166
|
+
});
|
167
|
+
|
168
|
+
xhr = this.createXhrRequest('POST', url);
|
169
|
+
this.deliverRequest(xhr, body, cb);
|
170
|
+
|
171
|
+
};
|
172
|
+
|
173
|
+
S3MP.prototype.signPartRequests = function(id, object_name, upload_id, parts, cb) {
|
174
|
+
var content_lengths, url, body, xhr;
|
175
|
+
|
176
|
+
content_lengths = _.reduce(_.rest(parts), function(memo, part) {
|
177
|
+
return memo + "-" + part.size;
|
178
|
+
}, parts[0].size);
|
179
|
+
|
180
|
+
url = "s3_multipart/uploads/"+id;
|
181
|
+
body = JSON.stringify({ object_name : object_name,
|
182
|
+
upload_id : upload_id,
|
183
|
+
content_lengths : content_lengths
|
184
|
+
});
|
185
|
+
|
186
|
+
xhr = this.createXhrRequest('PUT', url);
|
187
|
+
this.deliverRequest(xhr, body, cb);
|
188
|
+
};
|
189
|
+
|
190
|
+
S3MP.prototype.completeMultipart = function(uploadObj, cb) {
|
191
|
+
var url, body, xhr;
|
192
|
+
|
193
|
+
url = 's3_multipart/uploads/'+uploadObj.id;
|
194
|
+
body = JSON.stringify({ object_name : uploadObj.object_name,
|
195
|
+
upload_id : uploadObj.upload_id,
|
196
|
+
content_length : uploadObj.size,
|
197
|
+
parts : uploadObj.Etags
|
198
|
+
});
|
199
|
+
|
200
|
+
xhr = this.createXhrRequest('PUT', url);
|
201
|
+
this.deliverRequest(xhr, body, cb);
|
202
|
+
};
|
203
|
+
|
204
|
+
// Specify callbacks, request body, and settings for requests that contact
|
205
|
+
// the site server, and send the request.
|
206
|
+
S3MP.prototype.deliverRequest = function(xhr, body, cb) {
|
207
|
+
var self = this;
|
208
|
+
|
209
|
+
xhr.onload = function() {
|
210
|
+
response = JSON.parse(this.responseText);
|
211
|
+
if (response.error) {
|
212
|
+
return self.onError({
|
213
|
+
name: "ServerResponse",
|
214
|
+
message: "The server responded with an error"
|
215
|
+
});
|
216
|
+
}
|
217
|
+
cb(response);
|
218
|
+
};
|
219
|
+
|
220
|
+
xhr.onerror = function() {
|
221
|
+
// To-do: Handle communication errors
|
222
|
+
};
|
223
|
+
|
224
|
+
xhr.setRequestHeader('Content-Type', 'application/json');
|
225
|
+
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
|
226
|
+
|
227
|
+
xhr.send(body);
|
228
|
+
}
|
229
|
+
|
230
|
+
S3MP.prototype.createXhrRequest = function() {
|
231
|
+
var xhrRequest;
|
232
|
+
|
233
|
+
// Sniff for xhr object
|
234
|
+
if (typeof XMLHttpRequest.constructor === "function") {
|
235
|
+
xhrRequest = XMLHttpRequest;
|
236
|
+
} else if (typeof XDomainRequest !== "undefined") {
|
237
|
+
xhrRequest = XDomainRequest;
|
238
|
+
} else {
|
239
|
+
xhrRequest = null; // Error out to the client (To-do)
|
240
|
+
}
|
241
|
+
|
242
|
+
return function(method, url, cb, open) { // open defaults to true
|
243
|
+
var args, xhr, open = true;
|
244
|
+
|
245
|
+
args = Array.prototype.slice.call(arguments);
|
246
|
+
if (typeof args[0] === "undefined") {
|
247
|
+
cb = null;
|
248
|
+
open = false;
|
249
|
+
}
|
250
|
+
|
251
|
+
xhr = new xhrRequest();
|
252
|
+
if (open) { // open the request unless specified otherwise
|
253
|
+
xhr.open(method, url, true);
|
254
|
+
}
|
255
|
+
xhr.onreadystatechange = cb;
|
256
|
+
|
257
|
+
return xhr;
|
258
|
+
};
|
259
|
+
|
260
|
+
}();
|
261
|
+
|
262
|
+
S3MP.prototype.sliceBlob = function() {
|
263
|
+
try {
|
264
|
+
var test_blob = new Blob();
|
265
|
+
} catch(e) {
|
266
|
+
return "Unsupported";
|
267
|
+
}
|
268
|
+
|
269
|
+
if (test_blob.slice) {
|
270
|
+
return function(blob, start, end) {
|
271
|
+
return blob.slice(start, end);
|
272
|
+
}
|
273
|
+
} else if (test_blob.mozSlice) {
|
274
|
+
return function(blob, start, end) {
|
275
|
+
return blob.mozSlice(start, end);
|
276
|
+
}
|
277
|
+
} else if (test_blob.webkitSlice) {
|
278
|
+
return function(blob, start, end) {
|
279
|
+
return blob.webkitSlice(start, end);
|
280
|
+
}
|
281
|
+
} else {
|
282
|
+
return "Unsupported";
|
283
|
+
}
|
284
|
+
}();
|
285
|
+
|
286
|
+
// utility function to return an upload object given a file
|
287
|
+
S3MP.prototype._returnUploadObj = function(key) {
|
288
|
+
var uploadObj = _.find(this.uploadList, function(uploadObj) {
|
289
|
+
return uploadObj.key === key;
|
290
|
+
});
|
291
|
+
return uploadObj;
|
292
|
+
};
|
293
|
+
|
294
|
+
// cancel a given file upload
|
295
|
+
S3MP.prototype.cancel = function(key) {
|
296
|
+
var uploadObj, i;
|
297
|
+
|
298
|
+
uploadObj = this._returnUploadObj(key);
|
299
|
+
i = _.indexOf(this.uploadList, uploadObj);
|
300
|
+
|
301
|
+
this.uploadList.splice(i,i+1);
|
302
|
+
this.onCancel();
|
303
|
+
};
|
304
|
+
|
305
|
+
// pause a given file upload
|
306
|
+
S3MP.prototype.pause = function(key) {
|
307
|
+
var uploadObj = this._returnUploadObj(key);
|
308
|
+
|
309
|
+
_.each(uploadObj.parts, function(part, key, list) {
|
310
|
+
if (part.status == "active") {
|
311
|
+
part.pause();
|
312
|
+
}
|
313
|
+
});
|
314
|
+
|
315
|
+
this.onPause();
|
316
|
+
};
|
317
|
+
|
318
|
+
// resume a given file upload
|
319
|
+
S3MP.prototype.resume = function(key) {
|
320
|
+
var uploadObj = this._returnUploadObj(key);
|
321
|
+
|
322
|
+
_.each(uploadObj.parts, function(part, key, list) {
|
323
|
+
if (part.status == "paused") {
|
324
|
+
part.activate();
|
325
|
+
}
|
326
|
+
});
|
327
|
+
|
328
|
+
this.onResume();
|
329
|
+
};
|
@@ -0,0 +1,76 @@
|
|
1
|
+
// Upload constructor
|
2
|
+
function Upload(file, o, key) {
|
3
|
+
function Upload() {
|
4
|
+
var upload, id, parts, part, segs, chunk_segs, chunk_lens, pipes, blob;
|
5
|
+
|
6
|
+
upload = this;
|
7
|
+
|
8
|
+
this.key = key;
|
9
|
+
this.file = file;
|
10
|
+
this.name = file.name;
|
11
|
+
this.size = file.size;
|
12
|
+
this.type = file.type;
|
13
|
+
this.Etags = [];
|
14
|
+
this.inprogress = [];
|
15
|
+
this.uploaded = 0;
|
16
|
+
this.status = "";
|
17
|
+
|
18
|
+
// Break the file into an appropriate amount of chunks
|
19
|
+
// This needs to be optimized for various browsers types/versions
|
20
|
+
if (this.size > 1000000000) { // size greater than 1gb
|
21
|
+
num_segs = 100;
|
22
|
+
pipes = 10;
|
23
|
+
} else if (this.size > 500000000) { // greater than 500mb
|
24
|
+
num_segs = 50;
|
25
|
+
pipes = 5;
|
26
|
+
} else if (this.size > 100000000) { // greater than 100 mb
|
27
|
+
num_segs = 20;
|
28
|
+
pipes = 5;
|
29
|
+
} else if (this.size > 5000000) { // greater than 5 mb (S3 does not allow multipart uploads < 5 mb)
|
30
|
+
num_segs = 2;
|
31
|
+
pipes = 2;
|
32
|
+
} else {
|
33
|
+
num_segs = 10;
|
34
|
+
pipes = 3;
|
35
|
+
}
|
36
|
+
|
37
|
+
chunk_segs = _.range(num_segs + 1);
|
38
|
+
chunk_lens = _.map(chunk_segs, function(seg) {
|
39
|
+
return Math.round(seg * (file.size/num_segs));
|
40
|
+
});
|
41
|
+
|
42
|
+
this.parts = _.map(chunk_lens, function(len, i) {
|
43
|
+
blob = upload.sliceBlob(file, len, chunk_lens[i+1]);
|
44
|
+
return new UploadPart(blob, i+1, upload);
|
45
|
+
});
|
46
|
+
|
47
|
+
this.parts.pop(); // Remove the empty blob at the end of the array
|
48
|
+
|
49
|
+
// init function will initiate the multipart upload, sign all the parts, and
|
50
|
+
// start uploading some parts in parallel
|
51
|
+
this.init = function() {
|
52
|
+
upload.initiateMultipart(upload, function(obj) {
|
53
|
+
var id = upload.id = obj.id
|
54
|
+
, upload_id = upload.upload_id = obj.upload_id
|
55
|
+
, object_name = upload.object_name = obj.key // uuid generated by the server, different from name
|
56
|
+
, parts = upload.parts;
|
57
|
+
|
58
|
+
upload.signPartRequests(id, object_name, upload_id, parts, function(response) {
|
59
|
+
_.each(parts, function(part, key) {
|
60
|
+
var xhr = part.xhr;
|
61
|
+
|
62
|
+
xhr.open('PUT', 'http://'+upload.bucket+'.s3.amazonaws.com/'+object_name+'?partNumber='+part.num+'&uploadId='+upload_id, true);
|
63
|
+
xhr.setRequestHeader('x-amz-date', response[key].date);
|
64
|
+
xhr.setRequestHeader('Authorization', response[key].authorization);
|
65
|
+
|
66
|
+
// Notify handler that an xhr request has been opened
|
67
|
+
upload.handler.beginUpload(pipes, upload);
|
68
|
+
});
|
69
|
+
});
|
70
|
+
});
|
71
|
+
}
|
72
|
+
};
|
73
|
+
// Inherit the properties and prototype methods of the passed in S3MP instance object
|
74
|
+
Upload.prototype = o;
|
75
|
+
return new Upload();
|
76
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
// Upload part constructor
|
2
|
+
function UploadPart(blob, key, upload) {
|
3
|
+
var part, xhr;
|
4
|
+
|
5
|
+
part = this;
|
6
|
+
|
7
|
+
this.size = blob.size;
|
8
|
+
this.blob = blob;
|
9
|
+
this.num = key;
|
10
|
+
|
11
|
+
this.xhr = xhr = upload.createXhrRequest();
|
12
|
+
xhr.onload = function() {
|
13
|
+
upload.handler.onPartSuccess(upload, part);
|
14
|
+
};
|
15
|
+
xhr.onerror = function() {
|
16
|
+
upload.handler.onError(upload, part);
|
17
|
+
};
|
18
|
+
xhr.upload.onprogress = _.throttle(function(e) {
|
19
|
+
upload.inprogress[key] = e.loaded;
|
20
|
+
}, 1000);
|
21
|
+
|
22
|
+
};
|
23
|
+
|
24
|
+
UploadPart.prototype.activate = function() {
|
25
|
+
this.xhr.send(this.blob);
|
26
|
+
this.status = "active";
|
27
|
+
};
|
28
|
+
|
29
|
+
UploadPart.prototype.pause = function() {
|
30
|
+
this.xhr.abort();
|
31
|
+
this.status = "paused";
|
32
|
+
};
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module S3Multipart
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
desc "Generates all the necessary setup for integration with the S3 Multipart gem"
|
6
|
+
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
|
9
|
+
def create_migrations
|
10
|
+
copy_file "uploads_table_migration.rb", "db/migrate/#{migration_time}_create_s3_multipart_uploads.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_configuration_files
|
14
|
+
copy_file "aws.yml", "config/aws.yml"
|
15
|
+
copy_file "configuration_initializer.rb", "config/initializers/s3_multipart.rb"
|
16
|
+
route 'mount S3Multipart::Engine => "/s3_multipart"'
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def migration_time
|
22
|
+
Time.now.strftime("%Y%m%d%H%M%S")
|
23
|
+
end
|
24
|
+
|
25
|
+
def model_constant
|
26
|
+
model.split("_").map(&:capitalize).join()
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class <%= model_constant %>Uploader < ApplicationController
|
2
|
+
extend S3Multipart::Uploader::Core
|
3
|
+
|
4
|
+
# Attaches the specified model to the uploader, creating a "has_one"
|
5
|
+
# relationship between the internal upload model and the given model.
|
6
|
+
attach :<%= model %>
|
7
|
+
|
8
|
+
# Takes in a block that will be evaluated when the upload has been
|
9
|
+
# successfully initiated. The block will be passed an instance of
|
10
|
+
# the upload object when the callback is made.
|
11
|
+
#
|
12
|
+
# The following attributes are available on the upload object:
|
13
|
+
# - key: A randomly generated unique key to replace the file
|
14
|
+
# name provided by the client
|
15
|
+
# - upload_id: A hash generated by Amazon to identify the multipart upload
|
16
|
+
# - name: The name of the file (including extensions)
|
17
|
+
# - location: The location of the file on S3. Available only to the
|
18
|
+
# upload object passed into the on_complete callback
|
19
|
+
#
|
20
|
+
on_begin do |upload|
|
21
|
+
# Code to be evaluated when upload begins.
|
22
|
+
end
|
23
|
+
|
24
|
+
# See above comment. Called when the upload has successfully completed
|
25
|
+
on_complete do |upload|
|
26
|
+
# Code to be evaluated when upload completes
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# This migration comes from s3_multipart
|
1
2
|
class CreateS3MultipartUploads < ActiveRecord::Migration
|
2
3
|
def change
|
3
4
|
create_table :s3_multipart_uploads do |t|
|
@@ -5,6 +6,7 @@ class CreateS3MultipartUploads < ActiveRecord::Migration
|
|
5
6
|
t.string :upload_id
|
6
7
|
t.string :key
|
7
8
|
t.string :name
|
9
|
+
t.string :uploader
|
8
10
|
|
9
11
|
t.timestamps
|
10
12
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module S3Multipart
|
4
|
+
class UploaderGenerator < Rails::Generators::Base
|
5
|
+
desc "Generates an uploader for use with the S3 Multipart gem"
|
6
|
+
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
argument :model, :type => :string
|
9
|
+
# class_option :migrations, :type => :boolean, :default => true, :description => "Create migration files"
|
10
|
+
|
11
|
+
def create_uploader
|
12
|
+
empty_directory("app/uploaders")
|
13
|
+
empty_directory("app/uploaders/multipart")
|
14
|
+
template "uploader.rb", "app/uploaders/multipart/#{model}_uploader.rb"
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_migrations
|
18
|
+
# return unless options.migrations?
|
19
|
+
template "add_uploader_column_to_model.rb", "db/migrate/#{migration_time}_add_uploader_to_#{model}.rb"
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def migration_time
|
25
|
+
Time.now.strftime("%Y%m%d%H%M%S")
|
26
|
+
end
|
27
|
+
|
28
|
+
def model_constant
|
29
|
+
model.split("_").map(&:capitalize).join()
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -2,7 +2,8 @@ module S3Multipart
|
|
2
2
|
module ActionViewHelpers
|
3
3
|
module FormHelper
|
4
4
|
def multipart_uploader_form(options = {})
|
5
|
-
|
5
|
+
uploader_digest = S3Multipart::Uploader.serialize(options[:uploader])
|
6
|
+
html = file_field_tag options[:input_name], :accept => options[:types].join(','), :multiple => 'multiple', :data => {:uploader => uploader_digest}
|
6
7
|
html << options[:html].html_safe
|
7
8
|
html << button_tag(:class => options[:button_class]) do
|
8
9
|
content_tag(:span, options[:button_text])
|
@@ -37,13 +37,13 @@ module S3Multipart
|
|
37
37
|
|
38
38
|
private
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
def http
|
41
|
+
Net::HTTP.new("#{Config.instance.bucket_name}.s3.amazonaws.com", 80)
|
42
|
+
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
def request_class
|
45
|
+
Net::HTTP.const_get(method.to_s.capitalize)
|
46
|
+
end
|
47
47
|
|
48
48
|
end
|
49
49
|
end
|
data/lib/s3_multipart/railtie.rb
CHANGED
@@ -8,6 +8,18 @@ if defined?(Rails)
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
+
# Load all of the upload controllers in app/uploaders/multipart
|
12
|
+
initializer "s3_multipart.load_upload_controllers" do
|
13
|
+
begin
|
14
|
+
uploaders = Dir.entries(Rails.root.join('app', 'uploaders', 'multipart').to_s).keep_if {|n| n =~ /[uploader]/}
|
15
|
+
uploaders.each do |uploader|
|
16
|
+
require "#{Rails.root.join('app', 'uploaders', 'multipart')}/#{uploader}"
|
17
|
+
end
|
18
|
+
rescue
|
19
|
+
# Give some sort of error in the console
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
11
23
|
end
|
12
24
|
end
|
13
25
|
end
|