Module: Gluttonberg::Library::AttachmentMixin::InstanceMethods

Defined in:
lib/gluttonberg/library/attachment_mixin.rb

Instance Method Summary collapse

Instance Method Details

#asset_folder_pathObject



111
112
113
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 111

def asset_folder_path
  "/user_assets/#{asset_hash}"
end

#asset_processingObject



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 318

def asset_processing
  asset_id_to_process = self.id
  asset = Asset.find(:first , :conditions => { :id => asset_id_to_process } )
  if asset
      #if it is an image, then create a thumbnail
      if asset.asset_type.asset_category.name == "image"

        generate_thumb_and_proper_resolution(asset)
      elsif asset.asset_type.asset_category.name == "audio"
        # If its mp3 file, collect its sound info
        puts "==========step1 - audio"
        collect_mp3_info(asset)
        puts "==========step last - audio"
      end
      #additional processing
      asset_processors = Rails.configuration.asset_processors
      unless asset_processors.blank?
        asset_processors.each do |processor|
          processor.process(asset)
        end
      end

  end

end

#collect_mp3_info(asset) ⇒ Object

TODO Collect mp3 files info using Mp3Info gem



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 292

def collect_mp3_info(asset)
  audio = AudioAssetAttribute.find( :first , :conditions => {:asset_id => asset.id})

  begin
      #open mp3 file
      Mp3Info.open(location_on_disk) do |mp3|
        if audio.blank?
          #, :genre => mp3.genre
          AudioAssetAttribute.create( :asset_id => asset.id , :length => mp3.length , :title => mp3.tag.title , :artist => mp3.tag.artist , :album => mp3.tag.album , :tracknum => mp3.tag.tracknum)
        else
          audio.update_attributes( {:length => mp3.length, :genre =>"" , :title => mp3.tag.title , :artist => mp3.tag.artist , :album => mp3.tag.album , :tracknum => mp3.tag.tracknum })
        end

      end
      if Gluttonberg::Setting.get_setting("audio_assets") == "Enable"
        Delayed::Job.enqueue AudioJob.new(asset.id)
      end
  rescue => detail
    # if exception occurs and asset has some attributes, then remove them.
    unless audio.blank?
      audio.update_attributes( {:length => nil , :title => nil , :artist => nil , :album => nil , :tracknum => nil })
    end
  end

end

#directoryObject

The generated directory where this file is located. If it is an image it’s thumbnails will be stored here as well.



150
151
152
153
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 150

def directory
  Library.setup
  Library.root + "/" + self.asset_hash
end

#fileObject

Returns the file assigned by file=



98
99
100
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 98

def file
  @file
end

#file=(new_file) ⇒ Object

Setter for the file object. It sanatises the file name and stores in the filename property. It also sets the mime-type and size.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 75

def file=(new_file)
  unless new_file.blank?
    logger.info("\nFILENAME: #{new_file.original_filename} \n\n")

    # Forgive me this naive sanitisation, I'm still a regex n00b
    clean_filename = new_file.original_filename.split(%r{[\\|/]}).last
    clean_filename = clean_filename.gsub(" ", "_").gsub(/[^A-Za-z0-9\-_.]/, "").downcase

    # _thumb.#{file_extension} is a reserved name for the thumbnailing system, so if the user
    # has a file with that name rename it.
    if (clean_filename == '_thumb_small.#{file_extension}') || (clean_filename == '_thumb_large.#{file_extension}')
      clean_filename = 'thumb.#{file_extension}'
    end


    self.mime_type = new_file.content_type
    self.file_name = clean_filename
    self.size = new_file.size
    @file = new_file
  end
end

#file_extensionObject



102
103
104
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 102

def file_extension
  file_name.split(".").last
end

#generate_cropped_image(x, y, w, h, image_type) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 183

def generate_cropped_image(x , y , w , h, image_type)

  asset_thumb = self.asset_thumbnails.find(:first , :conditions => {:thumbnail_type => image_type.to_s })
  if asset_thumb.blank?
    asset_thumb = self.asset_thumbnails.create({:thumbnail_type => image_type.to_s , :user_generated => true })
  else
    asset_thumb.update_attributes(:user_generated => true)
  end

  path = original_file_on_disk
  original_ext = original_file_on_disk.split(".").last
  path = File.join(directory, "#{self.class.sizes[image_type.to_sym][:filename]}.#{file_extension}") unless image_type.blank?
  begin
    image = QuickMagick::Image.read(original_file_on_disk).first
  rescue
    image = QuickMagick::Image.read(original_file_on_disk).first
  end
  begin
    thumb_defined_width = self.class.sizes[image_type.to_sym][:geometry].split('x').first#.to_i
    scaling_percent = (thumb_defined_width.to_i/(w.to_i*1.0))*100
    image.arguments << " -crop #{w}x#{h}+#{x}+#{y} +repage"
    if scaling_percent != 1.0
      image.arguments << " -resize #{scaling_percent}%"
    end
  rescue => e
    puts e
  end
  image.save path
