Module: ActsAsPhocodable

Defined in:
lib/phocoder_rails/errors.rb,
lib/phocoder_rails/acts_as_phocodable.rb

Defined Under Namespace

Modules: InstanceMethods Classes: Error, HTTPError, ThumbnailAttributesNotFoundError, ThumbnailNotFoundError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.phocodable_configObject



78
79
80
81
82
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 78

def self.phocodable_config
  #puts "checking phocodable_config for #{self.config}"
  self.read_phocodable_configuration if self.config.nil?
  self.config
end

.read_phocodable_configurationObject



84
85
86
87
88
89
90
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 84

def self.read_phocodable_configuration
  
  config_path =  File.join(::Rails.root.to_s, ActsAsPhocodable.config_file)
  puts "#{self.config} - looking for a config in #{config_path}"
  self.config =  YAML.load(ERB.new(File.read(config_path)).result)[::Rails.env.to_s].symbolize_keys
  #self.apply_phocodable_configuration
end

Instance Method Details

#acts_as_phocodable(options = { }) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
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/phocoder_rails/acts_as_phocodable.rb', line 201

def acts_as_phocodable(options = { })
  
  include InstanceMethods
  if defined? Spawn
    include Spawn
  end
  attr_reader :saved_file
  attr_accessor :phocoding
  after_save :save_local_file
  before_destroy :cleanup #:remove_local_file,:destroy_thumbnails,:remove_s3_file
  
  include ActiveSupport::Callbacks

  define_callbacks :local_file_saved, :file_saved, :file_ready, :phocode_hdr, :phocode_hdrhtml, :phocode_composite, :phocode_tone_mapping
  
  #cattr_accessor :phocoder_options
  #self.phocoder_options = options
  
  cattr_accessor :phocoder_thumbnails
  self.phocoder_thumbnails = options[:thumbnails] ||= []
  
  cattr_accessor :zencoder_videos
  self.zencoder_videos = options[:videos] ||= []
  
  cattr_accessor :thumbnail_class
  self.thumbnail_class = options[:thumbnail_class] ? options[:thumbnail_class].constantize : self
  
  cattr_accessor :parent_class
  self.parent_class = options[:parent_class] ? options[:parent_class].constantize : self
  
  has_many   :thumbnails, :class_name => "::#{self.thumbnail_class.name}",:as => :parent 
  if self.thumbnail_class != self.parent_class
    #we have to do this to get the poster for videos covered
    belongs_to  :parent, :polymorphic => true
  else
    belongs_to  :parent, :class_name => "::#{self.parent_class.name}" ,:foreign_key => "parent_id"
  end
  
  
  has_many :encodable_jobs, :as => :encodable 
  
  scope :top_level, where({:parent_id=>nil}) if respond_to?(:parent_id)
  scope :top_level, where({}) if !respond_to?(:parent_id)
  # we can't just call this next scope 'parents' because that is already
  # taken and returns an array of parent classes of the ruby object
  scope :parent_items, where({:parent_id=>nil}) if respond_to?(:parent_id)
  scope :parent_items, where({}) if !respond_to?(:parent_id)
  
  scope :thumbnails, where("#{base_class.table_name}.parent_id is not null")
  
  #just a writer, the reader is below
  cattr_accessor :phocodable_configuration
  read_phocodable_configuration
end

#apply_phocodable_configurationObject



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 365

def apply_phocodable_configuration
  if self.phocodable_configuration[:base_url]
    ActsAsPhocodable.base_url = phocodable_configuration[:base_url]
  end
  if self.phocodable_configuration[:storeage_mode]
    ActsAsPhocodable.storeage_mode = phocodable_configuration[:storeage_mode]
  end
  if self.phocodable_configuration[:processing_mode]
    ActsAsPhocodable.processing_mode = phocodable_configuration[:processing_mode]
  end
  if self.phocodable_configuration[:s3_bucket_name]
    ActsAsPhocodable.s3_bucket_name = phocodable_configuration[:s3_bucket_name]
  end
  if self.phocodable_configuration[:s3_access_key_id]
    ActsAsPhocodable.s3_access_key_id = phocodable_configuration[:s3_access_key_id]
  end
  if self.phocodable_configuration[:s3_secret_access_key]
    ActsAsPhocodable.s3_secret_access_key = phocodable_configuration[:s3_secret_access_key]
  end
  if self.phocodable_configuration[:javascript_library]
    ActsAsPhocodable.javascript_library = phocodable_configuration[:javascript_library]
  end
  if self.phocodable_configuration[:local_base_dir]
    ActsAsPhocodable.local_base_dir = phocodable_configuration[:local_base_dir]
  end
  if self.phocodable_configuration[:track_jobs]
    ActsAsPhocodable.track_jobs = phocodable_configuration[:track_jobs]
  end
  if self.phocodable_configuration[:track_components]
    ActsAsPhocodable.track_components = phocodable_configuration[:track_components]
  end
  
  
  if self.phocodable_configuration[:phocoder_url]
    ::Phocoder.base_url = phocodable_configuration[:phocoder_url]
  end
  if self.phocodable_configuration[:phocoder_api_key]
    ::Phocoder.api_key = phocodable_configuration[:phocoder_api_key]
  end
  if self.phocodable_configuration[:zencoder_api_key]
    ::Zencoder.api_key = phocodable_configuration[:zencoder_api_key]
  end


  if ActsAsPhocodable.storeage_mode == "s3"
    self.establish_aws_connection
  end
