Module: Gluttonberg::Library::AttachmentMixin

Extended by:
ActiveSupport::Concern
Included in:
Asset
Defined in:
lib/gluttonberg/library/attachment_mixin.rb

Overview

The attachment mixin encapsulates the majority of logic for handling and processing uploads. It exists here in a mixin rather than in the Asset class purely because it is ultimately the intention to have a different Asset class for each major category of assets e.g. ImageAsset, DocumentAsset.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#asset_directory_public_urlObject



88
89
90
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 88

def asset_directory_public_url
  "#{assets_directory_public_url}/#{asset_hash}"
end

#asset_folder_pathObject



84
85
86
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 84

def asset_folder_path
  directory
end

#asset_processingObject

Perform asset processing



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 128

def asset_processing
  asset_id_to_process = self.id
  asset = Asset.where(:id => asset_id_to_process).first
  if asset
    asset_processors = [Library::Processor::Image , Library::Processor::Audio] #Core processors
    asset_processors << Rails.configuration.asset_processors unless Rails.configuration.asset_processors.blank? #additional processors
    asset_processors = asset_processors.flatten
    unless asset_processors.blank?
      asset_processors.each do |processor|
        processor.process(asset)
      end
    end
  end
end

#fileObject

Returns the file assigned by file=



76
77
78
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 76

def file
  @file
end

#file=(new_file) ⇒ Object

Setter for the file object. It sanatises the file name and stores in the filename property. It also sets the mime-type and size.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 53

def file=(new_file)
  unless new_file.blank?
    logger.info("\nFILENAME: #{new_file.original_filename} \n\n")

    # Forgive me this naive sanitisation, I'm still a regex n00b
    clean_filename = new_file.original_filename.split(%r{[\\|/]}).last
    clean_filename = clean_filename.gsub(" ", "_").gsub(/[^A-Za-z0-9\-_.]/, "").downcase

    # _thumb.#{file_extension} is a reserved name for the thumbnailing system, so if the user
    # has a file with that name rename it.
    if (clean_filename == '_thumb_small.#{file_extension}') || (clean_filename == '_thumb_large.#{file_extension}')
      clean_filename = 'thumb.#{file_extension}'
    end

    self.mime_type = new_file.content_type
    self.file_name = clean_filename
    self.size = new_file.size
    @file = new_file
    self.file_dirty = true
  end
end

#file_extensionObject



80
81
82
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 80

def file_extension
  file_name.split(".").last
end

#generate_cropped_image(x, y, w, h, image_type) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 117

def generate_cropped_image(x , y , w , h, image_type)
  if !File.exist?(self.tmp_location_on_disk) && !File.exist?(self.tmp_original_file_on_disk)
    self.download_asset_to_tmp_file
  end
  processor = Library::Processor::Image.new
  processor.asset = self
  processor.generate_cropped_image(x , y , w , h, image_type)
  self.remove_file_from_tmp_storage
end

#location_on_diskObject

Returns the full path to the file’s location on disk.



98
99
100
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 98

def location_on_disk
  directory + "/" + file_name
end

#original_file_on_diskObject

asset path in actual assets directory



103
104
105
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 103

def original_file_on_disk
  directory + "/original_" + file_name
end

#tmp_location_on_diskObject

Returns the full path to the file’s location on disk in tmp directory.



108
109
110
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 108

def tmp_location_on_disk
  tmp_directory + "/" + file_name
end

#tmp_original_file_on_diskObject

asset full path in tmp directory



113
114
115
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 113

def tmp_original_file_on_disk
  tmp_directory + "/original_" + file_name
end

#urlObject

Returns the public URL to this asset, relative to the domain.



93
94
95
# File 'lib/gluttonberg/library/attachment_mixin.rb', line 93

def url
  "#{asset_directory_public_url}/#{file_name}"
end