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.
Files changed (127) hide show
  1. data/.gitignore +11 -0
  2. data/Gemfile +2 -2
  3. data/Gemfile.lock +8 -1
  4. data/README.md +150 -42
  5. data/Rakefile +10 -0
  6. data/app/controllers/s3_multipart/application_controller.rb +6 -3
  7. data/app/controllers/s3_multipart/uploads_controller.rb +30 -29
  8. data/app/models/s3_multipart/upload.rb +13 -5
  9. data/grunt.js +44 -0
  10. data/javascripts/footer.js +5 -0
  11. data/javascripts/header.js +14 -0
  12. data/javascripts/libs/underscore.js +1 -0
  13. data/javascripts/s3mp.js +329 -0
  14. data/javascripts/upload.js +76 -0
  15. data/javascripts/uploadpart.js +32 -0
  16. data/lib/generators/s3_multipart/install_generator.rb +30 -0
  17. data/lib/generators/s3_multipart/templates/add_uploader_column_to_model.rb +7 -0
  18. data/lib/generators/s3_multipart/templates/aws.yml +4 -0
  19. data/lib/generators/s3_multipart/templates/configuration_initializer.rb +8 -0
  20. data/lib/generators/s3_multipart/templates/uploader.rb +29 -0
  21. data/{db/migrate/20110727184726_create_s3_multipart_uploads.rb → lib/generators/s3_multipart/templates/uploads_table_migration.rb} +2 -0
  22. data/lib/generators/s3_multipart/uploader_generator.rb +33 -0
  23. data/lib/s3_multipart/action_view_helpers/form_helper.rb +2 -1
  24. data/lib/s3_multipart/config.rb +12 -0
  25. data/lib/s3_multipart/http/net_http.rb +6 -6
  26. data/lib/s3_multipart/railtie.rb +12 -0
  27. data/lib/s3_multipart/transfer_helpers.rb +92 -0
  28. data/lib/s3_multipart/uploader/callbacks.rb +17 -0
  29. data/lib/s3_multipart/uploader/validations.rb +9 -0
  30. data/lib/s3_multipart/uploader.rb +27 -68
  31. data/lib/s3_multipart/version.rb +1 -1
  32. data/lib/s3_multipart.rb +4 -42
  33. data/package.json +9 -0
  34. data/s3_multipart.gemspec +6 -3
  35. data/spec/integration/uploads_controller_spec.rb +2 -1
  36. data/spec/internal/app/assets/javascripts/application.js +148 -63
  37. data/spec/internal/app/assets/stylesheets/application.css.scss +384 -0
  38. data/spec/internal/app/assets/stylesheets/font-awesome.scss +499 -0
  39. data/spec/internal/app/controllers/application_controller.rb +4 -1
  40. data/spec/internal/app/controllers/pages_controller.rb +3 -7
  41. data/spec/internal/app/models/user.rb +4 -0
  42. data/spec/internal/app/models/video.rb +5 -0
  43. data/spec/internal/app/uploaders/multipart/video_uploader.rb +32 -0
  44. data/spec/internal/app/views/pages/upload.html.erb +38 -4
  45. data/spec/internal/config/routes.rb +1 -1
  46. data/spec/internal/db/schema.rb +22 -14
  47. data/spec/internal/public/fonts/FontAwesome.otf +0 -0
  48. data/spec/internal/public/fonts/fontawesome-webfont.eot +0 -0
  49. data/spec/internal/public/fonts/fontawesome-webfont.ttf +0 -0
  50. data/spec/internal/public/fonts/fontawesome-webfont.woff +0 -0
  51. data/spec/internal/tmp/cache/sass/077f66d4d9153cfd737b81ab3f4c5d5858b0db3b/_grid-background.scssc +0 -0
  52. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_appearance.scssc +0 -0
  53. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_background-clip.scssc +0 -0
  54. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_background-origin.scssc +0 -0
  55. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_background-size.scssc +0 -0
  56. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_border-radius.scssc +0 -0
  57. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_box-shadow.scssc +0 -0
  58. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_box-sizing.scssc +0 -0
  59. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_box.scssc +0 -0
  60. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_columns.scssc +0 -0
  61. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_filter.scssc +0 -0
  62. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_font-face.scssc +0 -0
  63. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_hyphenation.scssc +0 -0
  64. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_images.scssc +0 -0
  65. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_inline-block.scssc +0 -0
  66. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_opacity.scssc +0 -0
  67. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_regions.scssc +0 -0
  68. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_shared.scssc +0 -0
  69. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_text-shadow.scssc +0 -0
  70. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_transform.scssc +0 -0
  71. data/spec/internal/tmp/cache/sass/1925334270d3f17f5ea2c4e86092ee0214484038/_transition.scssc +0 -0
  72. data/spec/internal/tmp/cache/sass/351072894b53bf1a2d2af4bd57c177c805cf063e/_contrast.scssc +0 -0
  73. data/spec/internal/tmp/cache/sass/49ecd6d86aba13e15e515be708bbfd8bb67a011a/_css3.scssc +0 -0
  74. data/spec/internal/tmp/cache/sass/49ecd6d86aba13e15e515be708bbfd8bb67a011a/_support.scssc +0 -0
  75. data/spec/internal/tmp/cache/sass/49ecd6d86aba13e15e515be708bbfd8bb67a011a/_typography.scssc +0 -0
  76. data/spec/internal/tmp/cache/sass/49ecd6d86aba13e15e515be708bbfd8bb67a011a/_utilities.scssc +0 -0
  77. data/spec/internal/tmp/cache/sass/633bc5f017268b81a60f921876c980c4adcebb74/_base.scssc +0 -0
  78. data/spec/internal/tmp/cache/sass/633bc5f017268b81a60f921876c980c4adcebb74/_sprite-img.scssc +0 -0
  79. data/spec/internal/tmp/cache/sass/68dbce8ac3037e0cd797c9b5c6aa131096361144/_utilities.scssc +0 -0
  80. data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_clearfix.scssc +0 -0
  81. data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_float.scssc +0 -0
  82. data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_hacks.scssc +0 -0
  83. data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_min.scssc +0 -0
  84. data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_reset.scssc +0 -0
  85. data/spec/internal/tmp/cache/sass/7722315031801f2a146090aa73380dfd28539145/_tag-cloud.scssc +0 -0
  86. data/spec/internal/tmp/cache/sass/775792c99f07f64e98296a8dc8c9d2c74a28fd8a/_reset.scssc +0 -0
  87. data/spec/internal/tmp/cache/sass/8b4f1545a124f2498699911561d1ec6302c9de56/_ellipsis.scssc +0 -0
  88. data/spec/internal/tmp/cache/sass/8b4f1545a124f2498699911561d1ec6302c9de56/_force-wrap.scssc +0 -0
  89. data/spec/internal/tmp/cache/sass/8b4f1545a124f2498699911561d1ec6302c9de56/_nowrap.scssc +0 -0
  90. data/spec/internal/tmp/cache/sass/8b4f1545a124f2498699911561d1ec6302c9de56/_replacement.scssc +0 -0
  91. data/spec/internal/tmp/cache/sass/abd68615441476dbd8e9fdc1606210305b3177fd/_bullets.scssc +0 -0
  92. data/spec/internal/tmp/cache/sass/abd68615441476dbd8e9fdc1606210305b3177fd/_horizontal-list.scssc +0 -0
  93. data/spec/internal/tmp/cache/sass/abd68615441476dbd8e9fdc1606210305b3177fd/_inline-block-list.scssc +0 -0
  94. data/spec/internal/tmp/cache/sass/abd68615441476dbd8e9fdc1606210305b3177fd/_inline-list.scssc +0 -0
  95. data/spec/internal/tmp/cache/sass/b1bb3e2fbcb5c6a71ea2273c6dcb7c8967d2a058/_alternating-rows-and-columns.scssc +0 -0
  96. data/spec/internal/tmp/cache/sass/b1bb3e2fbcb5c6a71ea2273c6dcb7c8967d2a058/_borders.scssc +0 -0
  97. data/spec/internal/tmp/cache/sass/b1bb3e2fbcb5c6a71ea2273c6dcb7c8967d2a058/_scaffolding.scssc +0 -0
  98. data/spec/internal/tmp/cache/sass/bc7f11d6e8f07a22f545deb35760cfc7830f3369/application.css.scssc +0 -0
  99. data/spec/internal/tmp/cache/sass/bc7f11d6e8f07a22f545deb35760cfc7830f3369/font-awesome.scssc +0 -0
  100. data/spec/internal/tmp/cache/sass/c9342b40bf8bc7bf64ce587860fb065cbf6d2ca4/_color.scssc +0 -0
  101. data/spec/internal/tmp/cache/sass/c9342b40bf8bc7bf64ce587860fb065cbf6d2ca4/_general.scssc +0 -0
  102. data/spec/internal/tmp/cache/sass/c9342b40bf8bc7bf64ce587860fb065cbf6d2ca4/_sprites.scssc +0 -0
  103. data/spec/internal/tmp/cache/sass/c9342b40bf8bc7bf64ce587860fb065cbf6d2ca4/_tables.scssc +0 -0
  104. data/spec/internal/tmp/cache/sass/d1da036a47062a9f69ce0327962c00aa77d2ea44/_hover-link.scssc +0 -0
  105. data/spec/internal/tmp/cache/sass/d1da036a47062a9f69ce0327962c00aa77d2ea44/_link-colors.scssc +0 -0
  106. data/spec/internal/tmp/cache/sass/d1da036a47062a9f69ce0327962c00aa77d2ea44/_unstyled-link.scssc +0 -0
  107. data/spec/internal/tmp/cache/sass/d559b7bf0c5e0c0d23f2f34bfc21b4817fb38463/_links.scssc +0 -0
  108. data/spec/internal/tmp/cache/sass/d559b7bf0c5e0c0d23f2f34bfc21b4817fb38463/_lists.scssc +0 -0
  109. data/spec/internal/tmp/cache/sass/d559b7bf0c5e0c0d23f2f34bfc21b4817fb38463/_text.scssc +0 -0
  110. data/spec/internal/tmp/cache/sass/d559b7bf0c5e0c0d23f2f34bfc21b4817fb38463/_vertical_rhythm.scssc +0 -0
  111. data/spec/internal/tmp/cache/sass/e2e5b9bb57a9d1b018b9f546bc8b16f6b2a627a9/_compass.scssc +0 -0
  112. data/spec/internal/tmp/cache/sass/e654955d4502954a409bf21071c0d98eb4ac2e9f/_utilities.scssc +0 -0
  113. data/spec/javascripts/UploadSpec.js +75 -0
  114. data/spec/javascripts/helpers/SpecHelper.js +9 -0
  115. data/spec/javascripts/support/jasmine.yml +84 -0
  116. data/spec/unit/upload_controller_spec.rb +31 -0
  117. data/spec/{requests/uploader_spec.rb → unit/upload_spec.rb} +9 -10
  118. data/spec/unit/uploader_module_spec.rb +31 -0
  119. data/vendor/assets/javascripts/s3_multipart/lib.js +456 -0
  120. data/vendor/assets/javascripts/s3_multipart/lib.min.js +1 -0
  121. metadata +103 -12
  122. data/.rspec +0 -1
  123. data/lib/s3_multipart/uploader/config.rb +0 -15
  124. data/spec/internal/app/assets/stylesheets/application.css +0 -0
  125. data/spec/internal/db/combustion_test.sqlite3 +0 -0
  126. data/vendor/assets/javascripts/s3_multipart/index.js +0 -1
  127. data/vendor/assets/javascripts/s3_multipart/s3_multipart.js +0 -478
