Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Created April 20, 2010 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarreck/372976 to your computer and use it in GitHub Desktop.
Save pmarreck/372976 to your computer and use it in GitHub Desktop.
require 'digest/sha1'
require 'tempfile'
class LocalPaperclipTempfile < ::Tempfile
# note: had a ton of trouble using "data" instead of "tmp_data" for naming here:
def initialize(filename, tmp_data, tmpdir = Dir::tmpdir)
@original_filename = filename
@tmp_data = tmp_data
super Digest::SHA1.hexdigest(@tmp_data), tmpdir
scribe
end
def scribe
self.sync = true
self.binmode
self.write @tmp_data
self.rewind
self
end
def original_filename
@original_filename
end
def content_type
begin
mime = `file --mime -br #{self.path}`.strip
mime = mime.gsub(/^.*: */,"")
mime = mime.gsub(/;.*$/,"")
mime = mime.gsub(/,.*$/,"")
rescue
mime = "image/png" # don't really like the dependency on the 'file' command...
end
mime
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment