Module: ActsAsPhocodable::InstanceMethods

Defined in:
lib/phocoder_rails/acts_as_phocodable.rb

Instance Method Summary collapse

Instance Method Details

#base_urlObject



1207
1208
1209
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1207

def base_url
  self.class.base_url
end

#calc_height(thumbnail_atts) ⇒ Object



1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1326

def calc_height(thumbnail_atts)
  tw = thumbnail_atts[:width].blank? ? 100000 : thumbnail_atts[:width].to_f
  th = thumbnail_atts[:height].blank? ? 100000 : thumbnail_atts[:height].to_f
  w =  width.to_f
  h =  height.to_f
  if w <= tw and h <= th
    h.round
  elsif w > h
    if (h * ( tw / w )).round < th
     (h * ( tw / w )).round
    else
      th.round
    end
  else
    if (w * ( th / h )).round < tw
      th.round
    else
     (h * ( tw / w )).round
    end
  end
end

#calc_width(thumbnail_atts) ⇒ Object

Calculate the width for the target thumbnail atts



1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1303

def calc_width(thumbnail_atts)   
  tw = thumbnail_atts[:width].blank? ? 100000 : thumbnail_atts[:width].to_f
  th = thumbnail_atts[:height].blank? ? 100000 : thumbnail_atts[:height].to_f
  w =  width.to_f
  h =  height.to_f
  if w <= tw and h <= th
    w.round
  elsif w > h
    if (h * ( tw / w )).round < tw
      tw .round
    else
     (h * ( tw / w )).round
    end
  else
    if (w * ( th / h )).round < tw
     (w * ( th / h )).round
    else
      tw.round
    end
  end
end

#callback_urlObject



1175
1176
1177
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1175

def callback_url
  self.base_url + self.notification_callback_path
end

#check_zencoder_detailsObject



899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 899

def check_zencoder_details
  # we don't get a lot of info from zencoder, so we have to ask for details
  shouldCheck = true
  thumbnails.each do |t|
    if t.encodable_status != 'ready'
      shouldCheck = false
    end
  end
  
  return  if !shouldCheck
  
  details = Zencoder::Job.details(self.encodable_jobs.last.zencoder_job_id)
  
  #puts "in check_zencoder_details the details.body = #{details.body.to_json}"
  
  if details.body["job"]["state"] != 'finished'
    self.encodable_jobs.last.zencoder_status = details.body["job"]["state"]
    save
    return
  end
  
  self.encodable_jobs.last.zencoder_status = self.encodable_status = "ready"
  self.width = details.body["job"]["input_media_file"]["width"]
  self.height = details.body["job"]["input_media_file"]["height"]
  self.duration_in_ms = details.body["job"]["input_media_file"]["duration_in_ms"]
  self.file_size = details.body["job"]["input_media_file"]["file_size_bytes"]
  self.save
  
  #puts "the output files = #{details.body["job"]["output_media_files"]}"
  
  update_zencoder_outputs(details)
        
  # Now create the image thumb
  create_zencoder_image_thumb(details)
  
end

#cleanupObject



1110
1111
1112
1113
1114
1115
1116
1117
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1110

def cleanup
  #puts "calling cleanup!"
  destroy_thumbnails
  remove_local_file
  if ActsAsPhocodable.storeage_mode == "s3"
    remove_s3_file
  end
end

#clear_phocodingObject



528
529
530
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 528

def clear_phocoding
  phocoding = false
end

#cloudfront_base_hostObject



1223
1224
1225
1226
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1223

def cloudfront_base_host
  host = self.class.cloudfront_host
  host.instance_of?(Proc) ? host.call(s3_key) : host
end

#cloudfront_urlObject



1228
1229
1230
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1228

def cloudfront_url
  "#{cloudfront_base_host}/#{s3_key}"
end

#create_atts_from_size_string(label_string) ⇒ Object



1000
1001
1002
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1000

def create_atts_from_size_string(label_string)
  self.class.create_atts_from_size_string(label_string)
end

#create_phocoder_job(params) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 543

def create_phocoder_job(params)
  response = nil
  tries = 0
  max_tries = 3
  sleeps = [2,4,8]
  while response.blank? do
    begin
      response = Phocoder::Job.create(params)
    rescue Exception => e
      if tries < max_tries
        sleep sleeps[tries]
        tries += 1
        response = nil
      else
        raise
      end
    end
  end
  response
end

#create_thumbnails_from_response(response_thumbs, job_id) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 489

