effective_assets 0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +341 -72
  4. data/app/assets/javascripts/effective/snippets/effective_asset.js.coffee +53 -0
  5. data/app/assets/javascripts/effective_assets.js +4 -0
  6. data/app/assets/javascripts/effective_assets/asset_box.js.coffee +36 -0
  7. data/app/assets/javascripts/effective_assets/asset_box_dialog.js.coffee +49 -0
  8. data/app/assets/javascripts/effective_assets/asset_box_drag_and_drop.js.coffee +2 -0
  9. data/app/assets/javascripts/effective_assets/asset_box_filtering.js.coffee +16 -0
  10. data/app/assets/javascripts/effective_assets/asset_box_sorting.js.coffee +8 -0
  11. data/app/assets/javascripts/effective_assets/jquery_ui_sortable.js +813 -585
  12. data/app/assets/javascripts/effective_assets/s3_uploader.js.coffee +268 -0
  13. data/app/assets/javascripts/effective_assets_iframe.js.coffee +28 -0
  14. data/app/assets/stylesheets/active_admin/effective_assets.css.scss +57 -0
  15. data/app/assets/stylesheets/effective_assets/_asset_box_input.scss +54 -175
  16. data/app/assets/stylesheets/effective_assets/_iframe_bootstrap.scss +1714 -0
  17. data/app/assets/stylesheets/effective_assets/_input_bootstrap.scss +508 -0
  18. data/app/assets/stylesheets/effective_assets_iframe.css.scss +23 -0
  19. data/app/controllers/effective/assets_controller.rb +41 -0
  20. data/app/controllers/effective/s3_uploads_controller.rb +58 -74
  21. data/app/helpers/effective_assets_helper.rb +30 -14
  22. data/app/helpers/effective_assets_s3_helper.rb +69 -0
  23. data/app/models/concerns/acts_as_asset_box.rb +75 -11
  24. data/app/models/effective/access_denied.rb +17 -0
  25. data/app/models/effective/asset.rb +130 -93
  26. data/app/models/effective/attachment.rb +2 -7
  27. data/app/models/effective/delayed_job.rb +33 -78
  28. data/app/models/effective/snippets/effective_asset.rb +19 -0
  29. data/app/models/effective/user_uploads.rb +19 -0
  30. data/app/models/inputs/asset_box.rb +154 -0
  31. data/app/models/inputs/asset_box_form_input.rb +7 -0
  32. data/app/models/inputs/asset_box_formtastic_input.rb +9 -0
  33. data/app/models/inputs/asset_box_input.rb +13 -82
  34. data/app/models/inputs/asset_box_simple_form_input.rb +7 -0
  35. data/app/uploaders/effective_assets_uploader.rb +14 -2
  36. data/app/uploaders/{asset_uploader.rb → test_asset_uploader.rb} +1 -1
  37. data/app/views/active_admin/effective_assets/_edit.html.haml +3 -3
  38. data/app/views/active_admin/effective_assets/_new.html.haml +2 -1
  39. data/app/views/asset_box_input/_attachment_as_list.html.haml +17 -0
  40. data/app/views/asset_box_input/_attachment_as_table.html.haml +32 -0
  41. data/app/views/asset_box_input/_attachment_as_thumbnail.html.haml +20 -0
  42. data/app/views/asset_box_input/_dialog.html.haml +18 -0
  43. data/app/views/asset_box_input/_progress_bar_template.html.haml +8 -0
  44. data/app/views/asset_box_input/_uploader.html.haml +21 -115
  45. data/app/views/effective/assets/iframe.html.haml +17 -0
  46. data/app/views/effective/snippets/_effective_asset.html.haml +8 -0
  47. data/config/routes.rb +8 -2
  48. data/db/migrate/01_create_effective_assets.rb.erb +3 -0
  49. data/lib/effective_assets.rb +34 -3
  50. data/lib/effective_assets/engine.rb +11 -8
  51. data/lib/effective_assets/version.rb +1 -1
  52. data/lib/generators/templates/asset_uploader.rb +35 -0
  53. data/lib/generators/templates/effective_assets.rb +31 -4
  54. data/lib/tasks/effective_assets_tasks.rake +115 -4
  55. data/spec/internal/config/database.yml +3 -0
  56. data/spec/internal/config/initializers/effective_assets.rb +18 -0
  57. data/spec/internal/config/routes.rb +3 -0
  58. data/spec/internal/db/combustion_test.sqlite +0 -0
  59. data/spec/internal/db/schema.rb +52 -0
  60. data/spec/internal/log/test.log +793 -0
  61. data/spec/{dummy → internal}/public/favicon.ico +0 -0
  62. data/spec/internal/public/sprites.png +0 -0
  63. data/spec/models/asset_spec.rb +119 -35
  64. data/spec/spec_helper.rb +9 -3
  65. metadata +95 -234
  66. data/app/assets/images/effective_assets/s3_down_button.gif +0 -0
  67. data/app/assets/images/effective_assets/s3_over_button.gif +0 -0
  68. data/app/assets/images/effective_assets/s3_up_button.gif +0 -0
  69. data/app/assets/images/effective_assets/s3_upload.swf +0 -0
  70. data/app/assets/javascripts/effective_assets/asset_box_input.js.coffee +0 -71
  71. data/app/assets/javascripts/effective_assets/asset_box_uploader.js +0 -122
  72. data/app/assets/javascripts/effective_assets/asset_box_uploader_customization.js +0 -166
  73. data/app/controllers/effective/attachments_controller.rb +0 -14
  74. data/app/views/asset_box_input/_attachment_fields.html.haml +0 -14
  75. data/spec/dummy/README.rdoc +0 -261
  76. data/spec/dummy/Rakefile +0 -7
  77. data/spec/dummy/app/assets/javascripts/application.js +0 -15
  78. data/spec/dummy/app/assets/stylesheets/application.css +0 -13
  79. data/spec/dummy/app/controllers/application_controller.rb +0 -3
  80. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  81. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  82. data/spec/dummy/config.ru +0 -4
  83. data/spec/dummy/config/application.rb +0 -65
  84. data/spec/dummy/config/boot.rb +0 -10
  85. data/spec/dummy/config/database.yml +0 -25
  86. data/spec/dummy/config/environment.rb +0 -5
  87. data/spec/dummy/config/environments/development.rb +0 -37
  88. data/spec/dummy/config/environments/production.rb +0 -67
  89. data/spec/dummy/config/environments/test.rb +0 -37
  90. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  91. data/spec/dummy/config/initializers/inflections.rb +0 -15
  92. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  93. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  94. data/spec/dummy/config/initializers/session_store.rb +0 -8
  95. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  96. data/spec/dummy/config/locales/en.yml +0 -5
  97. data/spec/dummy/config/routes.rb +0 -58
  98. data/spec/dummy/db/development.sqlite3 +0 -0
  99. data/spec/dummy/db/schema.rb +0 -16
  100. data/spec/dummy/db/test.sqlite3 +0 -0
  101. data/spec/dummy/log/development.log +0 -71
  102. data/spec/dummy/log/test.log +0 -33
  103. data/spec/dummy/public/404.html +0 -26
  104. data/spec/dummy/public/422.html +0 -26
  105. data/spec/dummy/public/500.html +0 -25
  106. data/spec/dummy/script/rails +0 -6
  107. data/spec/dummy/spec_link +0 -3
