Module: ActsAsCloudhdr::InstanceMethods

Defined in:
lib/cloudhdr_rails/acts_as_cloudhdr.rb

Instance Method Summary collapse

Instance Method Details

#base_urlObject



868
869
870
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 868

def base_url
  self.class.base_url
end

#calc_height(thumbnail_atts) ⇒ Object



991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 991

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



968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 968

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



852
853
854
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 852

def callback_url
  self.base_url + self.notification_callback_path
end

#cleanupObject



787
788
789
790
791
792
793
794
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 787

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

#clear_processingObject



404
405
406
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 404

def clear_processing
  processing = false
end

#cloudfront_base_hostObject



888
889
890
891
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 888

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

#cloudfront_urlObject



893
894
895
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 893

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

#cloudhdr?Boolean

Returns:

  • (Boolean)


661
662
663
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 661

def cloudhdr?
  true
end

#cloudhdr_composite_paramsObject



650
651
652
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 650

def cloudhdr_composite_params
  { }
end

#cloudhdr_configObject



657
658
659
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 657

def cloudhdr_config
  ActsAsCloudhdr.cloudhdr_config
end

#cloudhdr_extensionObject



610
611
612
613
614
615
616
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 610

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

#cloudhdr_hdr_paramsObject



638
639
640
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 638

def cloudhdr_hdr_params
  { }
end

#cloudhdr_hdrhtml_paramsObject



642
643
644
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 642

def cloudhdr_hdrhtml_params
  { }
end

#cloudhdr_params(input_thumbs = self.cloudhdr_thumbnails) ⇒ Object



618
619
620
621
622
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 618

def cloudhdr_params(input_thumbs = self.cloudhdr_thumbnails)
  {:input => {:url => self.public_url, :notifications=>[{:url=>callback_url }] },
    :thumbnails => cloudhdr_thumbnail_params(input_thumbs)
  }
end

#cloudhdr_thumbnail_params(input_thumbs = self.cloudhdr_thumbnails) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 624

