Problem
I have a lot of files being recognized as application/ocet-stream
instead of application/x-photoshop
, text/ruby
and many files’ official mime-type classifications.
I"m struggling to figure out whether the problem is Logic-specific or Paperclip-specific before I assume too fast it’s a Rack-specific problem and have to accordingly modify Rack::Mime::MIME_TYPES
.
I have the following definition defined in a custom decorator that outputs a string to be used as a class inside my views
def file_icon_class
case attachment.file_content_type
when 'application/pdf'
".pdf"
when 'text/ruby' || 'text/rb'
".rb"
when "text/html"
".html"
when "text/coffee"
".coffee"
else
".other"
end
end
For some reason, I get application/ocet-stream
defined everywhere. Is it truly Rack-specific and I have to do the following multiple times inside my app configuration files?
Rack::Mime::MIME_TYPES[".pdf"] = "application/pdf"
Otherwise, I’ll have no choice but to use regular expressions to infer the filetype based on the end of the file_name. It seems like a overkill step however. As a result, thought I’d ask this question first.
In case it’s a edge case, could this be related to how paperclip handles such files?
Appendix: Complete Decorator Definition
class AttachmentDecorator
attr_reader :attachment
include ActionView::Helpers
def initialize(attachment)
@attachment = attachment
end
def self.decorate_attachments(attachments)
attachments.map {|attachment| new(attachment)}
end
class << self
alias_method :build_collection, :decorate_attachments
end
def method_missing(method, *args, &block)
attachment.send(method, *args, &block)
end
def respond_to_missing?(method)
attachment.respond_to?(method) || super
end
def file_icon_class
case attachment.file_content_type
when 'application/pdf'
".pdf"
when 'text/ruby' || 'text/rb'
".rb"
when "text/html"
".html"
when "text/coffee"
".coffee"
else
".other"
end
# just to be explicit with my intentions here
end
#may have to define a get_ambigious_type(file_type) method for all application/ocet-stream methods and use the file_name instead?
end