module CloudhdrHelper def preview_reload_timeout #time in ms between preview reloading 10000 end #def cloudhdr_includes #tag = javascript_include_tag 'cloudhdr.js' #tag += "\n" #tag += stylesheet_link_tag 'cloudhdr.css' #end 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 # for now we'll assume that a thumbnail is needed # some files aren't displayable in a native way (NEF etc...) # 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 # 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 # 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 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 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 # def display_image(image_upload,options={}) # if ActsAsCloudhdr.storeage_mode == "offline" # offline_cloudhdr_image_thumbnail(image_upload,image_upload,options) # end # end 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 def find_or_create_thumbnail(image_upload,thumbnail="small",options={}) if thumbnail.is_a? String thumb = image_upload.thumbnail_for(thumbnail) end thumb end # # image = the original upload # # thumbnail = a record representing the dimensions of the thumbnail # # options = some other stuff # def display_image_thumbnail(image_upload,thumbnail,options) # puts "Thumbnail = #{thumbnail.to_json}" # if ActsAsCloudhdr.storeage_mode == "offline" # offline_cloudhdr_image_thumbnail(image_upload,thumbnail,options) # elsif thumbnail.cloudhdr_status != "ready" # pending_cloudhdr_thumbnail(image_upload,thumbnail,false,options) # else # image_tag thumbnail.public_url, {:width => thumbnail[:width],:height => thumbnail[:height]}.merge(options) # end # end # # A special handler when the mode is 'offline' # # The thumbnail record will contain the proper dimensions, but the path will be no good. # # This combines the path of the original with the dimensions of the original and serves from the local store. # def offline_cloudhdr_image_thumbnail(photo,thumbnail_atts,options={}) # image_tag photo.local_url, {:width => thumbnail_atts[:width],:height => thumbnail_atts[:height]}.merge(options) # #if thumbnail_atts.blank? # # image_tag photo.local_url, options # #elsif thumbnail_atts[:aspect_mode] == "stretch" # # # #else # # "
#{image_tag(photo.local_url,{:width => thumbnail_atts[:width]}.merge(options))}
".html_safe # #end # end # Thumbnail should either be an ActiveRecord or a Hash 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 # def error_cloudhdr_thumbnail(photo,thumbnail,options,spinner='error') # width = thumbnail.try :width # height = thumbnail.try :height # tag = image_tag "#{spinner}.gif", :size=>"#{width}x#{height}" # end # # for now we'll assume that a thumbnail is needed # # some files aren't displayable in a native way (NEF etc...) # # # def old_cloudhdr_image_thumbnail(image_upload,thumbnail="small",options={}) # puts "thumbnail = #{thumbnail}" # thumbnail_atts = image_upload.class.thumbnail_attributes_for thumbnail # if ActsAsCloudhdr.storeage_mode == "offline" and (thumbnail.blank? or !thumbnail_atts.blank?) # return offline_cloudhdr_thumbnail(image_upload,thumbnail_atts,options) # elsif thumbnail.nil? and (image_upload.cloudhdr_status == "ready") # return image_tag image_upload.public_url, {:size=>"#{image_upload.width}x#{image_upload.height}"}.merge(options) # elsif thumbnail_atts.blank? # return error_div("'#{thumbnail}' is not a valid thumbnail size for #{image_upload.class}") # elsif image_upload.cloudhdr_status != "ready" #and image_upload.zencoder_status != "ready" # puts "image_upload is not ready!!!!!!!!!!!!!!!!!!!!!!!!" # return pending_cloudhdr_thumbnail(image_upload,thumbnail,false,thumbnail_atts) # #else # # return "
Online mode is coming soon!
" # end # # thumb = image_upload.thumbnail_for(thumbnail) # if thumb.blank? or thumb.cloudhdr_status != "ready" # puts "thumb (#{thumb.to_json}) is not ready!!!!!!!!!!!!!!!!!!!!!!!!" # #this happens if the main image has been notified, but not this thumbnail # return pending_cloudhdr_thumbnail(image_upload,thumbnail,false,thumbnail_atts) # end # image_tag thumb.public_url, {:size=>"#{thumb.width}x#{thumb.height}"}.merge(options) # end def error_div(msg) %[
#{msg}
].html_safe end #def jquery_updater(photo,thumbnail,random) # %[ # # ] #end #def prototype_updater(photo,thumbnail,random) # %[ # # ] #end end