Module: Imgwire::Uploads
- Defined in:
- lib/imgwire/uploads.rb
Defined Under Namespace
Classes: ResolvedUpload
Constant Summary collapse
- DEFAULT_CONTENT_TYPE =
'application/octet-stream'- MIME_TYPES =
{ '.avif' => 'image/avif', '.gif' => 'image/gif', '.jpeg' => 'image/jpeg', '.jpg' => 'image/jpeg', '.png' => 'image/png', '.webp' => 'image/webp' }.freeze
Class Method Summary collapse
- .infer_content_length(file) ⇒ Object
- .infer_file_name(file) ⇒ Object
- .infer_mime_type(file_name) ⇒ Object
- .resolve(file:, file_name: nil, mime_type: nil, content_length: nil) ⇒ Object
Class Method Details
.infer_content_length(file) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/imgwire/uploads.rb', line 68 def infer_content_length(file) return file.size if file.respond_to?(:size) && file.size return File.size(file.path) if file.respond_to?(:path) && file.path return file.string.bytesize if file.is_a?(StringIO) nil end |
.infer_file_name(file) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/imgwire/uploads.rb', line 55 def infer_file_name(file) return File.basename(file.path) if file.respond_to?(:path) && file.path return 'upload.bin' if file.is_a?(StringIO) nil end |
.infer_mime_type(file_name) ⇒ Object
62 63 64 65 66 |
# File 'lib/imgwire/uploads.rb', line 62 def infer_mime_type(file_name) return nil unless file_name MIME_TYPES[File.extname(file_name).downcase] end |
.resolve(file:, file_name: nil, mime_type: nil, content_length: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/imgwire/uploads.rb', line 33 def resolve(file:, file_name: nil, mime_type: nil, content_length: nil) io = file inferred_file_name = file_name || infer_file_name(file) inferred_mime_type = mime_type || infer_mime_type(inferred_file_name) inferred_content_length = content_length || infer_content_length(file) raise ArgumentError, 'Upload file must respond to #read' unless io.respond_to?(:read) if inferred_file_name.nil? || inferred_file_name.empty? raise ArgumentError, 'Upload file_name could not be inferred' end raise ArgumentError, 'Upload content_length could not be inferred' if inferred_content_length.nil? ResolvedUpload.new( io: io, file_name: inferred_file_name, mime_type: inferred_mime_type || DEFAULT_CONTENT_TYPE, content_length: inferred_content_length ) end |