aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRobin Houston <robin@lenny.robin>2011-06-21 02:27:28 +0100
committerRobin Houston <robin@lenny.robin>2011-06-21 02:27:28 +0100
commit23a8c8fae9eb3e0892cdab5c6f3343bf06a5c6dc (patch)
tree012d5e686a3b6020487d384f38458ae6f9c5b111 /lib
parent9d62991558aba1f0603a3107a8b2ca19010dbf56 (diff)
- Refactor models/incoming_message.rb to get rid of the hideous
global variables and methods. Move the MIME type stuff into lib/alaveteli_file_types.rb. - Correct the spelling of the word extensions (not extentions).
Diffstat (limited to 'lib')
-rw-r--r--lib/alaveteli_file_types.rb92
1 files changed, 92 insertions, 0 deletions
diff --git a/lib/alaveteli_file_types.rb b/lib/alaveteli_file_types.rb
new file mode 100644
index 000000000..b8765d65e
--- /dev/null
+++ b/lib/alaveteli_file_types.rb
@@ -0,0 +1,92 @@
+require 'mahoro'
+
+class AlaveteliFileTypes
+ # To add an image, create a file with appropriate name corresponding to the
+ # mime type in public/images e.g. icon_image_tiff_large.png
+ FileExtensionToMimeType = {
+ "txt" => 'text/plain',
+ "pdf" => 'application/pdf',
+ "rtf" => 'application/rtf',
+ "doc" => 'application/vnd.ms-word',
+ "docx" => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ "xls" => 'application/vnd.ms-excel',
+ "xlsx" => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ "ppt" => 'application/vnd.ms-powerpoint',
+ "pptx" => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ "oft" => 'application/vnd.ms-outlook',
+ "msg" => 'application/vnd.ms-outlook',
+ "tnef" => 'application/ms-tnef',
+ "tif" => 'image/tiff',
+ "gif" => 'image/gif',
+ "jpg" => 'image/jpeg', # XXX add jpeg
+ "png" => 'image/png',
+ "bmp" => 'image/bmp',
+ "html" => 'text/html', # XXX add htm
+ "vcf" => 'text/x-vcard',
+ "zip" => 'application/zip',
+ "delivery-status" => 'message/delivery-status'
+ }
+ # XXX doesn't have way of choosing default for inverse map - might want to add
+ # one when you need it
+ FileExtensionToMimeTypeRev = FileExtensionToMimeType.invert
+
+ class << self
+ def all_extensions
+ return FileExtensionToMimeType.keys
+ end
+
+ # Given file name and its content, return most likely type
+ def filename_and_content_to_mimetype(filename, content)
+ # Try filename
+ ret = filename_to_mimetype(filename)
+ if !ret.nil?
+ return ret
+ end
+
+ # Otherwise look inside the file to work out the type.
+ # Mahoro is a Ruby binding for libmagic.
+ m = Mahoro.new(Mahoro::MIME)
+ mahoro_type = m.buffer(content)
+ mahoro_type.strip!
+ # XXX we shouldn't have to check empty? here, but Mahoro sometimes returns a blank line :(
+ # e.g. for InfoRequestEvent 17930
+ if mahoro_type.nil? || mahoro_type.empty?
+ return nil
+ end
+ # text/plain types sometimes come with a charset
+ mahoro_type.match(/^(.*);/)
+ if $1
+ mahoro_type = $1
+ end
+ # see if looks like a content type, or has something in it that does
+ # and return that
+ # mahoro returns junk "\012- application/msword" as mime type.
+ mahoro_type.match(/([a-z0-9.-]+\/[a-z0-9.-]+)/)
+ if $1
+ return $1
+ end
+ # otherwise we got junk back from mahoro
+ return nil
+ end
+
+ def filename_to_mimetype(filename)
+ if !filename
+ return nil
+ end
+ if filename.match(/\.([^.]+)$/i)
+ lext = $1.downcase
+ if FileExtensionToMimeType.include?(lext)
+ return FileExtensionToMimeType[lext]
+ end
+ end
+ return nil
+ end
+
+ def mimetype_to_extension(mime)
+ if FileExtensionToMimeTypeRev.include?(mime)
+ return FileExtensionToMimeTypeRev[mime]
+ end
+ return nil
+ end
+ end
+end \ No newline at end of file