def create_thumbnails_from_response(response_thumbs,job_id)
  new_thumbs = []
  response_thumbs.each do |thumb_params|
    #puts "creating a thumb for #{thumb_params["label"]}"
    # we do this the long way around just in case some of these
    # atts are attr_protected
    thumb = nil
    if respond_to?(:parent_id) and !self.parent_id.blank? 
      Rails.logger.debug "trying to create a thumb from the parent "
      thumb = self.parent.thumbnails.new()
      self.parent.thumbnails << thumb
    else
      Rails.logger.debug "trying to create a thumb from myself "
      thumb = self.thumbnails.new()
      self.thumbnails << thumb
    end
    
    
    thumb.thumbnail = thumb_params["label"]
    thumb.filename = thumb_params["filename"]
    thumb.width = thumb_params["width"]
    thumb.height = thumb_params["height"]
    if ActsAsPhocodable.track_components
      tjob = thumb.encodable_jobs.new
      tjob.phocoder_output_id = thumb_params["id"]
      tjob.phocoder_job_id = job_id
      #thumb.parent_id = self.id
      tjob.phocoder_status  =  "phocoding"
      thumb.encodable_jobs << tjob
    end
    thumb.encodable_status = "phocoding"
    thumb.save
    new_thumbs << thumb
    Rails.logger.debug "    thumb.errors = #{thumb.errors.to_json}"
    #puts "    thumb.errors = #{thumb.errors.to_json}"
  end
  new_thumbs
end

#create_zencoder_image_thumb(details) ⇒ Object



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 950

def create_zencoder_image_thumb(details)
  
  # for now we should only have one thumbnail
  output = details.body["job"]["thumbnails"].first
  return if output.blank?
  thumb = thumbnails.new
  thumb.thumbnail = "poster"
  thumb.width = output["width"]
  thumb.height = output["height"]
  thumb.file_size = output["file_size_bytes"]
  thumb.filename = "frame_0000.png" #File.basename(output["url"])
  thumb.content_type = "image/png"
  if ActsAsPhocodable.storeage_mode == "local"
    thumb.save_url(output["url"])
  end
  thumb.save
  #now get thumbnails for the poster
  thumb.phocode 
end

#dedupe_input_thumbs(input_thumbs) ⇒ Object



532
533
534
535
536
537
538
539
540
541
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 532

def dedupe_input_thumbs(input_thumbs)
  needed_thumbs = []
  input_thumbs.each do |it|
    t = thumbnails.find_by_thumbnail(it[:label])
    if t.blank?
      needed_thumbs << it 
    end
  end
  needed_thumbs
end

#destroy_thumbnailsObject



988
989
990
991
992
993
994
995
996
997
998
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 988

def destroy_thumbnails
  if self.class.phocoder_thumbnails.size == 0 and self.class.zencoder_videos.size == 0 
    #puts "we're skipping destory_thumbnails since we don't do any processing "
    return
  end
  #puts "calling destory thumbnails for #{self.thumbnails.count} - #{self.thumbnails.size}"
  self.thumbnails.each do |thumb|
    thumb.destroy
  end
  #puts "calling destory thumbnails for #{self.thumbnails.count}"
end

#encodeObject



446
447
448
449
450
451
452
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 446

def encode
  if image?
    phocode
  elsif video?
    zencode
  end
end

#error?Boolean

Returns:

  • (Boolean)


475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 475

def error?
  if ActsAsPhocodable.storeage_mode == "offline"
    false
  #elsif image?
  #  return phocoder_status=='failed'
  #elsif video?
  #  return zencoder_status=='failed'
  #else
  #  true
  else
    return encodable_status == "ready"
  end
end

#file=(new_file) ⇒ Object



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1041

def file=(new_file)
  return if new_file.nil?
  Rails.logger.debug "we got a new file of class = #{new_file.class}"
  cleanup
  if new_file.is_a? File
    self.filename = File.basename new_file.path
    self.content_type = MIME::Types.type_for(self.filename).first.content_type 
    self.file_size = new_file.size
  else
    self.filename = new_file.original_filename
    self.content_type = new_file.content_type
    self.file_size = new_file.size
  end
  self.content_type = File.mime_type?(self.filename) if (self.content_type.blank? || self.content_type == "[]")
  if new_file.respond_to? :tempfile
    @saved_file = new_file.tempfile
  else
    @saved_file = new_file
  end
end

#filename=(new_name) ⇒ Object