end

#configObject



256
257
258
259
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 256

def config
  return phocodable_configuration if !phocodable_configuration.blank?
  self.read_phocodable_configuration
end

#create_atts_from_size_string(label_string) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 335

def create_atts_from_size_string(label_string)
  match = label_string.match ActsAsPhocodable.label_size_regex
  return nil if match.blank?
  atts = {}
  if !match[1].blank?
    atts[:width] = match[1]
  end
  if !match[2].blank?
    atts[:height] = match[2]
  end
  if !match[3].blank?
    atts[:aspect_mode] = match[3]
  end
  atts[:label] = label_string
  #atts[:label] = "#{atts[:width]}x#{atts[:height]}"
  #atts[:label] += "_#{atts[:aspect_mode]}" if atts[:aspect_mode]
  atts
end

#create_label_from_size_string(size_string) ⇒ Object



327
328
329
330
331
332
333
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 327

def create_label_from_size_string(size_string)
  if size_string.match ActsAsPhocodable.size_string_regex
    size_string = size_string.gsub("!","crop")
    size_string = size_string.gsub(">","preserve")
  end
  size_string
end

#establish_aws_connectionObject



414
415
416
417
418
419
420
421
422
423
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 414

def establish_aws_connection
  AWS.config({
    :access_key_id     => ActsAsPhocodable.s3_access_key_id,
    :secret_access_key => ActsAsPhocodable.s3_secret_access_key
  })
#  AWS::S3::Base.establish_connection!(
#                                      :access_key_id     => ActsAsPhocodable.s3_access_key_id,
#                                      :secret_access_key => ActsAsPhocodable.s3_secret_access_key
#  )
end

#image?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 183

def image?(content_type)
  image_types.include?(content_type)
end

#image_typesObject

The list of content types that will trigger image handling.



140
141
142
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 140

def image_types
  web_safe_image_types + other_image_types
end

#read_phocodable_configurationObject



355
356
357
358
359
360
361
362
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 355

def read_phocodable_configuration
  #raise "This method is going away!"
  #puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  #puts "This method is going away!"
  #puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  self.phocodable_configuration = ActsAsPhocodable.phocodable_config
  self.apply_phocodable_configuration
end

#thumbnail_attributes_for(thumbnail_name = "small") ⇒ Object



316
317
318
319
320
321
322
323
324
325
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 316

def thumbnail_attributes_for(thumbnail_name = "small")
  atts = self.phocoder_thumbnails.select{|atts| atts[:label] == thumbnail_name }.first
  if atts.blank?
    atts = create_atts_from_size_string(thumbnail_name)
  end
  if atts.blank?
    raise ThumbnailAttributesNotFoundError.new("No thumbnail attributes were found for label '#{thumbnail_name}'")
  end
  atts
end

#update_from_phocoder(params) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 265

def update_from_phocoder(params)
  Rails.logger.debug "tying to call update from phocoder for params = #{params.to_json}"
  if !params[:output].blank?
    Rails.logger.debug "find_by_phocoder_output_id #{params[:output][:id]}"
    iu = find_by_phocoder_output_id params[:output][:id]
    Rails.logger.debug "the item = #{iu}"
    img_params = params[:output]
    iu.filename = File.basename(params[:output][:url]) #if iu.filename.blank?
    if ActsAsPhocodable.storeage_mode == "local"
      iu.save_url(params[:output][:url])
    end
  else
    iu = find_by_phocoder_input_id params[:input][:id]
    img_params = params[:input]
  end
  [:file_size,:width,:height,:taken_at,:lat,:lng].each do |att|
    setter = att.to_s + "="
    if iu.respond_to? setter and !img_params[att].blank?
      iu.send setter, img_params[att]
    end
  end
  
  #iu.file_size = img_params[:file_size]
  #iu.width = img_params[:width]
  #iu.height = img_params[:height]
  iu.phocoder_status = "ready"
  iu.save
  iu
end

#update_from_zencoder(params) ⇒ Object

Updating from zencoder is a two pass operation. This method gets called for each output when it’s ready. Once all outputs are ready, we call parent.check_zencoder_details



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 299

def update_from_zencoder(params)
  Rails.logger.debug "tying to call update from zencoder for params = #{params}"
  iu = find_by_zencoder_output_id params[:output][:id]
  if params[:output][:url].match /%2F(.*)\?/
    iu.filename = $1
  else
    iu.filename = File.basename(params[:output][:url].match(/(.*)\??/)[1])
  end
  #iu.filename = File.basename(params[:output][:url].match(/(.*)\??/)[1]) if iu.filename.blank?
  if ActsAsPhocodable.storeage_mode == "local"
    iu.save_url(params[:output][:url])
  end
  iu.zencoder_status = "ready"
  iu.save
  iu.parent.check_zencoder_details
end

#validates_phocodableObject



261
262
263
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 261

def validates_phocodable
  validates_presence_of :content_type, :filename, :if=>lambda{ parent_id.blank? }
end

#video?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 191

def video?(content_type)
  video_types.include?(content_type)
end

#web_safe?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 187

def web_safe?(content_type)
  web_safe_image_types.include?(content_type)
end