Module: CloudhdrHelper

Defined in:
app/helpers/cloudhdr_helper.rb

Instance Method Summary collapse

Instance Method Details

#cloudhdr_image_offline(image_upload, thumbnail = "small", options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/cloudhdr_helper.rb', line 52

def cloudhdr_image_offline(image_upload,thumbnail="small",options={})
  if !image_upload.web_safe?
    error_div "#{image_upload.filename} can not be displayed directly because it is not web safe. Content type = #{image_upload.content_type}"
  elsif thumbnail.blank? 
    image_tag(image_upload.public_url,options)   
  else
    begin
      # since we're in offline mode here we don't actually have files created for the thumbnail
      # so we return an image with the path to the original, we don't care about cloudhdr_status
      thumbnail = find_thumbnail_attributes(image_upload,thumbnail,options)
      image_tag image_upload.public_url, {:width => thumbnail[:width],:height => thumbnail[:height]}.merge(options)
    rescue ActsAsCloudhdr::ThumbnailAttributesNotFoundError
      error_div "'#{thumbnail}' is not a valid thumbnail name or size string."
    end
  end
end

#cloudhdr_image_online(image_upload, thumbnail = "small", options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/cloudhdr_helper.rb', line 69

def cloudhdr_image_online(image_upload,thumbnail="small",options={})
  if thumbnail.blank? and !image_upload.web_safe?
    error_div "#{image_upload.filename} can not be displayed directly because it is not web safe. Content type = #{image_upload.content_type}"
  elsif thumbnail.blank? and !image_upload.ready?
    # don't know width and height yet
    image_tag(image_upload.public_url,options)
  elsif thumbnail.blank?
    # now we have width and height
    image_tag(image_upload.public_url,options.merge(:width => image_upload.width, :height => image_upload.height))
  elsif !image_upload.ready?
    begin 
      # We can only look for attributes now to show a pending message with the correct dimensions
      thumb_atts = find_thumbnail_attributes(image_upload,thumbnail,options)
      pending_cloudhdr_thumbnail(image_upload,thumb_atts,options)
    rescue ActsAsCloudhdr::ThumbnailAttributesNotFoundError
      error_div "'#{thumbnail}' is not a valid thumbnail name or size string."
    end
  else
    begin
      thumb = find_or_create_thumbnail(image_upload,thumbnail,options)
      if thumb.ready?
        image_tag thumb.public_url, {:width => thumb[:width],:height => thumb[:height]}.merge(options)
      else
        pending_cloudhdr_thumbnail(image_upload,thumb,options)
      end
    rescue ActsAsCloudhdr::ThumbnailNotFoundError
      error_div "'#{thumbnail}' is not a valid thumbnail name or size string."
    end
  end
end

#cloudhdr_image_thumbnail(image_upload, thumbnail = "small", options = {}) ⇒ Object

Passing a thumbnail is STRONGLY ADVISED Unless you are abosultely sure that you are only accepting web safe image formats you’ll want to supply a thumbnail arg



40
41
42
43
44
45
46
47
48
49
50
# File 'app/helpers/cloudhdr_helper.rb', line 40

def cloudhdr_image_thumbnail(image_upload,thumbnail="small",options={})
  if ActsAsCloudhdr.storeage_mode == "offline"
    cloudhdr_image_offline(image_upload,thumbnail,options)
  else
    cloudhdr_image_online(image_upload,thumbnail,options)
  end
  #return display_image(image_upload,options) if thumbnail.blank?
  #thumbnail = find_or_create_thumbnail(image_upload,thumbnail,options)
  #return display_image_thumbnail(image_upload,thumbnail,options) if thumbnail
  #return error_div "Could not find a thumbnail for: class=#{image_upload.class} thumbnail=#{thumbnail.to_json}"
end


16
17
18
19
20
21
22
# File 'app/helpers/cloudhdr_helper.rb', line 16

def cloudhdr_link(text,file)
  if file.cloudhdr_status == "ready" or file.cloudhdr_status == "s3"
    link_to text, file.public_filename
  else
    text
  end
end

#cloudhdr_thumbnail(image, thumbnail = "small", options = {}) ⇒ Object

for now we’ll assume that a thumbnail is needed some files aren’t displayable in a native way (NEF etc…)



28
29
30
31
32
33
34
# File 'app/helpers/cloudhdr_helper.rb', line 28

def cloudhdr_thumbnail(image,thumbnail="small",options={})
  if image.image?
    return cloudhdr_image_thumbnail(image,thumbnail,options)  
  else
    return error_div("This #{image.class} is not an image.")
  end
end

#error_div(msg) ⇒ Object



208
209
210
# File 'app/helpers/cloudhdr_helper.rb', line 208

def error_div(msg)
  %[<div class="cloudhdr_error">#{msg}</div>].html_safe
end

#find_or_create_thumbnail(image_upload, thumbnail = "small", options = {}) ⇒ Object



114
115
116
117
118
119
# File 'app/helpers/cloudhdr_helper.rb', line 114

def find_or_create_thumbnail(image_upload,thumbnail="small",options={})
  if thumbnail.is_a? String
    thumb = image_upload.thumbnail_for(thumbnail)
  end
  thumb
end

#find_thumbnail_attributes(image_upload, thumbnail, options) ⇒ Object

def display_image(image_upload,options={})

  if ActsAsCloudhdr.storeage_mode == "offline"
    offline_cloudhdr_image_thumbnail(image_upload,image_upload,options)
  end
end


105
106
107
108
109
110
111
112
# File 'app/helpers/cloudhdr_helper.rb', line 105

def find_thumbnail_attributes(image_upload,thumbnail,options)
   if thumbnail.is_a? String
     thumb_atts = image_upload.thumbnail_attributes_for(thumbnail)
   elsif thumbnail.is_a? Hash
     thumb_atts = thumbnail
   end
   thumb_atts
end

#pending_cloudhdr_thumbnail(photo, thumbnail, options, spinner = 'waiting') ⇒ Object

Thumbnail should either be an ActiveRecord or a Hash



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/helpers/cloudhdr_helper.rb', line 154

def pending_cloudhdr_thumbnail(photo,thumbnail,options,spinner='waiting')
  random = SecureRandom.hex(16)
  Rails.logger.debug "======================================================"
  Rails.logger.debug "building pending thumbnail for #{thumbnail.class} #{thumbnail.to_json}"
  if thumbnail.is_a? Hash
    thumb_name = thumbnail[:label]
   else
    thumb_name = thumbnail.thumbnail
  end
  width = thumbnail[:width]
  height = thumbnail[:height]
 
  elemId = "#{photo.class.to_s}_#{photo.id.to_s}_#{thumb_name}_#{random}"

  tag = image_tag "#{spinner}.gif", :size=>"#{width}x#{height}", :id => elemId, "data-cloudhdr-waiting" => true
end

#preview_reload_timeoutObject



3
4
5
6
# File 'app/helpers/cloudhdr_helper.rb', line 3

def preview_reload_timeout
  #time in ms between preview reloading
  10000
end