Sanitizes a filename.



1286
1287
1288
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1286

def filename=(new_name)
  write_attribute :filename, sanitize_filename(new_name)
end

#fire_ready_callbackObject



1104
1105
1106
1107
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1104

def fire_ready_callback
  run_callbacks :file_ready do
  end
end

#get_thumbnail(thumbnail_name) ⇒ Object



1037
1038
1039
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1037

def get_thumbnail(thumbnail_name)
  thumbnail_for(thumbnail_name)
end

#image?Boolean

Returns:

  • (Boolean)


434
435
436
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 434

def image?
  self.class.image?(content_type)
end

#local_dirObject



1147
1148
1149
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1147

def local_dir
  File.join(ActsAsPhocodable.local_base_dir,resource_dir)
end

#local_pathObject



1151
1152
1153
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1151

def local_path
  filename.blank? ? nil : File.join(local_dir,filename)
end

#local_urlObject



1155
1156
1157
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1155

def local_url
  filename.blank? ? nil : File.join("/",resource_dir,filename)
end

#new_zencoder_filename(format) ⇒ Object



894
895
896
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 894

def new_zencoder_filename(format)
  filename + "." + self.class.video_extensions[format]
end

#notification_callback_pathObject



1191
1192
1193
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1191

def notification_callback_path
  "/phocoder/phocoder_notifications/#{self.class.name}/#{self.id}.json"
end

#path_idObject



1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1128

def path_id
  
  #puts "parent_id = #{parent_id}"
  #puts "parent = #{parent}"
  if respond_to?(:parent_id)
    parent_id.blank? ? id : parent.path_id
  else
    id
  end
end

#phocodable?Boolean

Returns:

  • (Boolean)


975
976
977
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 975

def phocodable?
  true
end

#phocodable_configObject



970
971
972
973
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 970

def phocodable_config
  #puts "looking for config!"
  self.class.config
end

#phocode(input_thumbs = self.parent_class.phocoder_thumbnails) ⇒ Object



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 564

def phocode(input_thumbs = self.parent_class.phocoder_thumbnails)
  #puts " input_thumbs.count = #{input_thumbs.size}"
  input_thumbs = dedupe_input_thumbs(input_thumbs)
  #puts " after dedupe input_thumbs.count = #{input_thumbs.size}"
  #if self.thumbnails.count >= self.class.phocoder_thumbnails.size
  #  raise "This item already has thumbnails!"
  #  return
  #end
  
  return if input_thumbs.size == 0
  # We do this because sometimes save will get called more than once
  # during a single request
  return if phocoding
  phocoding = true
  
  Rails.logger.debug "trying to phocode for #{Phocoder.base_url} "
  Rails.logger.debug "callback url = #{callback_url}"
  response = create_phocoder_job(phocoder_params(input_thumbs))
  Rails.logger.debug "the phocode response = #{response.to_json}" if Rails.env != "test"
  #puts "the phocode response = #{response.to_json}" if Rails.env != "test"
  if ActsAsPhocodable.track_components
    job = self.encodable_jobs.new
    job.phocoder_input_id = response.body["job"]["inputs"].first["id"]
    job.phocoder_job_id = response.body["job"]["id"]
    job.phocoder_status = "phocoding"
    job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
    self.encodable_jobs << job
  end
  if ActsAsPhocodable.track_jobs
    job = self.encodable_jobs.new
    job.tracking_mode = 'job'
    job.phocoder_input_id = response.body["job"]["inputs"].first["id"]
    job.phocoder_job_id = response.body["job"]["id"]
    job.phocoder_status = "phocoding"
    job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
    self.encodable_jobs << job
  end

  self.encodable_status = "phocoding" unless self.encodable_status == "ready" # the unless clause allows new thumbs to be created on the fly without jacking with the status
  self.save #false need to do save(false) here if we're calling phocode on after_save
  response_thumbs = response.body["job"]["thumbnails"]
  Rails.logger.debug "trying to decode #{response_thumbs.size} response_thumbs = #{response_thumbs.to_json}"
  #puts "trying to decode #{response_thumbs.size} response_thumbs = #{response_thumbs.to_json}"
  create_thumbnails_from_response(response_thumbs,response.body["job"]["id"])
end

#phocode_compositeObject



731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 731

