jbasdf-uploader 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +323 -0
  3. data/Rakefile +67 -0
  4. data/TODO +3 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/uploader/uploads_controller.rb +126 -0
  7. data/app/helpers/uploader_helper.rb +17 -0
  8. data/app/views/uploads/_swf_javascript.html.erb +63 -0
  9. data/app/views/uploads/_swf_upload.html.erb +32 -0
  10. data/config/uploader_routes.rb +3 -0
  11. data/db/migrate/20090517040220_create_uploads.rb +38 -0
  12. data/lib/active_record/acts/uploader_upload.rb +234 -0
  13. data/lib/daemons/amazonaws.rb +36 -0
  14. data/lib/uploader.rb +24 -0
  15. data/lib/uploader/exceptions.rb +5 -0
  16. data/lib/uploader/initialize_routes.rb +8 -0
  17. data/lib/uploader/middleware/flash_session_cookie_middleware.rb +18 -0
  18. data/lib/uploader/mime_type_groups.rb +18 -0
  19. data/lib/uploader/tasks.rb +42 -0
  20. data/locales/ar.yml +17 -0
  21. data/locales/bg.yml +17 -0
  22. data/locales/ca.yml +17 -0
  23. data/locales/cs.yml +17 -0
  24. data/locales/da.yml +17 -0
  25. data/locales/de.yml +17 -0
  26. data/locales/el.yml +17 -0
  27. data/locales/en.yml +16 -0
  28. data/locales/es.yml +17 -0
  29. data/locales/fr.yml +17 -0
  30. data/locales/it.yml +17 -0
  31. data/locales/iw.yml +17 -0
  32. data/locales/ja.yml +17 -0
  33. data/locales/ko.yml +17 -0
  34. data/locales/lt.yml +17 -0
  35. data/locales/lv.yml +17 -0
  36. data/locales/nl.yml +17 -0
  37. data/locales/no.yml +18 -0
  38. data/locales/pl.yml +17 -0
  39. data/locales/pt.yml +17 -0
  40. data/locales/ro.yml +17 -0
  41. data/locales/ru.yml +17 -0
  42. data/locales/sk.yml +17 -0
  43. data/locales/sl.yml +17 -0
  44. data/locales/sr.yml +17 -0
  45. data/locales/sv.yml +17 -0
  46. data/locales/tl.yml +17 -0
  47. data/locales/uk.yml +17 -0
  48. data/locales/vi.yml +17 -0
  49. data/locales/zh-CN.yml +17 -0
  50. data/locales/zh-TW.yml +17 -0
  51. data/locales/zh.yml +17 -0
  52. data/public/images/SWFUploadButton.png +0 -0
  53. data/public/images/file_icons/excel.gif +0 -0
  54. data/public/images/file_icons/file.gif +0 -0
  55. data/public/images/file_icons/file.png +0 -0
  56. data/public/images/file_icons/file_aac.gif +0 -0
  57. data/public/images/file_icons/file_ai.gif +0 -0
  58. data/public/images/file_icons/file_avi.gif +0 -0
  59. data/public/images/file_icons/file_bin.gif +0 -0
  60. data/public/images/file_icons/file_bmp.gif +0 -0
  61. data/public/images/file_icons/file_cue.gif +0 -0
  62. data/public/images/file_icons/file_divx.gif +0 -0
  63. data/public/images/file_icons/file_doc.gif +0 -0
  64. data/public/images/file_icons/file_eps.gif +0 -0
  65. data/public/images/file_icons/file_flac.gif +0 -0
  66. data/public/images/file_icons/file_flv.gif +0 -0
  67. data/public/images/file_icons/file_gif.gif +0 -0
  68. data/public/images/file_icons/file_html.gif +0 -0
  69. data/public/images/file_icons/file_ical.gif +0 -0
  70. data/public/images/file_icons/file_indd.gif +0 -0
  71. data/public/images/file_icons/file_inx.gif +0 -0
  72. data/public/images/file_icons/file_iso.gif +0 -0
  73. data/public/images/file_icons/file_jpg.gif +0 -0
  74. data/public/images/file_icons/file_mov.gif +0 -0
  75. data/public/images/file_icons/file_mp3.gif +0 -0
  76. data/public/images/file_icons/file_mpg.gif +0 -0
  77. data/public/images/file_icons/file_pdf.gif +0 -0
  78. data/public/images/file_icons/file_php.gif +0 -0
  79. data/public/images/file_icons/file_png.gif +0 -0
  80. data/public/images/file_icons/file_pps.gif +0 -0
  81. data/public/images/file_icons/file_ppt.gif +0 -0
  82. data/public/images/file_icons/file_psd.gif +0 -0
  83. data/public/images/file_icons/file_qxd.gif +0 -0
  84. data/public/images/file_icons/file_qxp.gif +0 -0
  85. data/public/images/file_icons/file_raw.gif +0 -0
  86. data/public/images/file_icons/file_rtf.gif +0 -0
  87. data/public/images/file_icons/file_svg.gif +0 -0
  88. data/public/images/file_icons/file_tif.gif +0 -0
  89. data/public/images/file_icons/file_txt.gif +0 -0
  90. data/public/images/file_icons/file_vcf.gif +0 -0
  91. data/public/images/file_icons/file_wav.gif +0 -0
  92. data/public/images/file_icons/file_wma.gif +0 -0
  93. data/public/images/file_icons/file_xls.gif +0 -0
  94. data/public/images/file_icons/file_xml.gif +0 -0
  95. data/public/images/file_icons/mp3.gif +0 -0
  96. data/public/images/file_icons/pdf.gif +0 -0
  97. data/public/images/file_icons/pdf.png +0 -0
  98. data/public/images/file_icons/text.gif +0 -0
  99. data/public/images/file_icons/text.png +0 -0
  100. data/public/images/file_icons/word.gif +0 -0
  101. data/public/javascripts/swfupload/fileprogress.js +151 -0
  102. data/public/javascripts/swfupload/handlers.js +206 -0
  103. data/public/javascripts/swfupload/swfupload.cookies.js +53 -0
  104. data/public/javascripts/swfupload/swfupload.js +945 -0
  105. data/public/javascripts/swfupload/swfupload.queue.js +77 -0
  106. data/public/javascripts/swfupload/swfupload.swfobject.js +110 -0
  107. data/public/stylesheets/swfupload.css +26 -0
  108. data/public/swf/swfupload.swf +0 -0
  109. data/rails/init.rb +10 -0
  110. data/rdoc/classes/ActionController.html +107 -0
  111. data/rdoc/classes/ActionController/Routing.html +107 -0
  112. data/rdoc/classes/ActionController/Routing/RouteSet.html +148 -0
  113. data/rdoc/classes/ActiveRecord.html +105 -0
  114. data/rdoc/classes/ActiveRecord/Acts/UploaderUpload/ClassMethods.html +186 -0
  115. data/rdoc/classes/ActiveRecord/Acts/UploaderUpload/InstanceMethods.html +750 -0
  116. data/rdoc/classes/ActiveRecord/Acts/UploaderUpload/SingletonMethods.html +111 -0
  117. data/rdoc/classes/Uploader.html +126 -0
  118. data/rdoc/classes/Uploader/Exceptions.html +111 -0
  119. data/rdoc/classes/Uploader/Exceptions/MissingTemplateError.html +111 -0
  120. data/rdoc/classes/Uploader/FlashSessionCookieMiddleware.html +177 -0
  121. data/rdoc/classes/Uploader/MimeTypeGroups.html +143 -0
  122. data/rdoc/classes/Uploader/Tasks.html +146 -0
  123. data/rdoc/created.rid +1 -0
  124. data/rdoc/files/README_rdoc.html +501 -0
  125. data/rdoc/files/lib/active_record/acts/uploader_upload_rb.html +101 -0
  126. data/rdoc/files/lib/uploader/exceptions_rb.html +101 -0
  127. data/rdoc/files/lib/uploader/initialize_routes_rb.html +101 -0
  128. data/rdoc/files/lib/uploader/middleware/flash_session_cookie_middleware_rb.html +108 -0
  129. data/rdoc/files/lib/uploader/mime_type_groups_rb.html +101 -0
  130. data/rdoc/files/lib/uploader/tasks_rb.html +110 -0
  131. data/rdoc/files/lib/uploader_rb.html +112 -0
  132. data/rdoc/fr_class_index.html +39 -0
  133. data/rdoc/fr_file_index.html +35 -0
  134. data/rdoc/fr_method_index.html +54 -0
  135. data/rdoc/index.html +24 -0
  136. data/rdoc/rdoc-style.css +208 -0
  137. data/tasks/rails.rake +2 -0
  138. data/test/test_helper.rb +3 -0
  139. data/test/unit/upload_test.rb +5 -0
  140. data/uninstall.rb +0 -0
  141. data/uploader.gemspec +182 -0
  142. metadata +204 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Justin Ball
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,323 @@
1
+ = Uploader
2
+
3
+ Uploader makes it easy to integrate multiple file uploads into your application using SWFUpload
4
+
5
+
6
+ == Installation
7
+
8
+ === Install Dependancies:
9
+ sudo gem install mime-types
10
+
11
+
12
+ === Install the gem:
13
+ sudo gem install uploader
14
+
15
+
16
+ === Add the gem to environment.rb
17
+ config.gem 'uploader'
18
+
19
+
20
+ === Install jQuery
21
+ uploader uses jQuery. You'll need to include it in your application. Download it here:
22
+ http://jquery.com/
23
+
24
+ Then include it in your layout:
25
+ <%= javascript_include_tag 'jquery/jquery.js' %>
26
+
27
+ Another option is to use jRails
28
+ http://ennerchi.com/projects/jrails
29
+
30
+ === Create a model for uploads.
31
+ We recommend creating a model called upload.rb. acts_as_uploader accepts all valid options for paperclip via :has_attached_file => {}
32
+
33
+ class Upload < ActiveRecord::Base
34
+
35
+ acts_as_uploader :enable_s3 => false,
36
+ :has_attached_file => {
37
+ :url => "/system/:attachment/:id_partition/:style/:basename.:extension",
38
+ :path => ":rails_root/public/system/:attachment/:id_partition/:style/:basename.:extension",
39
+ :styles => { :icon => "30x30!",
40
+ :thumb => "100>",
41
+ :small => "150>",
42
+ :medium => "300>",
43
+ :large => "660>" },
44
+ :default_url => "/images/profile_default.jpg",
45
+ :storage => :s3,
46
+ :s3_credentials => AMAZON_S3_CREDENTIALS,
47
+ :bucket => "assets.example.com",
48
+ :s3_host_alias => "assets.example.com",
49
+ :convert_options => {
50
+ :all => '-quality 80'
51
+ }
52
+ },
53
+ :s3_path => ':id_partition/:style/:basename.:extension'
54
+
55
+ # only allow images:
56
+ # validates_attachment_content_type :file, :content_type => ['image/jpeg', 'image/pjpeg', 'image/jpg']
57
+
58
+ # The following method is implemented in 'acts_as_uploader'. This is the method destroy will check to see if
59
+ # the user has permission to delete the object. Add additional logic as needed or if the existing logic
60
+ # looks fine then feel free to delete this comment and the can_edit? method.
61
+ def can_edit?(check_user)
62
+ return false if user.blank?
63
+ check_user == self.user
64
+ end
65
+
66
+ end
67
+
68
+
69
+ === Add multiple file uploads to one of your models
70
+
71
+ Your uploads will need a parent object to attach to. For example, a user might have many files:
72
+
73
+ class User < ActiveRecord::Base
74
+ has_many :uploads, :as => :uploadable, :order => 'created_at desc', :dependent => :destroy
75
+
76
+ def can_upload?(check_user)
77
+ self == check_user
78
+ end
79
+ end
80
+
81
+ or a photo album might have many photos
82
+
83
+ class PhotoAlbum < ActiveRecord::Base
84
+ has_many :photos, :as => :uploadable, :order => 'created_at desc', :dependent => :destroy
85
+
86
+ def can_upload?(check_user)
87
+ self.editors.include?(check_user)
88
+ end
89
+ end
90
+
91
+ Note that in both examples there is an implementation of 'can_upload?'. This method must be
92
+ included in any parent object and will control who has permission to upload files.
93
+
94
+ === The application controller
95
+ Be sure you have turned on protect from forgery. This is required for uploader to get the appropriate tokens
96
+ from your Rails application. It is also a good idea and is the default in new Rails applications.
97
+
98
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
99
+
100
+ === The uploads controller
101
+ You can modify the upload controller behavior by inheriting from the uploader controller. For example, you might want to
102
+ require that users be logged in to upload a file. There are a number of methods in the uploads controller that contain
103
+ default functionality that you may consider overriding.
104
+
105
+ Be sure to modify your routes file. Add the following line to ensure that your application uses the new uploads
106
+ controller instead of directly using the one inside the gem:
107
+
108
+ map.resources :uploads, :collection => { :swfupload => :post }
109
+
110
+
111
+ class UploadsController < Uploader::UploadsController
112
+
113
+ prepend_before_filter :login_required
114
+
115
+ protected
116
+
117
+ # The default 'get_upload_text' method throws an exception. You must override this method in your controller. It
118
+ # is used by the swf upload call to generate the html to be returned to the client.
119
+ # Here's an example:
120
+ def get_upload_text(upload)
121
+ render_to_string( :partial => 'uploads/upload_row', :object => upload, :locals => { :parent => @parent } )
122
+ end
123
+
124
+ # The existing method will handle most cases but you might choose a different message or a different redirect:
125
+ def permission_denied
126
+ message = t("uploader.permission_denied")
127
+ respond_to do |format|
128
+ format.html do
129
+ flash[:notice] = message
130
+ redirect_to get_redirect
131
+ end
132
+ format.js { render :text => message }
133
+ format.json { render :json => { :success => false, :message => message } }
134
+ end
135
+ end
136
+
137
+ # Simply attempts to redirect to the parent object. You might want to build something more sophisticated that
138
+ # redirect to different areas of you site depending on the type of object that was uploaded or on based on the parent.
139
+ # source can be :destroy_success, :create_success, :create_failure, :permission_denied
140
+ def get_redirect
141
+ @parent
142
+ end
143
+
144
+ # The default action is to call 'can_upload?' on the parent object. Be sure to implement 'can_upload?(check_user) on
145
+ # your parent objects
146
+ def has_permission_to_upload(user, upload_parent)
147
+ upload_parent.can_upload?(user)
148
+ end
149
+
150
+
151
+ # By default the controller will use a model named 'Upload' to do a destroy. If you want to use a different model
152
+ # you'll need to override 'set_upload_for_destroy in your controller to find the object using a different object.
153
+ # For example:
154
+ def set_upload_for_destroy
155
+ @upload = Photo.find(params[:id])
156
+ end
157
+
158
+ end
159
+
160
+ === Configure your views.
161
+ You'll need something like this in your layout so that uploader can add in the required css and javascript files.
162
+
163
+ <%= yield :head -%>
164
+
165
+ Then to add an upload form:
166
+ <%= upload_form(parent_object) %>
167
+ parent_object should be the object which owns the uploads. ie a user, photo_album, etc.
168
+
169
+ === Rake Tasks
170
+
171
+ Add the rake tasks for uploader to your project. You will need to add the following to your applications's Rakefile
172
+
173
+ require 'uploader'
174
+ require 'uploader/tasks'
175
+
176
+ Then run:
177
+ rake uploader:sync
178
+
179
+ Last run:
180
+ rake db:migrate
181
+ This will create an uploads table for you. If you selected a different name for your model you will need to modify the migration accordingly.
182
+ == WARNING
183
+ The migration will drop any existing 'uploads' table you have in place
184
+
185
+
186
+ That will copy all the required javascript and asset files into your project
187
+
188
+
189
+ == Amazon s3
190
+
191
+
192
+ If you'd like to store your uploads on Amazon's S3 service there are a few extra steps involved. See the example file above
193
+ to view the options in context.
194
+
195
+ === Turn on s3
196
+ Set the enable_s3 option to true in acts_as_uploader
197
+ :enable_s3 => true
198
+
199
+ Pass your s3 credentials into acts_as_uploader
200
+ :has_attached_file => { :s3_credentials => File.join(RAILS_ROOT, 'config', 's3.yml') }
201
+
202
+ === Setup your credentials
203
+ Create a file named s3.yml in your configuration directory and add the following lines:
204
+
205
+ access_key_id: PUT YOUR KEY HERE
206
+ secret_access_key: PUT YOUR SECRET ACCESS KEY HERE
207
+
208
+
209
+ === Turn on the Daemon process
210
+ There are a number of timing issues that you will run into if you attempt to upload files directly to s3. To overcome that
211
+ problem uploader includes a daemon process which will send the files to Amazon asynchronously. Note that the uploader
212
+ will leave your local copy in place.
213
+
214
+ Add the daemons gem and plugin:
215
+ sudo gem install daemons
216
+
217
+ Then inside your Rails project:
218
+ script/plugin install git://github.com/dougal/daemon_generator.git
219
+ script/generate daemon amazonaws
220
+
221
+ RAILS_ENV=development lib/daemons/mailer_ctl start
222
+
223
+ Learn more about the custom daemon gem with Ryan Bates screencast:
224
+ http://railscasts.com/episodes/129-custom-daemon
225
+
226
+
227
+ == Use Rake to send files to s3
228
+ uploader includes a task capable of sending files to s3 but it makes an assumption that the model you are interacting with
229
+ is named 'Upload'.
230
+
231
+ rake uploader:upload_to_s3
232
+
233
+ If you want to use a different model or several models just add a rake task to your project:
234
+
235
+ desc 'Send all uploads to S3. (Will only send uploads from a model named Upload)'
236
+ task :upload_to_s3 do
237
+
238
+ uploads = Upload.pending_s3_migration
239
+ uploads.each do |upload|
240
+ upload.remote = upload.local
241
+ upload.save!
242
+ end
243
+
244
+ photos = Photo.pending_s3_migration
245
+ photos.each do |photo|
246
+ photo.remote = photo.local
247
+ photo.save!
248
+ end
249
+
250
+ end
251
+
252
+ == Setup Domains
253
+ If you use Amazon's S3 service you can setup a cname to clean up your urls. Configure your s3 bucket as above:
254
+
255
+ :bucket => "assets.example.com"
256
+ :s3_host_alias => "assets.example.com"
257
+
258
+ Your assets will be available at assets.example.com.s3.amazon.com. You can then create a CNAME in your DNS entries
259
+ to point "assets.example.com" to "assets.example.com.s3.amazon.com". Your assets will then appear to be
260
+ be served from assets.example.com even though they are loaded from Amazon.
261
+
262
+
263
+ == Other Stuff
264
+
265
+ If you'd like to add an ajax delete to your uploads page this code might come in handy.
266
+
267
+ Say you have chosen to display your upload in a table. Your code might look like the following. Note that there are a number of assumptions made in this code. Modify it to suite your needs.
268
+
269
+ <tr id="<%= upload_row.dom_id %>" class="delete-container <%= cycle('odd', 'even') %>" <%=style-%> >
270
+ <td><div class="file-icon"><%= image_tag upload_row.icon -%></div></td>
271
+ <td><a href="<%=upload_row.file.url%>"><%= truncate(sanitize(upload_row.file_name), 100) %></a></td>
272
+ <td><%= upload_row.created_at.to_s(:long) -%></td>
273
+ <td>
274
+ <% if parent.can_edit?(current_user) -%>
275
+ <% form_for(:upload, :url => upload_path(upload_row.id), :html => { :class => "delete-form", :method => :delete} ) do |f| -%>
276
+ <%= image_submit_tag '/images/icons/delete.png', {:id => 'submit-comment', :title => t('general.delete_file'), :class => 'submit-delete', :width => '12', :height => '12', :alt => t('general.delete_file') } %>
277
+ <% end -%>
278
+ <% if !style.empty? -%>
279
+ <script type="text/javascript" language="JavaScript">
280
+ jQuery("#<%= upload_row.dom_id %>").fadeIn("slow");
281
+ </script>
282
+ <% end -%>
283
+ <% end -%>
284
+ </td>
285
+ </tr>
286
+
287
+
288
+ I put the following in my main upload view
289
+ <% content_for :javascript do -%>
290
+
291
+ setup_submit_delete();
292
+
293
+ function upload_completed_callback(data){
294
+ jQuery('#upload-list').prepend(data);
295
+ setup_submit_delete();
296
+ }
297
+ <% end -%>
298
+
299
+
300
+
301
+ The following jQuery code will do an ajax delete for you
302
+
303
+ function setup_submit_delete(){
304
+ jQuery(".submit-delete").click(function() {
305
+ // if(!confirm("Are you sure?")){
306
+ // return false;
307
+ // }
308
+ jQuery(this).parents('.delete-container').fadeOut();
309
+ var form = jQuery(this).parents('form');
310
+ jQuery.post(form.attr('action') + '.json', form.serialize(),
311
+ function(data){
312
+ var json = eval('(' + data + ')');
313
+ if(!json.success){
314
+ jQuery.jGrowl.info(json.message);
315
+ }
316
+ });
317
+ return false;
318
+ });
319
+ }
320
+
321
+
322
+
323
+ Copyright (c) 2009 Justin Ball, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,67 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the uploader plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the uploader plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Uploader'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README.rdoc')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gemspec|
28
+ gemspec.name = "uploader"
29
+ gemspec.summary = "SWFUpload + Paperclip wrapped in an engine with love."
30
+ gemspec.email = "justinball@gmail.com"
31
+ gemspec.homepage = "http://github.com/jbasdf/uploader"
32
+ gemspec.description = "Uploader gem that makes it simple add multiple file uploads to your Rails project using SWFUpload and Paperclip"
33
+ gemspec.authors = ["Justin Ball", "David South"]
34
+ #gemspec.files.include %w( tasks/rails.rake lib/uploader/*.rb lib/uploader/middleware/*.rb db/migrate/*.rb public/images/file_icons/* app/views/uploads/* )
35
+ gemspec.rubyforge_project = 'uploader'
36
+ gemspec.add_dependency "mime-types"
37
+ end
38
+ rescue LoadError
39
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
40
+ end
41
+
42
+ # rubyforge tasks
43
+ begin
44
+ require 'rake/contrib/sshpublisher'
45
+ namespace :rubyforge do
46
+
47
+ desc "Release gem and RDoc documentation to RubyForge"
48
+ task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
49
+
50
+ namespace :release do
51
+ desc "Publish RDoc to RubyForge."
52
+ task :docs => [:rdoc] do
53
+ config = YAML.load(
54
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
55
+ )
56
+
57
+ host = "#{config['username']}@rubyforge.org"
58
+ remote_dir = "/var/www/gforge-projects/uploader/"
59
+ local_dir = 'rdoc'
60
+
61
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
62
+ end
63
+ end
64
+ end
65
+ rescue LoadError
66
+ puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
67
+ end
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+ Figure out how to localize javascript files
2
+ Write the tests
3
+ Match up icons with mime types in uploader_upload.rb
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.8
@@ -0,0 +1,126 @@
1
+ class Uploader::UploadsController < ApplicationController
2
+ unloadable
3
+
4
+ before_filter :get_parent, :only => [:create, :swfupload]
5
+ before_filter :set_upload_for_destroy, :only => [:destroy]
6
+
7
+ def create
8
+ # Standard, one-at-a-time, upload action
9
+ @upload = @parent.uploads.build(params[:upload])
10
+ @upload.creator = get_creator
11
+ @upload.save!
12
+ message = t('uploader.successful_upload')
13
+ upload_json = basic_uploads_json(@upload)
14
+
15
+ respond_to do |format|
16
+ format.html do
17
+ flash[:notice] = message
18
+ redirect_to get_redirect(:create_success)
19
+ end
20
+ format.js { render :text => get_upload_text(@upload) }
21
+ format.json { render :json => upload_json }
22
+ end
23
+ rescue => ex
24
+ message = t('uploader.standard_file_upload_error', :error => ex)
25
+ respond_to do |format|
26
+ format.html do
27
+ flash[:notice] = message
28
+ redirect_to get_redirect(:create_failure)
29
+ end
30
+ format.js { render :text => message }
31
+ format.json { render :json => { :success => false, :message => message } }
32
+ end
33
+ end
34
+
35
+ def swfupload
36
+ @upload = @parent.uploads.build
37
+ @upload.is_public = true if params[:is_public] == true
38
+ @upload.creator = get_creator
39
+ @upload.swfupload_local = params[:filedata]
40
+ @upload.save!
41
+ @parent.uploads << @upload
42
+ respond_to do |format|
43
+ format.js { render :text => get_upload_text(@upload) }
44
+ format.json { render :json => basic_uploads_json(@upload) }
45
+ end
46
+ rescue => ex
47
+ message = t('uploader.standard_file_upload_error', :error => ex)
48
+ respond_to do |format|
49
+ format.js { render :text => message }
50
+ format.json { render :json => { :success => false, :message => message } }
51
+ end
52
+ end
53
+
54
+ def destroy
55
+ @parent = @upload.uploadable # set this for redirect
56
+ if @upload.can_edit?(current_user)
57
+ @upload.destroy
58
+ message = t('uploader.file_deleted')
59
+ success = true
60
+ else
61
+ message = t("uploader.file_delete_permission_denied")
62
+ success = false
63
+ end
64
+ respond_to do |format|
65
+ format.html do
66
+ flash[:notice] = message
67
+ redirect_to get_redirect(:destroy_success)
68
+ end
69
+ format.js { render :text => message }
70
+ format.json { render :json => { :success => success, :message => message } }
71
+ end
72
+ end
73
+
74
+ protected
75
+
76
+ def get_upload_text(upload)
77
+ raise 'implement get_upload_text in your controller'
78
+ end
79
+
80
+ def set_upload_for_destroy
81
+ @upload = Upload.find(params[:id])
82
+ end
83
+
84
+ def permission_denied
85
+ message = t("uploader.permission_denied")
86
+ respond_to do |format|
87
+ format.html do
88
+ flash[:notice] = message
89
+ redirect_to get_redirect(:permission_denied)
90
+ end
91
+ format.js { render :text => message }
92
+ format.json { render :json => { :success => false, :message => message } }
93
+ end
94
+ end
95
+
96
+ # override this method in your controller to set the redirect file upload completion
97
+ # source can be :destroy_success, :create_success, :create_failure, :permission_denied
98
+ def get_redirect(source)
99
+ @parent
100
+ end
101
+
102
+ def get_parent
103
+ if !params[:parent_type] || !params[:parent_id]
104
+ raise t('uploader.missing_parent_id_error')
105
+ return
106
+ end
107
+ @klass = params[:parent_type].to_s.capitalize.constantize
108
+ @parent = @klass.find(params[:parent_id])
109
+ unless has_permission_to_upload(current_user, @parent)
110
+ permission_denied
111
+ end
112
+ end
113
+
114
+ def get_creator
115
+ current_user
116
+ end
117
+
118
+ def has_permission_to_upload(user, upload_parent)
119
+ upload_parent.can_upload?(user)
120
+ end
121
+
122
+ def basic_uploads_json(upload)
123
+ upload.to_json(:only => [:id, :file_name], :methods => [:icon])
124
+ end
125
+
126
+ end