Class: Whoosh::UploadedFile
- Inherits:
-
Object
- Object
- Whoosh::UploadedFile
- Defined in:
- lib/whoosh/uploaded_file.rb
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Instance Method Summary collapse
-
#initialize(rack_hash, storage: nil) ⇒ UploadedFile
constructor
A new instance of UploadedFile.
- #read ⇒ Object
- #read_text ⇒ Object
- #save(prefix = "") ⇒ Object
- #size ⇒ Object
- #to_base64 ⇒ Object
- #validate!(types: nil, max_size: nil) ⇒ Object
Constructor Details
#initialize(rack_hash, storage: nil) ⇒ UploadedFile
Returns a new instance of UploadedFile.
10 11 12 13 14 15 |
# File 'lib/whoosh/uploaded_file.rb', line 10 def initialize(rack_hash, storage: nil) @filename = rack_hash[:filename] @content_type = rack_hash[:type] @tempfile = rack_hash[:tempfile] @storage = storage end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
8 9 10 |
# File 'lib/whoosh/uploaded_file.rb', line 8 def content_type @content_type end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
8 9 10 |
# File 'lib/whoosh/uploaded_file.rb', line 8 def filename @filename end |
Instance Method Details
#read ⇒ Object
21 22 23 24 |
# File 'lib/whoosh/uploaded_file.rb', line 21 def read @tempfile.rewind @tempfile.read end |
#read_text ⇒ Object
26 27 28 |
# File 'lib/whoosh/uploaded_file.rb', line 26 def read_text read.force_encoding("UTF-8") end |
#save(prefix = "") ⇒ Object
34 35 36 37 |
# File 'lib/whoosh/uploaded_file.rb', line 34 def save(prefix = "") raise Errors::DependencyError, "No storage adapter configured" unless @storage @storage.save(self, prefix) end |
#size ⇒ Object
17 18 19 |
# File 'lib/whoosh/uploaded_file.rb', line 17 def size @tempfile.size end |
#to_base64 ⇒ Object
30 31 32 |
# File 'lib/whoosh/uploaded_file.rb', line 30 def to_base64 Base64.strict_encode64(read) end |
#validate!(types: nil, max_size: nil) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/whoosh/uploaded_file.rb', line 39 def validate!(types: nil, max_size: nil) errors = [] errors << { field: "file", message: "No file uploaded" } if @filename.nil? || @filename.empty? errors << { field: "file", message: "File type #{@content_type} not allowed" } if types && !types.include?(@content_type) errors << { field: "file", message: "File too large (#{size} bytes > #{max_size})" } if max_size && size > max_size raise Errors::ValidationError.new(errors) unless errors.empty? end |