class Media::TempFile
Public Class Methods
new(data, content_type=nil)
click to toggle source
data may be one of:
- FileUpload object: like the kind returned in multibyte encoded file upload forms. - Pathname object: then load data from the file pointed to by the pathname. - IO object: read the contents of the io object, copy to tmp file. - otherwise, dump the contents of the data to the tmp file.
if data is empty, we generate an empty one.
# File lib/media/temp_file.rb, line 41 def initialize(data, content_type=nil) if data.nil? @tmpfile = TempFile.create_from_content_type(content_type) elsif data.respond_to?(:path) # we are dealing with an uploaded file object @tmpfile = TempFile.create_from_file(data.path, content_type, {mode: :move}) elsif data.is_a?(StringIO) data.rewind @tmpfile = TempFile.create_from_data(data.read, content_type) elsif data.instance_of?(Pathname) @tmpfile = TempFile.create_from_file(data.to_s, content_type) else @tmpfile = TempFile.create_from_data(data, content_type) end end
open(data, content_type=nil) { |tmp| ... }
click to toggle source
like initialize, but if given a block, then it yields the TempFile
and also unlinks the file at the end of the block.
# File lib/media/temp_file.rb, line 61 def self.open(data, content_type=nil) tmp = TempFile.new(data, content_type) if block_given? begin yield tmp ensure tmp.clear end nil else tmp end end
tempfile_path()
click to toggle source
# File lib/media/temp_file.rb, line 21 def self.tempfile_path ::Media::TMP_PATH end
Private Class Methods
content_type_basename(content_type)
click to toggle source
create a filename with a file extension from the content_type
# File lib/media/temp_file.rb, line 142 def self.content_type_basename(content_type) if content_type extension = Media::MimeType.extension_from_mime_type(content_type) || :bin ['media_temp_file', ".#{extension}"] else 'media_temp_file' end end
create_from_content_type(content_type)
click to toggle source
create an empty temp file with an extension to match the content_type
# File lib/media/temp_file.rb, line 113 def self.create_from_content_type(content_type) tf = new_for_content_type(content_type) tf.close tf end
create_from_data(data, content_type=nil)
click to toggle source
creates a tempfile filled with the given binary data
# File lib/media/temp_file.rb, line 102 def self.create_from_data(data, content_type=nil) tf = new_for_content_type(content_type) tf.binmode tf.write(data) tf.close tf end
create_from_file(filepath, content_type, options = {})
click to toggle source
create a tmp file that is a copy of another file.
# File lib/media/temp_file.rb, line 122 def self.create_from_file(filepath, content_type, options = {}) tf = new_for_content_type(content_type) tf.close if options[:mode] == :move FileUtils.mv filepath, tf.path else FileUtils.cp filepath, tf.path end tf end
new_for_content_type(content_type)
click to toggle source
# File lib/media/temp_file.rb, line 133 def self.new_for_content_type(content_type) Tempfile.new content_type_basename(content_type), tempfile_path, mode: 022 end
Public Instance Methods
any?()
click to toggle source
# File lib/media/temp_file.rb, line 81 def any? @tmpfile.any? end
clear()
click to toggle source
# File lib/media/temp_file.rb, line 75 def clear # this is not really needed, because the tmp files are deleted as soon as # @tmpfile gets garbage collected. # @tmpfile.unlink end
path()
click to toggle source
# File lib/media/temp_file.rb, line 85 def path @tmpfile.path end
to_s()
click to toggle source
# File lib/media/temp_file.rb, line 89 def to_s @tmpfile.path end