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.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/file_asset.rb', line 120

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)


83
84
85
# File 'app/models/file_asset.rb', line 83

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

.all_valid_extensionsObject



109
110
111
# File 'app/models/file_asset.rb', line 109

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

.split_path(path) ⇒ Object



113
114
115
116
117
# File 'app/models/file_asset.rb', line 113

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

.type_by_extension(extension) ⇒ Object



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

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



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

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

.valid_extensionsObject



105
106
107
# File 'app/models/file_asset.rb', line 105

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

.validate_extension(data, file) ⇒ Object



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

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



204
205
206
# File 'app/models/file_asset.rb', line 204

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

#check_name_uniquenessObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/models/file_asset.rb', line 139

def check_name_uniqueness
  # check if name is already taken
  unless FileAsset.where('directory = ? and name = ?',  self.directory, self.name).first.nil?
    # if it is keeping add incrementing by 1 until we have a good name
    counter = 0
    while true
      counter += 1

      # break after 25, we don't want in infinite loop
      break if counter == 25

      new_name = "#{basename}-#{counter}.#{extname}"

      if FileAsset.where('directory = ? and name = ?', self.directory, new_name).first.nil?
        self.name = new_name
        break
      end
    end
  end
end

#copy(path, name) ⇒ Object



164
165
166
167
168
# File 'app/models/file_asset.rb', line 164

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

  file_support.copy(self.path, path, name)
end

#extnameObject



216
217
218
# File 'app/models/file_asset.rb', line 216

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

#get_contentsObject



236
237
238
239
# File 'app/models/file_asset.rb', line 236

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)


160
161
162
# File 'app/models/file_asset.rb', line 160

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

#move(new_parent_path) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/models/file_asset.rb', line 241

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



176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/models/file_asset.rb', line 176

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/models/file_asset.rb', line 189

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 => 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



224
225
226
227
228
229
230
# File 'app/models/file_asset.rb', line 224

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



232
233
234
# File 'app/models/file_asset.rb', line 232

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

#set_stiObject



220
221
222
# File 'app/models/file_asset.rb', line 220

def set_sti
  update_attribute :type, @type
end

#trim_name(size) ⇒ Object



208
209
210
211
212
213
214
# File 'app/models/file_asset.rb', line 208

def trim_name(size)
  if self.name.length < size
    self.name
  else
    self.name[0..size]
  end
end

#urlObject

compass file download url



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

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