Guest User

Untitled

a guest
Mar 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. # AttachmentFu sets the Attachment content type from the browser sent Content-type header
  2. #
  3. # This monkey patch uses UNIX file utility to fix broken or missing content types headers
  4.  
  5. module Technoweenie::AttachmentFu::InstanceMethods
  6. def uploaded_data_with_unix_file_mime_type=(file_data)
  7. tmp_file = self.uploaded_data_without_unix_file_mime_type=(file_data)
  8.  
  9. if tmp_file.present? && (unix_file = `which file`.chomp).present? && File.exists?(unix_file)
  10. `#{ unix_file } -v 2>&1` =~ /^file-(.*)$/
  11. version = $1
  12.  
  13. self.content_type = if version >= "4.26"
  14. `#{ unix_file } -b --mime-type #{ tmp_file.path }`.chomp
  15. else
  16. `#{ unix_file } -bi #{ tmp_file.path }`.chomp =~ /(\w*\/[\w+-\.]*)/
  17. $1
  18. end
  19. end
  20.  
  21. tmp_file
  22. end
  23.  
  24. alias_method_chain :uploaded_data=, :unix_file_mime_type
  25. end
Add Comment
Please, Sign In to add comment