def cloudhdr_thumbnail_params(input_thumbs = self.cloudhdr_thumbnails)
  input_thumbs.map{|thumb|
    thumb_filename = thumb[:label] + "_" + File.basename(self.filename,File.extname(self.filename)) + cloudhdr_extension 
    base_url = ActsAsCloudhdr.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

#cloudhdr_tone_mapping_paramsObject



646
647
648
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 646

def cloudhdr_tone_mapping_params
  { }
end

#create_atts_from_size_string(label_string) ⇒ Object



686
687
688
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 686

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

#create_cloudhdr_job(params) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 419

def create_cloudhdr_job(params)
  response = nil
  tries = 0
  max_tries = 3
  sleeps = [2,4,8]
  while response.blank? do
    begin
      response = Cloudhdr::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



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
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 371

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
    
    if self.respond_to?(:user_id) && thumb.respond_to?(:user_id=)
      thumb.user_id = self.user_id
    end
    thumb.thumbnail = thumb_params["label"]
    thumb.filename = thumb_params["filename"]
    thumb.width = thumb_params["width"]
    thumb.height = thumb_params["height"]
    thumb.cloudhdr_status = "processing"
    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

#dedupe_input_thumbs(input_thumbs) ⇒ Object



408
409
410
411
412
413
414
415
416
417
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 408

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



674
675
676
677
678
679
680
681
682
683
684
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 674

def destroy_thumbnails
  if self.class.cloudhdr_thumbnails.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



332
333
334
335
336
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 332

def encode
  if image?
    process
  end
end

#error?Boolean

Returns:

  • (Boolean)


357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 357

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

#file=(new_file) ⇒ Object



727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 727

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.



951
952
953
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 951

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

#fire_ready_callbackObject



781
782
783
784
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 781

def fire_ready_callback
  run_callbacks :file_ready do
  end
end

#get_thumbnail(thumbnail_name) ⇒ Object



723
724
725
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 723

def get_thumbnail(thumbnail_name)
  thumbnail_for(thumbnail_name)
end

#image?Boolean

Returns:

  • (Boolean)


324
325
326
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 324

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

#local_dirObject



824
825
826
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 824

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

#local_pathObject



828
829
830
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 828

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

#local_urlObject



832
833
834
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 832

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

#notification_callback_pathObject



860
861
862
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 860

def notification_callback_path
  "/cloudhdr/cloudhdr_notifications/#{self.class.name}/#{self.id}.json"
end

#path_idObject



805
806
807
808
809
810
811
812
813
814
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 805

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

#process(input_thumbs = self.cloudhdr_thumbnails) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 440

def process(input_thumbs = self.cloudhdr_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.cloudhdr_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 processing
  processing = true
  
  Rails.logger.debug "trying to process for #{Cloudhdr.base_url} "
  Rails.logger.debug "callback url = #{callback_url}"
  response = create_cloudhdr_job(cloudhdr_params(input_thumbs))
  Rails.logger.debug "the process response = #{response.to_json}" if Rails.env != "test"
  #puts "the process response = #{response.to_json}" if Rails.env != "test"
  
  job = self.cloudhdr_jobs.new
  job.tracking_mode = 'job'
  job.cloudhdr_input_id = response.body["job"]["inputs"].first["id"]
  job.cloudhdr_job_id = response.body["job"]["id"]
  job.cloudhdr_status = "processing"
  job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
  self.cloudhdr_jobs << job

  self.cloudhdr_status = "processing" unless self.cloudhdr_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 process 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

#process_compositeObject



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
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 574

def process_composite
  #if self.thumbnails.count >= self.class.cloudhdr_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 processing
  processing = true
  run_callbacks :process_composite do
    destroy_thumbnails
    Rails.logger.debug "trying to process for #{Cloudhdr.base_url} "
    Rails.logger.debug "callback url = #{callback_url}"
    response = create_cloudhdr_job(cloudhdr_composite_params)
    Rails.logger.debug "composite response = #{response.body.to_json}"
    #puts "composite response = #{response.body.to_json}"
  
    job = self.cloudhdr_jobs.new
    job.tracking_mode = 'job'
    job.cloudhdr_output_id = response.body["job"]["composite"]["id"]
    job.cloudhdr_job_id = response.body["job"]["id"]
    job.cloudhdr_status = "processing"
    job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
    self.cloudhdr_jobs << job
  
    self.cloudhdr_status = "processing"
    self.save #false need to do save(false) here if we're calling process 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

#process_hdrObject



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 477

def process_hdr
  #if self.thumbnails.count >= self.class.cloudhdr_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 processing
  processing = true
  run_callbacks :process_hdr do
    Rails.logger.debug "trying to process for #{Cloudhdr.base_url} "
    Rails.logger.debug "callback url = #{callback_url}"
    response = create_cloudhdr_job(cloudhdr_hdr_params)
    Rails.logger.debug "the response from process_hdr = #{response.body.to_json}"
    
    job = self.cloudhdr_jobs.new
    job.tracking_mode = 'job'
    job.cloudhdr_output_id = response.body["job"]["hdr"]["id"]
    job.cloudhdr_job_id = response.body["job"]["id"]
    job.cloudhdr_status = "processing"
    job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
    self.cloudhdr_jobs << job

    self.cloudhdr_status = "processing"
    self.save #false need to do save(false) here if we're calling process on after_save
  end
 
end

#process_hdrhtmlObject



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 508

def process_hdrhtml
  #if self.thumbnails.count >= self.class.cloudhdr_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 processing
  processing = true
  run_callbacks :process_hdrhtml do
    Rails.logger.debug "trying to process for #{Cloudhdr.base_url} "
    Rails.logger.debug "callback url = #{callback_url}"
    response = create_cloudhdr_job(cloudhdr_hdrhtml_params)
    Rails.logger.debug "the response from process_hdrhtml = #{response.body.to_json}"
   
    job = self.cloudhdr_jobs.new
    job.tracking_mode = 'job'
    job.cloudhdr_output_id = response.body["job"]["hdrhtml"]["id"]
    job.cloudhdr_job_id = response.body["job"]["id"]
    job.cloudhdr_status = "processing"
    job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
    self.cloudhdr_jobs << job
  
    self.cloudhdr_status = "processing"
    self.save #false need to do save(false) here if we're calling process on after_save
  end
 
end

#process_tone_mappingObject



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 539

def process_tone_mapping
  #if self.thumbnails.count >= self.class.cloudhdr_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 processing
  processing = true
  run_callbacks :process_tone_mapping do
    destroy_thumbnails
    Rails.logger.debug "trying to process for #{Cloudhdr.base_url} "
    Rails.logger.debug "callback url = #{callback_url}"
    response = create_cloudhdr_job(cloudhdr_tone_mapping_params)
    Rails.logger.debug "tone_mapping response = #{response.body.to_json}"
    #puts "tone_mapping response = #{response.body.to_json}"
    
    job = self.cloudhdr_jobs.new
    job.tracking_mode = 'job'
    job.cloudhdr_output_id = response.body["job"]["tone_mapping"]["id"]
    job.cloudhdr_job_id = response.body["job"]["id"]
    job.cloudhdr_status = "processing"
    job.user_id = self.user_id if (self.respond_to?(:user_id) && self.user_id)
    self.cloudhdr_jobs << job
  
    self.cloudhdr_status = "processing"
    self.save #false need to do save(false) here if we're calling process 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

#public_filenameObject



847
848
849
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 847

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.



838
839
840
841
842
843
844
845
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 838

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

#ready?Boolean

Returns:

  • (Boolean)


345
346
347
348
349
350
351
352
353
354
355
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 345

def ready?
  if ActsAsCloudhdr.storeage_mode == "offline"
    true
  #elsif image?
  #  return cloudhdr_status=='ready'
  #else
  #  return false
  else
    return cloudhdr_status == "ready"
  end
end

#recodeObject



338
339
340
341
342
343
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 338

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

#remove_local_fileObject



796
797
798
799
800
801
802
803
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 796

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



940
941
942
943
944
945
946
947
948
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 940

def remove_s3_file
  #puts "trying to delete #{s3_key} #{s3_bucket_name}"
  #if ActsAsCloudhdr.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



816
817
818
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 816

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

#s3Object



904
905
906
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 904

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

#s3_bucket_nameObject



884
885
886
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 884

def s3_bucket_name
  self.class.s3_bucket_name
end

#s3_keyObject



872
873
874
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 872

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

#s3_objObject



908
909
910
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 908

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

#s3_urlObject



876
877
878
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 876

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

#sanitize_filename(filename) ⇒ Object



955
956
957
958
959
960
961
962
963
964
965
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 955

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



753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 753

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.cloudhdr_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 ActsAsCloudhdr.storeage_mode == "s3" && ActsAsCloudhdr.processing_mode == "automatic"
      self.save_s3_file
    end
    if ActsAsCloudhdr.storeage_mode == "local" && ActsAsCloudhdr.processing_mode == "automatic" 
      self.encode
    end
    
  end
end

#save_s3_fileObject



912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 912

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.cloudhdr_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 ActsAsCloudhdr::Error.new msg
  end
  self.encode
end

#save_url(url) ⇒ Object



665
666
667
668
669
670
671
672
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 665

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

#signed_s3_url(options) ⇒ Object



880
881
882
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 880

def signed_s3_url options
  s3_obj.url_for(:read, :expires => 10*60).to_s
end

#thumbnail_attributes_for(thumbnail_name) ⇒ Object



690
691
692
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 690

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

#thumbnail_callback_urlObject



856
857
858
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 856

def thumbnail_callback_url
  self.base_url + self.thumbnail_notification_callback_path
end

#thumbnail_for(thumbnail_hash_or_name) ⇒ Object



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
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 694

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 ActsAsCloudhdr.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 ActsAsCloudhdr.storeage_mode == "offline"
    thumb = self
  elsif thumb.blank? and thumbnail_hash_or_name.is_a? Hash
    thumb = self.process([thumbnail_hash_or_name]).first
  elsif thumb.blank? and thumbnail_hash_or_name.is_a?(String) and thumbnail_hash_or_name.match ActsAsCloudhdr.label_size_regex
    atts = create_atts_from_size_string(thumbnail_name)
    thumb = self.process([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



864
865
866
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 864

def thumbnail_notification_callback_path
  "/cloudhdr/cloudhdr_notifications/#{self.thumbnail_class.name}/#{self.id}.json"
end

#thumbnail_resource_dirObject



820
821
822
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 820

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

#unused_s3_demo_stuffObject



897
898
899
900
901
902
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 897

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

#uploaded_data=(data) ⇒ Object

compatability method for attachment_fu



749
750
751
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 749

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

#web_safe?Boolean

Returns:

  • (Boolean)


328
329
330
# File 'lib/cloudhdr_rails/acts_as_cloudhdr.rb', line 328

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