Class: FileAsset

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/file_asset.rb

Direct Known Subclasses

Image, Swf, TextFile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, options = {}) ⇒ FileAsset

Returns a new instance of FileAsset.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/file_asset.rb', line 113

def initialize(attributes = {}, options={})
  attributes ||= {}

  base_path = attributes.delete(:base_path)
  @type, directory, name, data = attributes.values_at(:type, :directory, :name, :data)
  base_path ||= data.original_filename if data.respond_to?(:original_filename)

  directory, name = FileAsset.split_path(base_path) if base_path and name.blank?
  directory.gsub!(Rails.root.to_s,'')

  @type ||= FileAsset.type_for(name) if name
  @type = "TextFile" if @type.nil?
  @name = name

  data = StringIO.new(data) if data.is_a?(String)

  super attributes.merge(:directory => directory, :name => name, :data => data)
end

Class Method Details

.acceptable?(name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/file_asset.rb', line 76

def acceptable?(name)
  valid_extensions.include?(File.extname(name))
end

.all_valid_extensionsObject



102
103
104
# File 'app/models/file_asset.rb', line 102

def all_valid_extensions
  all_subclasses.map { |k| k.valid_extensions }.flatten.uniq
end

.split_path(path) ⇒ Object



106
107
108
109
110
# File 'app/models/file_asset.rb', line 106

def split_path(path)
  directory, name = ::File.split(path)
  directory = nil if directory == '.'
  [directory, name]
end

.type_by_extension(extension) ⇒ Object



85
86
87
88
89
# File 'app/models/file_asset.rb', line 85

def type_by_extension(extension)
  klass = all_subclasses.detect{ |k| k.valid_extensions.include?(extension) }
  klass = TextFile if klass.nil?
  klass
end

.type_for(name) ⇒ Object



80
81
82
83
# File 'app/models/file_asset.rb', line 80

def type_for(name)
  classes = all_subclasses.uniq
  classes.detect{ |k| k.acceptable?(name) }.try(:name)
end

.valid_extensionsObject



98
99
100
# File 'app/models/file_asset.rb', line 98

def valid_extensions
  read_inheritable_attribute(:valid_extensions) || []
end

.validate_extension(data, file) ⇒ Object



91
92
93
94
95
96
# File 'app/models/file_asset.rb', line 91

def validate_extension(data, file)
  if file.name && !file.class.valid_extensions.include?(File.extname(file.name))
    types = all_valid_extensions.map{ |type| type.gsub(/^\./, '') }.join(', ')
    "#{file.name} is not a valid file type. Valid file types are #{types}."
  end
end

Instance Method Details

#basenameObject



170
171
172
# File 'app/models/file_asset.rb', line 170

def basename
  data_file_name.gsub(/\.#{extname}$/, "")
end

#extnameObject



174
175
176
# File 'app/models/file_asset.rb', line 174

def extname
  File.extname(self.data_file_name).gsub(/^\.+/, '')
end

#get_contentsObject



194
195
196
197
# File 'app/models/file_asset.rb', line 194

def get_contents
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
  file_support.get_contents(File.join(file_support.root, self.directory, self.data_file_name))
end

#is_secured?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'app/models/file_asset.rb', line 132

def is_secured?
  self.protected_with_capability?('download')
end

#move(new_parent_path) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/file_asset.rb', line 199

def move(new_parent_path)
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)

  if ErpTechSvcs::Config.file_storage == :filesystem and !self.directory.include?(Rails.root.to_s)
    old_path = File.join(Rails.root, self.directory, self.name)
  else
    old_path = File.join(self.directory, self.name)
  end

  result, message = file_support.save_move(old_path, new_parent_path)
  if result
    dir = new_parent_path.gsub(Regexp.new(Rails.root.to_s), '') # strip rails root from new_parent_path, we want relative path
    dir = '/' + dir unless dir.match(%r{^/})
    self.directory = dir
    self.save
  end

  return result, message
end

#pathObject

returns full path to local image or url to s3 image



142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/file_asset.rb', line 142

def path
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)

  if ErpTechSvcs::Config.file_storage == :s3
    file_path = File.join(self.directory,self.name).sub(%r{^/},'')
    options = {}
    options[:expires] = ErpTechSvcs::Config.s3_url_expires_in_seconds if self.is_secured?
    return file_support.bucket.objects[file_path].url_for(:read, options).to_s
  else
    return File.join(Rails.root, self.directory, self.name)
  end
end

#save_dimensionsObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/file_asset.rb', line 155

def save_dimensions 
  if type == 'Image'
    begin
      f = Paperclip::Geometry.from_file(self.path)
      w = f.width.to_i
      h = f.height.to_i
      update_attribute(:width, w) if width != w
      update_attribute(:height, h) if height != h
    rescue Exception=>ex
      Rails.logger.error('Could not save width and height of image. Make sure Image Magick and the identify command are accessible')
    end
  end
  #return true
end

#set_content_typeObject



182
183
184
185
186
187
188
# File 'app/models/file_asset.rb', line 182

def set_content_type
  unless @type.nil?
    klass = @type.constantize
    content_type = klass == Image ? "image/#{File.extname(@name).gsub(/^\.+/, '')}" : klass.content_type
    self.data.instance_write(:content_type, content_type)
  end
end

#set_data_file_nameObject



190
191
192
# File 'app/models/file_asset.rb', line 190

def set_data_file_name
  update_attribute :data_file_name, name if data_file_name != name
end

#set_stiObject



178
179
180
# File 'app/models/file_asset.rb', line 178

def set_sti
  update_attribute :type, @type
end

#urlObject

compass file download url



137
138
139
# File 'app/models/file_asset.rb', line 137

def url
  "/download/#{self.name}?path=#{self.directory}"
end