@@ -0,0 +1,17 @@
1
+ unless defined?(Effective::AccessDenied)
2
+ module Effective
3
+ class AccessDenied < StandardError
4
+ attr_reader :action, :subject
5
+
6
+ def initialize(message = nil, action = nil, subject = nil)
7
+ @message = message
8
+ @action = action
9
+ @subject = subject
10
+ end
11
+
12
+ def to_s
13
+ @message || I18n.t(:'unauthorized.default', :default => 'Access Denied')
14
+ end
15
+ end
16
+ end
17
+ end
@@ -21,12 +21,16 @@ module Effective
21
21
 
22
22
  structure do
23
23
  title :string
24
+
24
25
  description :text
25
26
  tags :string
27
+ extra :text
26
28
 
27
29
  content_type :string, :validates => [:presence]
28
- upload_file :string # The full url of the file, as originally uploaded
30
+ upload_file :string, :validates => [:presence] # The full url of the file, as originally uploaded
31
+
29
32
  processed :boolean, :default => false
33
+ aws_acl :string, :default => 'public-read', :validates => [:presence, :inclusion => {:in => ['public-read', 'authenticated-read']}]
30
34
 
31
35
  data :string
32
36
 
@@ -40,26 +44,29 @@ module Effective
40
44
  end
41
45
 
