lib/fleximage/model.rb in fleximage-1.0.1 vs lib/fleximage/model.rb in fleximage-1.0.2

- old
+ new

@@ -267,14 +267,15 @@ # structure based on the creation date, to prevent hitting the OS imposed # limit on the number files in a directory. # # @some_image.directory_path #=> /var/www/myapp/uploaded_images/2008/3/30 def directory_path - raise 'No image directory was defined, cannot generate path' unless self.class.image_directory + directory = self.class.image_directory + raise 'No image directory was defined, cannot generate path' unless directory # base directory - directory = "#{RAILS_ROOT}/#{self.class.image_directory}" + directory = "#{RAILS_ROOT}/#{directory}" unless /^\// =~ directory # specific creation date based directory suffix. creation = self[:created_at] || self[:created_on] if self.class.use_creation_date_based_directories && creation "#{directory}/#{creation.year}/#{creation.month}/#{creation.day}" @@ -285,13 +286,32 @@ # Returns the path to the master image file for this record. # # @some_image.file_path #=> /var/www/myapp/uploaded_images/123.png def file_path - "#{directory_path}/#{id}.#{self.class.image_storage_format}" + "#{directory_path}/#{id}.#{extension}" end + + # Returns original format of the image if the image_format column exists + # otherwise returns the globally set format. + def extension + if self.respond_to?( :image_format) + case image_format + when "JPEG" + "jpg" + else + image_format ? image_format.downcase : self.class.image_storage_format + end + else + self.class.image_storage_format + end + end + def url_format + extension.to_sym + end + # Sets the image file for this record to an uploaded file. This can # be called directly, or passively like from an ActiveRecord mass # assignment. # # Rails will automatically call this method for you, in most of the @@ -403,11 +423,11 @@ # Sets the uploaded image to the name of a file in RAILS_ROOT/tmp that was just # uploaded. Use as a hidden field in your forms to keep an uploaded image when # validation fails and the form needs to be redisplayed def image_file_temp=(file_name) - if !@uploaded_image && file_name && file_name.present? + if !@uploaded_image && file_name && file_name.present? && file_name !~ %r{\.\./} @image_file_temp = file_name file_path = "#{RAILS_ROOT}/tmp/fleximage/#{file_name}" @dont_save_temp = true if File.exists?(file_path) @@ -572,11 +592,11 @@ if @uploaded_image # perform preprocessing perform_preprocess_operation # Convert to storage format - @uploaded_image.format = self.class.image_storage_format.to_s.upcase + @uploaded_image.format = self.class.image_storage_format.to_s.upcase unless respond_to?(:image_format) # Write image data to the DB field if self.class.db_store? self.image_file_data = @uploaded_image.to_blob end @@ -621,10 +641,11 @@ def clear_magic_attributes unless frozen? self.image_filename = nil if respond_to?(:image_filename=) self.image_width = nil if respond_to?(:image_width=) self.image_height = nil if respond_to?(:image_height=) + self.image_format = nil if respond_to?(:image_format=) end end # If any magic column names exists fill them with image meta data. def set_magic_attributes(file = nil) @@ -633,18 +654,19 @@ filename = file.basename if file.respond_to?(:basename) self.image_filename = filename end self.image_width = @uploaded_image.columns if self.respond_to?(:image_width=) self.image_height = @uploaded_image.rows if self.respond_to?(:image_height=) + self.image_format = @uploaded_image.format if self.respond_to?(:image_format=) end # Save the image in the rails tmp directory def save_temp_image(file) file_name = file.respond_to?(:original_filename) ? file.original_filename : file.path @image_file_temp = Time.now.to_f.to_s.sub('.', '_') path = "#{RAILS_ROOT}/tmp/fleximage" FileUtils.mkdir_p(path) - File.open("#{path}/#{@image_file_temp}", 'w') do |f| + File.open("#{path}/#{@image_file_temp}", 'wb') do |f| file.rewind f.write file.read end end