def phocode_composite
  #if self.thumbnails.count >= self.class.phocoder_thumbnails.size
  #  raise "This item already has thumbnails!"
  #  return
  #end
  
  # We do this because sometimes save will get called more than once
  # during a single request
  return if phocoding
  phocoding = true
  run_callbacks :phocode_composite do
    destroy_thumbnails
    Rails.logger.debug "trying to phocode for #{Phocoder.base_url} "
    Rails.logger.debug "callback url = #{callback_url}"
    response = create_phocoder_job(phocoder_composite_params)
    Rails.logger.debug "composite response = #{response.body.to_json}"
    #puts "composite response = #{response.body.to_json}"
    if ActsAsPhocodable.track_components
      job = self.encodable_jobs.new
      job.phocoder_output_id = response.body["job"]["composite"]["id"]
      job.phocoder_job_id = response.body["job"]["id"]
      job.phocoder_status = "phocoding"
      job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
      self.encodable_jobs << job
    end
    if ActsAsPhocodable.track_jobs
      job = self.encodable_jobs.new
      job.tracking_mode = 'job'
      job.phocoder_output_id = response.body["job"]["composite"]["id"]
      job.phocoder_job_id = response.body["job"]["id"]
      job.phocoder_status = "phocoding"
      job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
      self.encodable_jobs << job
    end
    self.encodable_status = "phocoding"
    self.save #false need to do save(false) here if we're calling phocode on after_save
    response_thumbs = response.body["job"]["thumbnails"]
    Rails.logger.debug "trying to decode #{response_thumbs.size} response_thumbs = #{response_thumbs.to_json}"
    #puts "trying to decode #{response_thumbs.size} response_thumbs = #{response_thumbs.to_json}"
    create_thumbnails_from_response(response_thumbs,response.body["job"]["id"])
  end
end

#phocode_hdrObject



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 610

def phocode_hdr
  #if self.thumbnails.count >= self.class.phocoder_thumbnails.size
  #  raise "This item already has thumbnails!"
  #  return
  #end
  
  # We do this because sometimes save will get called more than once
  # during a single request
  return if phocoding
  phocoding = true
  run_callbacks :phocode_hdr do
    Rails.logger.debug "trying to phocode for #{Phocoder.base_url} "
    Rails.logger.debug "callback url = #{callback_url}"
    response = create_phocoder_job(phocoder_hdr_params)
    Rails.logger.debug "the response from phocode_hdr = #{response.body.to_json}"
    if ActsAsPhocodable.track_components
      job = self.encodable_jobs.new
      job.phocoder_output_id = response.body["job"]["hdr"]["id"]
      job.phocoder_job_id = response.body["job"]["id"]
      job.phocoder_status = "phocoding"
      job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
      self.encodable_jobs << job
    end
    if ActsAsPhocodable.track_jobs
      job = self.encodable_jobs.new
      job.tracking_mode = 'job'
      job.phocoder_output_id = response.body["job"]["hdr"]["id"]
      job.phocoder_job_id = response.body["job"]["id"]
      job.phocoder_status = "phocoding"
      job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
      self.encodable_jobs << job
    end
    self.encodable_status = "phocoding"
    self.save #false need to do save(false) here if we're calling phocode on after_save
  end
 
end

#phocode_hdrhtmlObject



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 649

def phocode_hdrhtml
  #if self.thumbnails.count >= self.class.phocoder_thumbnails.size
  #  raise "This item already has thumbnails!"
  #  return
  #end
  
  # We do this because sometimes save will get called more than once
  # during a single request
  return if phocoding
  phocoding = true
  run_callbacks :phocode_hdrhtml do
    Rails.logger.debug "trying to phocode for #{Phocoder.base_url} "
    Rails.logger.debug "callback url = #{callback_url}"
    response = create_phocoder_job(phocoder_hdrhtml_params)
    Rails.logger.debug "the response from phocode_hdrhtml = #{response.body.to_json}"
    if ActsAsPhocodable.track_components
      job = self.encodable_jobs.new
      job.phocoder_output_id = response.body["job"]["hdrhtml"]["id"]
      job.phocoder_job_id = response.body["job"]["id"]
      job.phocoder_status = "phocoding"
      job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
      self.encodable_jobs << job
    end
    if ActsAsPhocodable.track_jobs
      job = self.encodable_jobs.new
      job.tracking_mode = 'job'
      job.phocoder_output_id = response.body["job"]["hdrhtml"]["id"]
      job.phocoder_job_id = response.body["job"]["id"]
      job.phocoder_status = "phocoding"
      job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
      self.encodable_jobs << job
    end
    self.encodable_status = "phocoding"
    self.save #false need to do save(false) here if we're calling phocode on after_save
  end
 