42
46
  serialize :versions_info, Hash
47
+ serialize :extra, Hash
43
48
 
44
- #attr_accessible :title, :description, :tags, :content_type, :data_size, :upload_file, :user, :user_id, :id
45
- #validates_presence_of :user_id
46
-
49
+ before_validation :set_content_type
47
50
  before_save :update_asset_dimensions
51
+ after_save :enqueue_delayed_job
48
52
 
49
- default_scope order('created_at DESC')
53
+ default_scope -> { order(:id) }
54
+ scope :nonplaceholder, -> { where("#{EffectiveAssets.assets_table_name}.upload_file != ?", 'placeholder') }
50
55
 
51
- scope :images, -> { where('content_type LIKE ?', '%image%') }
52
- scope :videos, -> { where('content_type LIKE ?', '%video%') }
53
- scope :audio, -> { where('content_type LIKE ?', '%audio%') }
54
- scope :files, -> { where('(content_type NOT LIKE ?) AND (content_type NOT LIKE ?) AND (content_type NOT LIKE ?)', '%image%', '%video%', '%audio%') }
56
+ scope :images, -> { nonplaceholder().where("#{EffectiveAssets.assets_table_name}.content_type LIKE ?", '%image%') }
57
+ scope :nonimages, -> { nonplaceholder().where("(#{EffectiveAssets.assets_table_name}.content_type NOT LIKE ?)", '%image%') }
55
58
 
56
- scope :today, -> { where("created_at >= ?", Date.today.beginning_of_day) }
57
- scope :this_week, -> { where("created_at >= ?", Date.today.beginning_of_week) }
58
- scope :this_month, -> { where("created_at >= ?", Date.today.beginning_of_month) }
59
+ scope :videos, -> { nonplaceholder().where("#{EffectiveAssets.assets_table_name}.content_type LIKE ?", '%video%') }
60
+ scope :audio, -> { nonplaceholder().where("#{EffectiveAssets.assets_table_name}.content_type LIKE ?", '%audio%') }
61
+ scope :files, -> { nonplaceholder().where("(#{EffectiveAssets.assets_table_name}.content_type NOT LIKE ?) AND (#{EffectiveAssets.assets_table_name}.content_type NOT LIKE ?) AND (#{EffectiveAssets.assets_table_name}.content_type NOT LIKE ?)", '%image%', '%video%', '%audio%') }
62
+
63
+ scope :today, -> { nonplaceholder().where("#{EffectiveAssets.assets_table_name}.created_at >= ?", Time.zone.today.beginning_of_day) }
64
+ scope :this_week, -> { nonplaceholder().where("#{EffectiveAssets.assets_table_name}.created_at >= ?", Time.zone.today.beginning_of_week) }
65
+ scope :this_month, -> { nonplaceholder().where("#{EffectiveAssets.assets_table_name}.created_at >= ?", Time.zone.today.beginning_of_month) }
59
66
 
60
67
  class << self
61
68
  def s3_base_path
62
- "http://#{EffectiveAssets.aws_bucket}.s3.amazonaws.com"
69
+ "https://#{EffectiveAssets.aws_bucket}.s3.amazonaws.com/"
63
70
  end
64
71
 
65
72
  def string_base_path
@@ -68,49 +75,22 @@ module Effective
68
75
 
