class Koala::HTTPService::UploadableIO

Constants

DETECTION_STRATEGIES
MIME_TYPE_STRATEGIES
PARSE_STRATEGIES

Attributes

content_type[R]
filename[R]
io_or_path[R]

Public Class Methods

binary_content?(content) click to toggle source
   # File lib/koala/http_service/uploadable_io.rb
30 def self.binary_content?(content)
31   content.is_a?(UploadableIO) || DETECTION_STRATEGIES.detect {|method| send(method, content)}
32 end
new(io_or_path_or_mixed, content_type = nil, filename = nil) click to toggle source
   # File lib/koala/http_service/uploadable_io.rb
 9 def initialize(io_or_path_or_mixed, content_type = nil, filename = nil)
10   # see if we got the right inputs
11   parse_init_mixed_param io_or_path_or_mixed, content_type
12 
13   # filename is used in the Ads API
14   # if it's provided, take precedence over the detected filename
15   # otherwise, fall back to a dummy name
16   @filename = filename || @filename || "koala-io-file.dum"
17 
18   raise KoalaError.new("Invalid arguments to initialize an UploadableIO") unless @io_or_path
19   raise KoalaError.new("Unable to determine MIME type for UploadableIO") if !@content_type
20 end

Private Class Methods

file_param?(file) click to toggle source

takes a file object

   # File lib/koala/http_service/uploadable_io.rb
83 def self.file_param?(file)
84   file.kind_of?(File) || file.kind_of?(Tempfile)
85 end
rails_3_param?(uploaded_file) click to toggle source

Expects a parameter of type ActionDispatch::Http::UploadedFile

   # File lib/koala/http_service/uploadable_io.rb
57 def self.rails_3_param?(uploaded_file)
58   uploaded_file.respond_to?(:content_type) and uploaded_file.respond_to?(:tempfile) and uploaded_file.tempfile.respond_to?(:path)
59 end
sinatra_param?(file_hash) click to toggle source

Expects a Sinatra hash of file info

   # File lib/koala/http_service/uploadable_io.rb
70 def self.sinatra_param?(file_hash)
71   file_hash.kind_of?(Hash) and file_hash.has_key?(:type) and file_hash.has_key?(:tempfile)
72 end

Public Instance Methods

to_file() click to toggle source
   # File lib/koala/http_service/uploadable_io.rb
26 def to_file
27   @io_or_path.is_a?(String) ? File.open(@io_or_path) : @io_or_path
28 end
to_upload_io() click to toggle source
   # File lib/koala/http_service/uploadable_io.rb
22 def to_upload_io
23   UploadIO.new(@io_or_path, @content_type, @filename)
24 end

Private Instance Methods

detect_mime_type(filename) click to toggle source
    # File lib/koala/http_service/uploadable_io.rb
115 def detect_mime_type(filename)
116   if filename
117     MIME_TYPE_STRATEGIES.each do |method|
118       result = send(method, filename)
119       return result if result
120     end
121   end
122   nil # if we can't find anything
123 end
parse_file_object(file, content_type = nil) click to toggle source
   # File lib/koala/http_service/uploadable_io.rb
87 def parse_file_object(file, content_type = nil)
88   if UploadableIO.file_param?(file)
89     @io_or_path = file
90     @content_type = content_type || detect_mime_type(file.path)
91     @filename = File.basename(file.path)
92   end
93 end
parse_init_mixed_param(mixed, content_type = nil) click to toggle source
   # File lib/koala/http_service/uploadable_io.rb
49 def parse_init_mixed_param(mixed, content_type = nil)
50   PARSE_STRATEGIES.each do |method|
51     send(method, mixed, content_type)
52     return if @io_or_path && @content_type
53   end
54 end
parse_io(io, content_type = nil) click to toggle source
    # File lib/koala/http_service/uploadable_io.rb
103 def parse_io(io, content_type = nil)
104   if io.respond_to?(:read)
105     @io_or_path = io
106     @content_type = content_type
107   end
108 end
parse_rails_3_param(uploaded_file, content_type = nil) click to toggle source
   # File lib/koala/http_service/uploadable_io.rb
61 def parse_rails_3_param(uploaded_file, content_type = nil)
62   if UploadableIO.rails_3_param?(uploaded_file)
63     @io_or_path = uploaded_file.tempfile.path
64     @content_type = content_type || uploaded_file.content_type
65     @filename = uploaded_file.original_filename
66   end
67 end
parse_sinatra_param(file_hash, content_type = nil) click to toggle source
   # File lib/koala/http_service/uploadable_io.rb
74 def parse_sinatra_param(file_hash, content_type = nil)
75   if UploadableIO.sinatra_param?(file_hash)
76     @io_or_path = file_hash[:tempfile]
77     @content_type = content_type || file_hash[:type] || detect_mime_type(tempfile)
78     @filename = file_hash[:filename]
79   end
80 end
parse_string_path(path, content_type = nil) click to toggle source
    # File lib/koala/http_service/uploadable_io.rb
 95 def parse_string_path(path, content_type = nil)
 96   if path.kind_of?(String)
 97     @io_or_path = path
 98     @content_type = content_type || detect_mime_type(path)
 99     @filename = File.basename(path)
100   end
101 end
use_mime_module(filename) click to toggle source
    # File lib/koala/http_service/uploadable_io.rb
125 def use_mime_module(filename)
126   # if the user has installed mime/types, we can use that
127   # if not, rescue and return nil
128   begin
129     type = MIME::Types.type_for(filename).first
130     type ? type.to_s : nil
131   rescue
132     nil
133   end
134 end
use_simple_detection(filename) click to toggle source
    # File lib/koala/http_service/uploadable_io.rb
136 def use_simple_detection(filename)
137   # very rudimentary extension analysis for images
138   # first, get the downcased extension, or an empty string if it doesn't exist
139   extension = ((filename.match(/\.([a-zA-Z0-9]+)$/) || [])[1] || "").downcase
140   case extension
141     when ""
142       nil
143     # images
144     when "jpg", "jpeg"
145       "image/jpeg"
146     when "png"
147       "image/png"
148     when "gif"
149       "image/gif"
150 
151     # video
152     when "3g2"
153           "video/3gpp2"
154     when "3gp", "3gpp"
155           "video/3gpp"
156     when "asf"
157           "video/x-ms-asf"
158     when "avi"
159           "video/x-msvideo"
160     when "flv"
161           "video/x-flv"
162     when "m4v"
163           "video/x-m4v"
164     when "mkv"
165           "video/x-matroska"
166     when "mod"
167           "video/mod"
168     when "mov", "qt"
169           "video/quicktime"
170     when "mp4", "mpeg4"
171           "video/mp4"
172     when "mpe", "mpeg", "mpg", "tod", "vob"
173           "video/mpeg"
174     when "nsv"
175           "application/x-winamp"
176     when "ogm", "ogv"
177           "video/ogg"
178     when "wmv"
179           "video/x-ms-wmv"
180   end
181 end