end

#phocode_tone_mappingObject



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 688

def phocode_tone_mapping
  #if self.thumbnails.count >= self.class.phocoder_thumbnails.size
  #  raise "This item already has thumbnails!"
  #  return
  #end
  
  # We do this because sometimes save will get called more than once
  # during a single request
  return if phocoding
  phocoding = true
  run_callbacks :phocode_tone_mapping do
    destroy_thumbnails
    Rails.logger.debug "trying to phocode for #{Phocoder.base_url} "
    Rails.logger.debug "callback url = #{callback_url}"
    response = create_phocoder_job(phocoder_tone_mapping_params)
    Rails.logger.debug "tone_mapping response = #{response.body.to_json}"
    #puts "tone_mapping response = #{response.body.to_json}"
    if ActsAsPhocodable.track_components
      job = self.encodable_jobs.new
      job.phocoder_output_id = response.body["job"]["tone_mapping"]["id"]
      job.phocoder_job_id = response.body["job"]["id"]
      job.phocoder_status = "phocoding"
      job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
      self.encodable_jobs << job
    end
    if ActsAsPhocodable.track_jobs
      job = self.encodable_jobs.new
      job.tracking_mode = 'job'
      job.phocoder_output_id = response.body["job"]["tone_mapping"]["id"]
      job.phocoder_job_id = response.body["job"]["id"]
      job.phocoder_status = "phocoding"
      job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
      self.encodable_jobs << job
    end
    self.encodable_status = "phocoding"
    self.save #false need to do save(false) here if we're calling phocode on after_save
    response_thumbs = response.body["job"]["thumbnails"]
    Rails.logger.debug "trying to decode #{response_thumbs.size} response_thumbs = #{response_thumbs.to_json}"
    create_thumbnails_from_response(response_thumbs,response.body["job"]["id"])
  end
end

#phocoder_composite_paramsObject



861
862
863
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 861

def phocoder_composite_params
  { }
end

#phocoder_extensionObject



821
822
823
824
825
826
827
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 821

def phocoder_extension
  if self.content_type.blank?
    ".jpg"
  else
    self.content_type.match(/png/) ? ".png" : ".jpg"
  end
end

#phocoder_hdr_paramsObject



849
850
851
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 849

def phocoder_hdr_params
  { }
end

#phocoder_hdrhtml_paramsObject



853
854
855
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 853

def phocoder_hdrhtml_params
  { }
end

#phocoder_params(input_thumbs = self.parent_class.phocoder_thumbnails) ⇒ Object



829
830
831
832
833
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 829

def phocoder_params(input_thumbs = self.parent_class.phocoder_thumbnails)
  {:input => {:url => self.public_url, :notifications=>[{:url=>callback_url }] },
    :thumbnails => phocoder_thumbnail_params(input_thumbs)
  }
end

#phocoder_thumbnail_params(input_thumbs = self.parent_class.phocoder_thumbnails) ⇒ Object



835
836
837
838
839
840
841
842
843
844
845
846
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 835

def phocoder_thumbnail_params(input_thumbs = self.parent_class.phocoder_thumbnails)
  input_thumbs.map{|thumb|
    thumb_filename = thumb[:label] + "_" + File.basename(self.filename,File.extname(self.filename)) + phocoder_extension 
    base_url = ActsAsPhocodable.storeage_mode == "s3" ? "s3://#{self.s3_bucket_name}/#{self.thumbnail_resource_dir}/" : ""
    th = thumb.clone
    th[:base_url] = base_url  if !base_url.blank?
    th.merge({
      :filename=>thumb_filename,
      :notifications=>[{:url=>thumbnail_callback_url }]
    })
  }
end

#phocoder_tone_mapping_paramsObject



857
858
859
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 857

def phocoder_tone_mapping_params
  { }
end

#public_filenameObject



1170
1171
1172
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1170

def public_filename
  public_url
end

#public_urlObject

This should generate a fully qualified something-something type of a reference. Depending on storeage_mode/base_url settings.



1161
1162
1163
1164
1165
1166
1167
1168
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1161

def public_url
  #puts "our base_url = #{base_url} and our local_url = #{local_url}"
  if ActsAsPhocodable.storeage_mode == "local" or ActsAsPhocodable.storeage_mode == "offline" 
    base_url + local_url
  else
    self.class.cloudfront_host.present? ? cloudfront_url : s3_url
  end
