class Tilia::Dav::Browser::GuessContentType

GuessContentType plugin

A lot of the built-in File objects just return application/octet-stream as a content-type by default. This is a problem for some clients, because they expect a correct contenttype.

There's really no accurate, fast and portable way to determine the contenttype so this extension does what the rest of the world does, and guesses it based on the file extension.

Attributes

extension_map[RW]

List of recognized file extensions

Feel free to add more

@var array

Public Class Methods

new() click to toggle source

TODO: document

# File lib/tilia/dav/browser/guess_content_type.rb, line 62
def initialize
  @extension_map = {
    # images
    'jpg' => 'image/jpeg',
    'gif' => 'image/gif',
    'png' => 'image/png',

    # groupware
    'ics' => 'text/calendar',
    'vcf' => 'text/vcard',

    # text
    'txt' => 'text/plain'
  }
end

Public Instance Methods

prop_find(prop_find, _node) click to toggle source

Our PROPFIND handler

Here we set a contenttype, if the node didn't already have one.

@param PropFind prop_find @param INode node @return void

# File lib/tilia/dav/browser/guess_content_type.rb, line 38
def prop_find(prop_find, _node)
  prop_find.handle(
    '{DAV:}getcontenttype',
    lambda do
      file_name = Http::UrlUtil.split_path(prop_find.path)[1]
      return content_type(file_name)
    end
  )
end
setup(server) click to toggle source

Initializes the plugin

@param DAVServer server @return void

# File lib/tilia/dav/browser/guess_content_type.rb, line 25
def setup(server)
  # Using a relatively low priority (200) to allow other extensions
  # to set the content-type first.
  server.on('propFind', method(:prop_find), 200)
end

Protected Instance Methods

content_type(file_name) click to toggle source

Simple method to return the contenttype

@param string file_name @return string

# File lib/tilia/dav/browser/guess_content_type.rb, line 54
def content_type(file_name)
  # Just grabbing the extension
  extension = ::File.extname(file_name.downcase)[1..-1]
  return @extension_map[extension] if @extension_map.key?(extension)
  'application/octet-stream'
end