end

#generate_image_thumbObject

Create thumbnailed versions of image attachements. TODO: generate thumbnails with the correct extension



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 215

def generate_image_thumb

  begin

    self.class.sizes.each_pair do |name, config|

      asset_thumb = self.asset_thumbnails.find(:first , :conditions => {:thumbnail_type => name.to_s, :user_generated => true })

      if asset_thumb.blank?
          #image = QuickMagick::Image.read(location_on_disk).first
          begin
            image = QuickMagick::Image.read(original_file_on_disk).first
          rescue
            image = QuickMagick::Image.read(location_on_disk).first
          end

          original_ext = original_file_on_disk.split(".").last

          path = File.join(directory, "#{config[:filename]}.#{file_extension}")
          if config[:geometry].include?("#")
            #todo
            begin
              image.resize suggested_measures(image, config[:geometry])
              image.arguments << " -gravity Center  -crop #{config[:geometry].delete("#")}+0+0 +repage"
            rescue => e
              puts e
            end
          else
            image.resize config[:geometry]
          end
          image.save path
      end
    end

    update_attribute( :custom_thumbnail , true)
  rescue => e
    update_attribute( :custom_thumbnail , false)
  end

end

#generate_proper_resolutionObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 256

def generate_proper_resolution

    begin
      make_backup
      begin
        image = QuickMagick::Image.read(original_file_on_disk).first
      rescue => e
        image = QuickMagick::Image.read(location_on_disk).first
      end

      actual_width = image.width.to_i
      actual_height = image.height.to_i

      update_attributes( :width => actual_width ,:height => actual_height)

      image.resize self.class.max_image_size
      image.save File.join(directory, file_name)
      # remove mp3 info if any image have. it may happen in the case of updating asset from mp3 to image
        audio = AudioAssetAttribute.find( :first , :conditions => {:asset_id => asset.id})
        audio.destroy
    rescue #TypeError => error
      # ignore TypeErrors, just means it wasn't a supported image
      update_attribute( :custom_thumbnail , false)
    end
end

#generate_thumb_and_proper_resolution(asset) ⇒ Object

Generates thumbnails for images, but also additionally checks to see TODO if the uploaded image exceeds the specified maximum, in which case it will resize it down.



285
286
287
288
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 285

def generate_thumb_and_proper_resolution(asset)
      asset.generate_proper_resolution
      asset.generate_image_thumb
end

#location_on_diskObject

Returns the full path to the file’s location on disk.



137
138
139
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 137

def location_on_disk
  directory + "/" + file_name
end

#original_file_on_diskObject

In the case where an uploaded image has been larger that the specified max-size and consequently resized, this method will provide the path to the original, un-altered file.



144
145
146
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 144

def original_file_on_disk
  directory + "/original_" + file_name
end

#suggested_measures(object, required_geometry) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 155

def suggested_measures(object , required_geometry)
   required_geometry = required_geometry.delete("#")
   required_geometry_tokens = required_geometry.split("x")
   actual_width = object.width.to_i
   actual_height = object.height.to_i
   required_width = required_geometry_tokens.first.to_i
   required_height = required_geometry_tokens.last.to_i

   ratio_required = required_width.to_f / required_height
   ratio_actual = actual_width.to_f / actual_height

   crossover_ratio = required_height.to_f / actual_height
   crossover_ratio2 = required_width.to_f / actual_width

   if(crossover_ratio < crossover_ratio2 )
     crossover_ratio = crossover_ratio2
   end

   projected_height = actual_height * crossover_ratio

   if(projected_height < required_height )
     required_width = required_width * (1 + (ratio_actual -  ratio_required ) )
   end
projected_width = actual_width * crossover_ratio

   "#{(projected_width).ceil}x#{(projected_width/ratio_actual).ceil}"
end

#thumb_large_urlObject

Returns the public URL to the asset’s large thumbnail — relative to the domain.



132
133
134
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 132

def thumb_large_url
  url_for(:large_thumb) if category.downcase == "image"
end

#thumb_small_urlObject

Returns the public URL to the asset’s small thumbnail — relative to the domain.



126
127
128
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 126

def thumb_small_url
  url_for(:small_thumb) if category.downcase == "image"
end

#urlObject

Returns the public URL to this asset, relative to the domain.



107
108
109
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 107

def url
  "/user_assets/#{asset_hash}/#{file_name}"
end

#url_for(name) ⇒ Object

Returns the URL for the specified image size.



117
118
119
120
121
122
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 117

def url_for(name)
    if self.class.sizes.has_key? name
      filename = self.class.sizes[name][:filename]
      "/user_assets/#{asset_hash}/#{filename}.#{file_extension}"
    end
end