end

#ready?Boolean

Returns:

  • (Boolean)


461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 461

def ready?
  if ActsAsPhocodable.storeage_mode == "offline"
    true
  #elsif image?
  #  return phocoder_status=='ready'
  #elsif video?
  #  return zencoder_status=='ready'  
  #else
  #  return false
  else
    return encodable_status == "ready"
  end
end

#recodeObject



454
455
456
457
458
459
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 454

def recode
  reload #make sure that we have current thumbs
  destroy_thumbnails
  reload
  encode
end

#remove_local_fileObject



1119
1120
1121
1122
1123
1124
1125
1126
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1119

def remove_local_file
  if local_path and File.exists? local_path
    FileUtils.rm local_path
    if Dir.glob(File.join(local_dir,"*")).size == 0
      FileUtils.rmdir local_dir 
    end
  end
end

#remove_s3_fileObject



1275
1276
1277
1278
1279
1280
1281
1282
1283
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1275

def remove_s3_file
  #puts "trying to delete #{s3_key} #{s3_bucket_name}"
  #if ActsAsPhocodable.storeage_mode == "s3"
    #AWS::S3::S3Object.delete s3_key, s3_bucket_name
  #end
  s3_obj.delete
rescue Exception => e
  #this probably means that the file never made it to S3
end

#resource_dirObject



1139
1140
1141
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1139

def resource_dir
  File.join(self.class.table_name, path_id.to_s )
end

#s3Object



1239
1240
1241
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1239

def s3
  @s3 ||= AWS::S3.new
end

#s3_bucket_nameObject



1219
1220
1221
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1219

def s3_bucket_name
  self.class.s3_bucket_name
end

#s3_keyObject



1211
1212
1213
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1211

def s3_key
  filename.blank? ? nil : File.join(resource_dir,filename)
end

#s3_objObject



1243
1244
1245
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1243

def s3_obj
  s3.buckets[s3_bucket_name].objects[s3_key]
end

#s3_urlObject



1215
1216
1217
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1215

def s3_url
  "http://#{s3_bucket_name}.s3.amazonaws.com/#{s3_key}"
end

#sanitize_filename(filename) ⇒ Object



1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1290

def sanitize_filename(filename)
  return unless filename
  filename.strip.tap do |name|
    # NOTE: File.basename doesn't work right with Windows paths on Unix
    # get only the filename, not the whole path
    name.gsub! /^.*(\\|\/)/, ''
    
    # Finally, replace all non alphanumeric, underscore or periods with underscore
    name.gsub! /[^A-Za-z0-9\.\-]/, '_'
  end
end

#save_local_fileObject



1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1067

def save_local_file
  return if @saved_file.blank?
  #puts "saving the local file!!!!!!"
  Rails.logger.debug "==================================================================================================="
  Rails.logger.debug "about to save the local file"
  run_callbacks :file_saved do
    run_callbacks :local_file_saved do
      FileUtils.mkdir_p local_dir
      FileUtils.cp @saved_file.path, local_path
      FileUtils.chmod 0755, local_path
      self.encodable_status = "local"
      if self.respond_to? :upload_host      
        self.upload_host = %x{hostname}.strip
      end
      @saved_file = nil
      @saved_a_new_file = true
      self.save
    end
    if ActsAsPhocodable.storeage_mode == "s3" && ActsAsPhocodable.processing_mode == "automatic"
      self.save_s3_file
    end
    if ActsAsPhocodable.storeage_mode == "s3" && ActsAsPhocodable.processing_mode == "spawn" && defined?(Spawn)
      spawn do # :method => :thread # <-- I think that should be set at the config/environment level
        Rails.logger.debug "------------beginning of spawn block"
        #puts               "------------beginning of spawn block"
        self.save_s3_file
        Rails.logger.debug "------------end of spawn block"
        #puts               "------------end of spawn block"
      end
    end
    if ActsAsPhocodable.storeage_mode == "local" && ActsAsPhocodable.processing_mode == "automatic" 
      self.encode
    end
    
  end
end

#save_s3_fileObject



1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1247