69
76
  # Just call me with Asset.create_from_url('http://somewhere.com/somthing.jpg')
70
77
  def create_from_url(url, options = {})
71
- opts = {:upload_file => url, :user_id => 1}.merge(options)
72
-
73
- if (asset = Asset.create(opts))
74
- Effective::DelayedJob.new.process_asset(asset)
75
- asset
76
- else
77
- false
78
- end
79
- end
80
-
81
- # We have just uploaded an asset via our s3 uploader
82
- # We want this image to be immediately available.
83
- def create_from_s3_uploader(url, options = {})
84
- opts = {:upload_file => "#{Asset.s3_base_path}/#{url}", :user_id => 1}.merge(options)
85
-
86
- asset = false
78
+ opts = {:upload_file => url, :user_id => 1, :aws_acl => EffectiveAssets.aws_acl}.merge(options)
87
79
 
88
- Asset.transaction do
89
- begin
90
- asset = Asset.create!(opts)
91
-
92
- Rails.logger.info "Copying s3 uploaded file to final resting place..."
93
- storage = Fog::Storage.new(:provider => 'AWS', :aws_access_key_id => EffectiveAssets.aws_access_key_id, :aws_secret_access_key => EffectiveAssets.aws_secret_access_key)
94
- storage.copy_object(EffectiveAssets.aws_bucket, url, EffectiveAssets.aws_bucket, "#{EffectiveAssets.aws_final_path.chomp('/')}/#{asset.id}/#{asset.file_name}")
95
- storage.put_object_acl(EffectiveAssets.aws_bucket, "#{EffectiveAssets.aws_final_path.chomp('/')}/#{asset.id}/#{asset.file_name}", EffectiveAssets.aws_acl)
96
-
97
- Rails.logger.info "Deleting original..."
98
- directory = storage.directories.get(EffectiveAssets.aws_bucket)
99
- directory.files.new(:key => url).destroy
100
-
101
- asset.update_column(:upload_file, asset.url) # This is our upload file as far as CarrierWave is now concerned
102
-
103
- Effective::DelayedJob.new.process_asset(asset)
104
- rescue => e
105
- Rails.logger.info e.message
106
- Rails.logger.info e.backtrace.join('\n')
107
- asset = false
80
+ attempts = 3 # Try to upload this string file 3 times
81
+ begin
82
+ asset = Asset.new(opts)
83
+
84
+ if asset.save
85
+ asset
86
+ else
87
+ puts asset.errors.inspect
88
+ Rails.logger.info asset.errors.inspect
89
+ false
108
90
  end
109
-
110
- raise ActiveRecord::Rollback unless asset
91
+ rescue => e
92
+ (attempts -= 1) >= 0 ? (sleep 2; retry) : false
111
93
  end
112
-
113
- asset
114
94
  end
115
95
 
116
96
  # This loads the raw contents of a string into a file and uploads that file to s3
@@ -119,50 +99,56 @@ module Effective
119
99
  def create_from_string(str, options = {})
120
100
  filename = options.delete(:filename) || "file-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.txt"
121
101
 
122
- opts = {:upload_file => "#{Asset.string_base_path}#{filename}", :user_id => 1}.merge(options)
102
+ filename = URI.escape(filename).gsub(/%\d\d|[^a-zA-Z0-9.-]/, '_') # Replace anything not A-Z, a-z, 0-9, . -
123
103
 
124
- asset = Asset.new(opts)
125
- asset.data = AssetStringIO.new(filename, str)
104
+ opts = {:upload_file => "#{Asset.string_base_path}#{filename}", :user_id => 1, :aws_acl => EffectiveAssets.aws_acl}.merge(options)
126
105
 
127
- if asset.save
128
- Effective::DelayedJob.new.process_asset(asset)
129
- asset
106
+ attempts = 3 # Try to upload this string file 3 times
107
+ begin
108
+ asset = Asset.new(opts)
109
+ asset.data = AssetStringIO.new(filename, str)
110
+
111
+ if asset.save
112
+ asset
113
+ else
114
+ puts asset.errors.inspect
115
+ Rails.logger.info asset.errors.inspect
116
+ false
117
+ end
118
+ rescue => e
119
+ (attempts -= 1) >= 0 ? (sleep 2; retry) : false
130
120
  end
