Module: Attacheable

Includes:
FileNaming, Uploading
Defined in:
lib/attacheable/uploading.rb,
lib/attacheable/file_naming.rb,
lib/attacheable/photo_handler.rb,
lib/attacheable.rb

Defined Under Namespace

Modules: ClassMethods, FileNaming, Uploading Classes: PhotoHandler

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FileNaming

#attachment_basename, #attachment_extname, #base_path, #full_filename_with_creation, #full_filename_without_creation, #public_filename_without_creation, #sanitize_filename, #thumbnail_name_for

Methods included from Uploading

#accepts_file_type_for_upload?, #handle_uploaded_file, #identify_image_properties, #identify_uploaded_file_type, #prepare_merb_uploaded_file, #prepare_uploaded_file, #save_to_replicas, #save_to_storage

Class Method Details

.included(base) ⇒ Object

:nodoc:



66
67
68
69
70
71
# File 'lib/attacheable.rb', line 66

def self.included(base) #:nodoc:
  base.before_update :rename_file
  base.after_save :save_to_storage
  base.after_destroy :remove_files
  base.extend(ClassMethods)
end

.rootObject



74
75
76
77
78
# File 'lib/attacheable.rb', line 74

def self.root
  return RAILS_ROOT if defined?(RAILS_ROOT)
  return Merb.root if defined?(Merb)
  return File.dirname(__FILE__)+"/../.."
end

Instance Method Details

#attachment_optionsObject

:nodoc:



140
141
142
# File 'lib/attacheable.rb', line 140

def attachment_options #:nodoc:
  self.class.attachment_options
end

#destroy_thumbnails!(thumbnail = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/attacheable.rb', line 81

def destroy_thumbnails!(thumbnail = nil)
  return if filename.blank?
  if thumbnail
    FileUtils.rm_f(full_filename_without_creation(thumbnail))
  else
    to_remove = Dir["#{File.dirname(full_filename_without_creation)}/*"] - [full_filename_without_creation]
    FileUtils.rm_f(to_remove)
  end
end

#filename=(value) ⇒ Object



256
257
258
259
# File 'lib/attacheable.rb', line 256

def filename=(value)
  @old_filename = full_filename unless filename.nil? || @old_filename
  write_attribute :filename, sanitize_filename(value)
end

#full_filename(thumbnail = nil) ⇒ Object

Returns real path to original file if thumbnail is nil or path with thumbnail part inserted If options is set to true, this method will autogenerate thumbnail



148
149
150
151
# File 'lib/attacheable.rb', line 148

def full_filename(thumbnail = nil)
  return "" if filename.blank?
  attachment_options[:autocreate] ? full_filename_with_creation(thumbnail) : full_filename_without_creation(thumbnail)
end

#full_filename_by_path(path) ⇒ Object

:nodoc:



153
154
155
156
157
158
159
# File 'lib/attacheable.rb', line 153

def full_filename_by_path(path) #:nodoc:
  return if filename.blank?
  thumbnail = path.gsub(%r((^#{Regexp.escape(attachment_basename)}_)(\w+)(#{Regexp.escape(attachment_extname)})$), '\2')
  return unless thumbnail
  return unless attachment_options[:thumbnails][thumbnail.to_sym]
  full_filename_with_creation(thumbnail.to_sym)
end

#image_height(thumbnail = nil) ⇒ Object



173
174
175
# File 'lib/attacheable.rb', line 173

def image_height(thumbnail = nil)
  self.class.image_height(full_filename(thumbnail))
end

#image_sizeObject



252
253
254
# File 'lib/attacheable.rb', line 252

def image_size
  [width.to_s, height.to_s] * 'x'
end

#image_width(thumbnail = nil) ⇒ Object



169
170
171
# File 'lib/attacheable.rb', line 169

def image_width(thumbnail = nil)
  self.class.image_width(full_filename(thumbnail))
end

#public_filename(thumbnail = nil) ⇒ Object

Gets the public path to the file, visible to browser The optional thumbnail argument will output the thumbnail’s filename. If options is set to true, this method will autogenerate thumbnail



164
165
166
167
# File 'lib/attacheable.rb', line 164

def public_filename(thumbnail = nil)
  return "" if filename.blank?
  (full_filename(thumbnail) || "").gsub %r(^#{Regexp.escape(base_path)}), ''
end

#public_filename_with_download(*args) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/attacheable.rb', line 261

def public_filename_with_download(*args)
  filename = public_filename_without_download(*args)
  return filename if File.exists?(RAILS_ROOT + "/public/" + filename)
  return filename if attachment_options[:production_host].blank?
  FileUtils.mkdir_p(File.dirname(full_filename))
  begin
    image = open("http://#{attachment_options[:production_host]}"+public_filename).read
    File.open(full_filename, "w+") do |f|
      f << image
    end
  rescue Exception => e
  end
  public_filename_without_download(*args)
end

#source_url=(url) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/attacheable.rb', line 233

def source_url=(url)
  return if url.blank?
  http_getter = Net::HTTP
  uri = URI.parse(url)
  response = http_getter.start(uri.host, uri.port) {|http| http.get(uri.path) }
  case response
  when Net::HTTPSuccess
    file_data = response.body
    return nil if file_data.nil? || file_data.size == 0
    filename = url.split("/").last
    tempfile = Tempfile.new(filename)
    tempfile.write(file_data)
    tempfile.close
    self.uploaded_data = {"tempfile" => tempfile, "filename" => filename, "size" => file_data.size}
  else
    return nil
  end
end

#uploaded_data=(file_data) ⇒ Object

Main method, that accepts uploaded data



225
226
227
228
229
230
231
# File 'lib/attacheable.rb', line 225

def uploaded_data=(file_data)
  prepare_uploaded_file(file_data)
  file_type = identify_uploaded_file_type
  if accepts_file_type_for_upload?(file_type)
    handle_uploaded_file
  end
end

#valid_filetype?Boolean

:nodoc:

Returns:

  • (Boolean)


219
220
221
# File 'lib/attacheable.rb', line 219

def valid_filetype? #:nodoc:
  errors.add("uploaded_data", attachment_options[:validation_message]) if @save_new_attachment && !@valid_filetype
end