def save_s3_file
  #I don't think we need this return check anymore.  
  #return if !@saved_a_new_file
  #@saved_a_new_file = false
  #AWS::S3::S3Object.store(
                          #s3_key, 
                          #open(local_path),
  #s3_bucket_name,
  #:access => :public_read,
  #"Cache-Control" => 'max-age=315360000'
  #)
  s3_obj.write(Pathname.new(local_path),{:acl=>:public_read,"Cache-Control"=>'max-age=315360000'})
  self.encodable_status = "s3"
  self.save
  #obj_data = AWS::S3::S3Object.find(s3_key,s3_bucket_name)
  Rails.logger.debug "----------------------------------------------------------------------------------------------------"
  if s3_obj.content_length == file_size # it made it into s3 safely
    Rails.logger.debug " we are about to remove local file!"
    remove_local_file
  else
    msg = "The file was not saved to S3 sucessfully.  Orig size: #{file_size} - S3 size: #{obj_data.size}"
    Rails.logger.debug msg
    raise ActsAsPhocodable::Error.new msg
  end
  self.encode
end

#save_url(url) ⇒ Object



979
980
981
982
983
984
985
986
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 979

def save_url(url)
  Rails.logger.debug "We are about to download : #{url} to #{local_dir} - #{local_path}"
  FileUtils.mkdir_p(local_dir) if !File.exists?(local_dir)
  FileUtils.touch local_path
  writeOut = open(local_path, "wb")
  writeOut.write(open(url).read)
  writeOut.close
end

#thumbnail_attributes_for(thumbnail_name) ⇒ Object



1004
1005
1006
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1004

def thumbnail_attributes_for(thumbnail_name)
  self.class.thumbnail_attributes_for(thumbnail_name)
end

#thumbnail_callback_urlObject



1183
1184
1185
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1183

def thumbnail_callback_url
  self.base_url + self.thumbnail_notification_callback_path
end

#thumbnail_for(thumbnail_hash_or_name) ⇒ Object



1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1008

def thumbnail_for(thumbnail_hash_or_name)
  thumbnail_name = thumbnail_hash_or_name.is_a?(Hash) ? thumbnail_hash_or_name[:label] : thumbnail_hash_or_name 
  if thumbnail_name and thumbnail_name.match ActsAsPhocodable.size_string_regex
    thumbnail_name = self.class.create_label_from_size_string(thumbnail_name)
  end
  if thumbnail_name.blank? and thumbnail_hash_or_name.is_a?(Hash)
    thumbnail_name = "#{thumbnail_hash_or_name[:width]}x#{thumbnail_hash_or_name[:height]}"
    #puts "thumbnail_name = #{thumbnail_name}"
    thumbnail_hash_or_name[:label] = thumbnail_name
  end
  thumb = thumbnails.find_by_thumbnail(thumbnail_name)
  if thumb.blank? and ActsAsPhocodable.storeage_mode == "offline"
    thumb = self
  elsif thumb.blank? and thumbnail_hash_or_name.is_a? Hash
    thumb = self.phocode([thumbnail_hash_or_name]).first
  elsif thumb.blank? and thumbnail_hash_or_name.is_a?(String) and thumbnail_hash_or_name.match ActsAsPhocodable.label_size_regex
    atts = create_atts_from_size_string(thumbnail_name)
    thumb = self.phocode([atts]).first
  end
  if thumb.blank?
    raise ThumbnailNotFoundError.new("No thumbnail was found for label '#{thumbnail_name}'")
  end
  thumb
  #a dirty hack for now to keep things working.  
  #Remove this!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  #Go back to just returning the thumb
  #thumb.blank? ? self : thumb
end

#thumbnail_notification_callback_pathObject



1199
1200
1201
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1199

def thumbnail_notification_callback_path
  "/phocoder/phocoder_notifications/#{self.thumbnail_class.name}/#{self.id}.json"
end

#thumbnail_resource_dirObject



1143
1144
1145
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1143

def thumbnail_resource_dir
  File.join(self.thumbnail_class.table_name, path_id.to_s )
end

#unused_s3_demo_stuffObject



1232
1233
1234
1235
1236
1237
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1232

def unused_s3_demo_stuff
  s3 = AWS::S3.new
  key,bucket = get_s3_key_and_bucket
  obj = s3.buckets[bucket].objects[key]
  obj.write(Pathname.new(tmpFile),{:acl=>:public_read,"Cache-Control"=>'max-age=315360000'})
end

#update_zencoder_outputs(details) ⇒ Object



937
938
939
940
941
942
943
944
945
946
947
948
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 937

