Class: Gluttonberg::AssetType

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

Class Method Summary collapse

Class Method Details

.build_defaultsObject



59
60
61
# File 'app/models/gluttonberg/asset_type.rb', line 59

def self.build_defaults
  Library::build_default_asset_types
end

.for_file(mime_type, file_name) ⇒ Object

Take the reported mime-type and the file_name and return the best AssetType to use for that file.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/gluttonberg/asset_type.rb', line 16

def self.for_file(mime_type, file_name)

  mime_types = ::MIME::Types[mime_type]


  # if the supplied mime_type isn't recognised as aregistered or common one,
  # try and work out a suitable one from the file extension
  if mime_types.blank?
    mime_types = ::MIME::Types.type_for(file_name)
  end

  # OK, we really have no idea what this is
  if mime_types.empty?
    file_mime_type = mime_type
    file_base_type = mime_type.split('/').first
  else
    # multiple mime-types may be returned, but we only want to work with
    # one, so pick the first one
    file_mime_type = mime_types.first.content_type
    file_base_type = mime_types.first.raw_media_type
  end

  asset_mime_type = AssetMimeType.find(:first, :conditions => "mime_type = '#{file_mime_type}'")

  if asset_mime_type.nil? then
    asset_mime_type = AssetMimeType.find(:first, :conditions => "mime_type = '#{file_base_type}'")
    if asset_mime_type.nil? then
      # this is a completely unknown type, so default to unkown
      asset_mime_type = AssetMimeType.find( :first, :conditions => "mime_type = 'unknown'")
      if asset_mime_type.nil? then
        # something went wrong, so just assign anything :-(
        return AssetType.first
      else
        return asset_mime_type.asset_type
      end
    else
      return asset_mime_type.asset_type
    end
  else
    return asset_mime_type.asset_type
  end
end