Module: Attacheable::FileNaming

Included in:
Attacheable
Defined in:
lib/attacheable/file_naming.rb

Instance Method Summary collapse

Instance Method Details

#attachment_basenameObject



29
30
31
# File 'lib/attacheable/file_naming.rb', line 29

def attachment_basename
  filename && filename.gsub(/\.[^\.]+$/, '')
end

#attachment_extnameObject



33
34
35
# File 'lib/attacheable/file_naming.rb', line 33

def attachment_extname
  filename && filename.gsub(/^(.*)(\.[^\.]+)$/, '\2')
end

#base_pathObject

:nodoc:



25
26
27
# File 'lib/attacheable/file_naming.rb', line 25

def base_path #:nodoc:
  @base_path ||= File.join(Attacheable.root, 'public')
end

#full_filename_with_creation(thumbnail = nil) ⇒ Object

:nodoc:



3
4
5
# File 'lib/attacheable/file_naming.rb', line 3

def full_filename_with_creation(thumbnail = nil) #:nodoc:
  create_thumbnail_if_required(thumbnail) 
end

#full_filename_without_creation(thumbnail = nil) ⇒ Object

:nodoc:



7
8
9
10
# File 'lib/attacheable/file_naming.rb', line 7

def full_filename_without_creation(thumbnail = nil) #:nodoc:
  file_system_path = attachment_options[:path_prefix]
  File.join(Attacheable.root, file_system_path, *partitioned_path(thumbnail_name_for(thumbnail)))
end

#public_filename_without_creation(thumbnail = nil) ⇒ Object



21
22
23
# File 'lib/attacheable/file_naming.rb', line 21

def public_filename_without_creation(thumbnail = nil)
  full_filename_without_creation(thumbnail).gsub %r(^#{Regexp.escape(base_path)}), ''
end

#sanitize_filename(filename) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/attacheable/file_naming.rb', line 37

def sanitize_filename(filename)
  returning filename.strip 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! /[^\w\.\-]/, '_'
  end
end

#thumbnail_name_for(thumbnail = nil) ⇒ Object

:nodoc:



12
13
14
15
16
17
18
19
# File 'lib/attacheable/file_naming.rb', line 12

def thumbnail_name_for(thumbnail = nil) #:nodoc:
  return filename if thumbnail.blank?
  ext = nil
  basename = filename.gsub /\.\w+$/ do |s|
    ext = s; ''
  end
  "#{basename}_#{thumbnail}#{ext}"
end