def update_zencoder_outputs(details)
  details.body["job"]["output_media_files"].each do |output|
    #puts "updating for output = #{output.to_json}"
    job = EncodableJob.find_by_zencoder_output_id output["id"]
    thumb = job.encodable
    thumb.width = output["width"]
    thumb.height = output["height"]
    thumb.file_size = output["file_size_bytes"]
    thumb.duration_in_ms = output["duration_in_ms"]
    thumb.save
  end
end

#uploaded_data=(data) ⇒ Object

compatability method for attachment_fu



1063
1064
1065
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1063

def uploaded_data=(data)
  self.file = data
end

#video?Boolean

Returns:

  • (Boolean)


442
443
444
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 442

def video?
  self.class.video?(content_type)
end

#web_safe?Boolean

Returns:

  • (Boolean)


438
439
440
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 438

def web_safe?
  self.class.web_safe?(content_type)
end

#zencodeObject



775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 775

def zencode
  # We do this because sometimes save will get called more than once
  # during a single request
  return if @zencoding
  @zencoding = true
  
  Rails.logger.debug "trying to zencode!!!!!"
  Rails.logger.debug "callback url = #{callback_url}"
  response = Zencoder::Job.create(zencoder_params)
  Rails.logger.debug "response from Zencoder = #{response.body.to_json}"
  if ActsAsPhocodable.track_components
    job = self.encodable_jobs.new
    job.zencoder_job_id = response.body["id"]
    job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
    self.encodable_jobs << job
  end
  if ActsAsPhocodable.track_jobs
    job = self.encodable_jobs.new
    job.tracking_mode = 'job'
    job.zencoder_job_id = response.body["id"]
    job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
    self.encodable_jobs << job
  end

  response.body["outputs"].each do |output_params|
    thumb = self.thumbnails.new()
    thumb.thumbnail = output_params["label"]
    
    if ActsAsPhocodable.track_components
      tjob = thumb.encodable_jobs.new
      tjob.zencoder_output_id = output_params["id"]
      tjob.zencoder_url = output_params["url"]
      tjob.zencoder_job_id = response.body["id"]
      tjob.zencoder_status  =  "zencoding"
      tjob.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
      thumb.encodable_jobs << tjob
    end
    self.thumbnails << thumb
    thumb.encodable_status = "zencoding"
    thumb.save
    #puts "    thumb.errors = #{thumb.errors.to_json}"
  end
  
  self.save
end

#zencoder_callback_urlObject



1179
1180
1181
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1179

def zencoder_callback_url
  self.base_url + self.zencoder_notification_callback_path
end

#zencoder_notification_callback_pathObject



1195
1196
1197
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1195

def zencoder_notification_callback_path
  "/phocoder/zencoder_notifications/#{self.class.name}/#{self.id}.json"
end

#zencoder_paramsObject



865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 865

def zencoder_params
  base_url = ActsAsPhocodable.storeage_mode == "s3" ? "s3://#{self.s3_bucket_name}/#{self.thumbnail_resource_dir}/" : ""
  params = {:input =>  self.public_url ,
    :outputs => self.class.zencoder_videos.map{|video|
      vid_filename = self.new_zencoder_filename( video[:video_codec] )
      vid = video.clone
      if vid[:thumbnails] and vid[:thumbnails].is_a? Array
        vid[:thumbnails].each do |thumb|
          thumb[:base_url] = base_url if !base_url.blank?
        end
      elsif vid[:thumbnails]
        vid[:thumbnails][:base_url] = base_url  if !base_url.blank?
      end        
      
      vid[:base_url] = base_url  if !base_url.blank?
      vid.merge({
        :filename=>vid_filename,
        :public=>1,
        :notifications=>[{:url=>zencoder_thumbnail_callback_url }]
      })
    }
  }
  params[:outputs][0][:thumbnails] = { :number=>1, :start_at_first_frame=>1,:public=>1 }
  params[:outputs][0][:thumbnails][:base_url] = base_url  if !base_url.blank?
  Rails.logger.debug "for zencoder the params = #{params.to_json}"
  #puts "for zencoder the params = #{params.to_json}"
  params
end

#zencoder_thumbnail_callback_urlObject



1187
1188
1189
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1187

def zencoder_thumbnail_callback_url
  self.base_url + self.zencoder_thumbnail_notification_callback_path
end

#zencoder_thumbnail_notification_callback_pathObject



1203
1204
1205
# File 'lib/phocoder_rails/acts_as_phocodable.rb', line 1203

def zencoder_thumbnail_notification_callback_path
  "/phocoder/zencoder_notifications/#{self.thumbnail_class.name}/#{self.id}.json"
end