@@ -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,7 @@
1
+ class AddUploaderTo<%= model_constant %> < ActiveRecord::Migration
2
+ def change
3
+ change_table :<%= model %> do |t|
4
+ t.string :uploader
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ development:
2
+ access_key_id: ""
3
+ secret_access_key: ""
4
+ bucket: ""
@@ -0,0 +1,8 @@
1
+ AWS_Config = YAML.load_file("config/aws.yml")[Rails.env]
2
+
3
+ S3Multipart.configure do |config|
4
+ config.bucket_name = AWS_Config['bucket']
5
+ config.s3_access_key = AWS_Config['access_key_id']
6
+ config.s3_secret_key = AWS_Config['secret_access_key']
7
+ end
8
+
@@ -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
- html = file_field_tag options[:input_name], :accept => options[:types].join(',')
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])
@@ -0,0 +1,12 @@
1
+ require 'singleton'
2
+
3
+ module S3Multipart
4
+ class Config
5
+ include Singleton
6
+ attr_accessor :s3_access_key, :s3_secret_key, :bucket_name
7
+
8
+ def self.configure(block)
9
+ block.call(self.instance)
10
+ end
11
+ end
12
+ end
@@ -37,13 +37,13 @@ module S3Multipart
37
37
 
38
38
  private
39
39
 
40
- def http
41
- Net::HTTP.new('bitcast-bucket.s3.amazonaws.com', 80)
42
- end
40
+ def http
41
+ Net::HTTP.new("#{Config.instance.bucket_name}.s3.amazonaws.com", 80)
42
+ end
43
43
 
44
- def request_class
45
- Net::HTTP.const_get(method.to_s.capitalize)
46
- end
44
+ def request_class
45
+ Net::HTTP.const_get(method.to_s.capitalize)
46
+ end
47
47
 
48
48
  end
49
49
  end
@@ -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