121
+
131
122
  end
123
+ end # End of Class methods
124
+
125
+ def title
126
+ self[:title].presence || file_name
132
127
  end
133
128
 
134
- before_validation do
135
- if !content_type.present? or content_type == 'null' or content_type == 'unknown' or content_type == 'application/octet-stream'
136
- self.content_type = case url.to_s[-4, 4].downcase
137
- when '.mp3' ; 'audio/mp3'
138
- when '.mp4' ; 'video/mp4'
139
- when '.mov' ; 'video/mov'
140
- when '.m2v' ; 'video/m2v'
141
- when '.m4v' ; 'video/m4v'
142
- when '.3gp' ; 'video/3gp'
143
- when '.3g2' ; 'video/3g2'
144
- when '.avi' ; 'video/avi'
145
- when '.jpg' ; 'image/jpg'
146
- when '.gif' ; 'image/gif'
147
- when '.png' ; 'image/png'
148
- when '.bmp' ; 'image/bmp'
149
- when '.ico' ; 'image/x-icon'
150
- else ; 'unknown'
151
- end
152
- end
129
+ def extra
130
+ self[:extra] || {}
153
131
  end
154
132
 
155
- def title
156
- self[:title].present? ? self[:title] : file_name
133
+ def url(version = nil, expire_in = nil)
134
+ aws_acl == 'authenticated-read' ? authenticated_url(version, expire_in) : public_url(version)
157
135
  end
158
136
 
159
- # Return the final location of this asset
160
- def url
161
- "#{Asset.s3_base_path}/#{EffectiveAssets.aws_final_path.chomp('/')}/#{self.id.to_i}/#{upload_file.to_s.split('/').last}"
137
+ def public_url(version = nil)
138
+ if data.present?
139
+ version.present? ? data.send(version).file.url : data.file.url
140
+ else
141
+ "#{Asset.s3_base_path.chomp('/')}/#{EffectiveAssets.aws_path.chomp('/')}/#{id.to_i}/#{file_name}"
142
+ end
143
+ end
144
+
145
+ def authenticated_url(version = nil, expire_in = 60.minutes)
146
+ data.fog_authenticated_url_expiration = expire_in
147
+ version.present? ? data.send(version).file.authenticated_url : data.file.authenticated_url
162
148
  end
163
149
 
164
150
  def file_name
165
- url.split('/').last rescue url
151
+ upload_file.to_s.split('/').last rescue upload_file
166
152
  end
167
153
 
168
154
  def video?
@@ -181,22 +167,66 @@ module Effective
181
167
  content_type.include? 'audio'
182
168
  end
183
169
 
184
- def as_json(options={})
185
- {:thumbnail => image_tag(:thumb).html_safe}.merge super
170
+ def versions_info
171
+ self[:versions_info] || {}
186
172
  end
187
173
 
188
- def still_processing?
189
- !processed
174
+ def to_s
175
+ title
190
176
  end
191
177
 
192
- def versions_info
193
- self[:versions_info] || {}
178
+ def reprocess!
179
+ begin
180
+ Rails.logger.info "Reprocessing ##{id}..."
181
+ print "Reprocessing ##{id}..."
182
+
183
+ raise 'must be processed first before reprocessed' unless processed?
184
+
185
+ data.cache_stored_file!
186
+ data.retrieve_from_cache!(data.cache_name)
187
+ data.recreate_versions!
188
+ save!
189
+
190
+ Rails.logger.info "Successfully reprocessed ##{id}"
191
+ print "success"; puts ''
192
+ true
193
+ rescue => e
194
+ Rails.logger.info "Error: #{id} -> #{e.to_s}"
195
+ print "error: #{e.to_s}"; puts ''
196
+ false
197
+ end
194
198
  end
195
199
 
196
200
  protected
197
- # Called in the DelayedJob
198
- def update_asset_dimensions
199
- if data.present? and data_changed? and image?
201
+
202
+ def set_content_type
203
+ if [nil, 'null', 'unknown', 'application/octet-stream', ''].include?(content_type)
204
+ self.content_type = case File.extname(file_name).downcase.gsub(/\?.+/, '')
205
+ when '.mp3' ; 'audio/mp3'
206
+ when '.mp4' ; 'video/mp4'
207
+ when '.mov' ; 'video/mov'
208
+ when '.m2v' ; 'video/m2v'
209
+ when '.m4v' ; 'video/m4v'
210
+ when '.3gp' ; 'video/3gp'
211
+ when '.3g2' ; 'video/3g2'
212
+ when '.avi' ; 'video/avi'
213
+ when '.jpg' ; 'image/jpg'
214
+ when '.gif' ; 'image/gif'
215
+ when '.png' ; 'image/png'
216
+ when '.bmp' ; 'image/bmp'
217
+ when '.ico' ; 'image/x-icon'
218
+ when '.txt' ; 'text/plain'
219
+ when '.doc' ; 'application/msword'
220
+ when '.docx' ; 'application/msword'
221
+ when '.xls' ; 'application/excel'
222
+ when '.xlsx' ; 'application/excel'
223
+ else ; 'unknown'
224
+ end
225
+ end
226
+ end
227
+
228
+ def update_asset_dimensions
229
+ if data.present? && data_changed? && image?
200
230
  begin
201
231
  image = MiniMagick::Image.open(self.data.path)
202
232
  self.width = image[:width]
@@ -206,6 +236,13 @@ module Effective
206
236
  end
207
237
  true
208
238
  end
239
+
240
+ def enqueue_delayed_job
241
+ if !self.processed && self.upload_file.present? && self.upload_file != 'placeholder'
242
+ Effective::DelayedJob.new.process_asset(self.id)
243
+ end
244
+ end
245
+
209
246
  end
210
247
 
211
248
  class AssetStringIO < StringIO
@@ -12,14 +12,9 @@ module Effective
12
12
  box :string, :default => 'assets', :validates => [:presence] # This is essentially a category
13
13
  end
14
14
 
15
- validates_presence_of :asset_id
15
+ default_scope -> { includes(:asset).order("\"#{EffectiveAssets.attachments_table_name.to_s}\".\"position\" ASC, \"#{EffectiveAssets.attachments_table_name.to_s}\".\"asset_id\" ASC") }
16
16
 
17
- #attr_accessible :box, :position, :asset_id, :attachable_type, :attachable_id
17
+ validates_presence_of :asset_id
18
18
 
19
- class << self
20
- def default_scope
21
- includes(:asset).order(:position)
22
- end
23
- end
24
19
  end
25
20
  end
@@ -4,96 +4,51 @@
4
4
 
5
5
  module Effective
6
6
  class DelayedJob
7
- def process_asset(asset)
8
- DelayedJob.configure_carrierwave
9
-
10
- if asset and !asset.processed? and asset.upload_file.present?
11
- begin
12
- puts "Processing an asset ID ##{asset.id}..."
13
-
14
- if asset.upload_file.include?("#{Asset.s3_base_path}")
15
- if asset.image?
16
- puts "Asset is an image in our S3 assets directory. Downloading and processing..."
17
-
18
- # Carrierwave must download the file, process it, then re-upload it to S3
19
- asset.remote_data_url = asset.upload_file
20
- asset.processed = true
21
- asset.save!
22
- else
23
- puts "Asset is a non-image in our S3 uploads directory. Copying to final location..."
24
-
25
- # We have uploaded a video, or something non-image to our S3 bucket.
26
- # We do not currently process anything.
27
-
28
- puts "Marking local asset as processed..."
29
- asset.update_column(:data, asset.file_name)
30
- asset.processed = true
31
- asset.save!
32
- end
33
- elsif asset.upload_file.include?(Asset.string_base_path)
34
- puts "Asset is a string-based asset. Processing..."
35
-
36
- asset.data.cache_stored_file!
37
- asset.data.retrieve_from_cache!(asset.data.cache_name)
38
- asset.data.recreate_versions!
39
- asset.processed = true
40
- asset.save!
41
- else
42
- puts "Asset is not an s3 uploaded asset. Downloading and processing..."
43
-
44
- # Carrierwave must download the file, process it, then re-upload it to S3
45
- asset.remote_data_url = asset.upload_file
46
- asset.processed = true
47
- asset.save!
48
- end
49
-
50
- puts "Successfully processed the asset."
51
- rescue => e
52
- puts "An error occurred while processing an asset:"
53
- puts e.message
54
- puts e.backtrace.inspect
55
- end
7
+ def process_asset(obj)
8
+ if obj.kind_of?(Effective::Asset)
9
+ asset = obj
10
+ else
11
+ asset = Effective::Asset.where(:id => (obj.to_i rescue 0)).first
56
12
  end
57
- end
58
- handle_asynchronously :process_asset
59
13
 
60
- def reprocess_all_assets
61
- DelayedJob.configure_carrierwave
14
+ if asset.present? && !asset.processed? && asset.upload_file.present? && asset.upload_file != 'placeholder'
15
+ puts "Processing asset ##{asset.id} from #{asset.upload_file}."
16
+
17
+ if asset.upload_file.include?(Effective::Asset.string_base_path)
18
+ puts "String-based Asset processing and uploading..."
62
19
 
63
- Asset.all.each do |asset|
64
- begin
65
- puts "Processing Asset ID=#{asset.id}..."
66
20
  asset.data.cache_stored_file!
67
21
  asset.data.retrieve_from_cache!(asset.data.cache_name)
68
22
  asset.data.recreate_versions!
69
- asset.save!
70
- puts "Successfully processed #{asset.inspect}"
71
- rescue => e
72
- puts "ERROR: #{asset.id} -> #{e.to_s}"
23
+ elsif asset.upload_file.include?(Effective::Asset.s3_base_path)
24
+ puts "S3 Uploaded Asset downloading and processing..."
25
+ # Carrierwave must download the file, process it, then upload the generated versions to S3
26
+ # We only want to process if it's an image, so we don't download zips or videos
27
+ asset.remote_data_url = asset.url if asset.image?
28
+ else
29
+ puts "Non S3 Asset downloading and processing..."
30
+ puts "Downloading #{asset.url}"
31
+
32
+ # Carrierwave must download the file, process it, then upload it and generated verions to S3
33
+ # We only want to process if it's an image, so we don't download zips or videos
34
+ asset.remote_data_url = asset.upload_file
73
35
  end
74
- end
75
- end
76
- handle_asynchronously :reprocess_all_assets
77
36
 
78
- def self.configure_carrierwave
79
- return if defined? @@configured_carrierwave
37
+ asset.processed = true
38
+ asset.save!
80
39
 
81
- CarrierWave.configure do |config|
82
- config.fog_credentials = {
83
- :provider => 'AWS',
84
- :aws_access_key_id => EffectiveAssets.aws_access_key_id,
85
- :aws_secret_access_key => EffectiveAssets.aws_secret_access_key
86
- }
87
- config.fog_directory = EffectiveAssets.aws_bucket
88
- config.fog_public = EffectiveAssets.aws_acl.to_s.include?('public')
89
- config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
90
- config.cache_dir = "#{Rails.root}/tmp/uploads" # For heroku
91
- end
40
+ (GC.start rescue nil)
92
41
 
93
- Rails.logger.info "CONFIGURED CARRIERWAVE"
42
+ puts "Successfully processed the asset."
43
+ end
44
+ end
45
+ handle_asynchronously :process_asset
94
46
 
95
- @@configured_carrierwave = true
47
+ def reprocess_asset(id)
48
+ Effective::Asset.find(id).reprocess!
49
+ (GC.start rescue nil)
96
50
  end
51
+ handle_asynchronously :reprocess_asset
97
52
 
98
53
  end
99
54
  end