Class: Spikard::UploadFile
- Inherits:
-
Object
- Object
- Spikard::UploadFile
- Defined in:
- lib/spikard/upload_file.rb
Overview
File upload handling for multipart/form-data requests
This class provides an interface for handling file uploads, designed to be compatible with Rails patterns while optimized for Spikard’s Rust-backed request processing.
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
Get the raw content as a string.
-
#content_type ⇒ String
readonly
MIME type of the uploaded file.
-
#filename ⇒ String
readonly
Original filename from the client.
-
#headers ⇒ Hash<String, String>
readonly
Additional headers associated with this file field.
-
#size ⇒ Integer
readonly
Size of the file in bytes.
Instance Method Summary collapse
-
#close ⇒ nil
Close the file (no-op for StringIO-based implementation).
-
#closed? ⇒ Boolean
Check if file is closed.
-
#initialize(filename, content, content_type: nil, size: nil, headers: nil, content_encoding: nil) ⇒ UploadFile
constructor
Create a new UploadFile instance.
-
#read(size = nil) ⇒ String
Read file contents.
-
#rewind ⇒ Integer
Rewind to the beginning of the file.
-
#seek(offset, whence = IO::SEEK_SET) ⇒ Integer
Seek to a specific position in the file.
-
#tell ⇒ Integer
(also: #pos)
Get current position in the file.
-
#text(encoding: 'UTF-8') ⇒ String
Read file contents as text.
Constructor Details
#initialize(filename, content, content_type: nil, size: nil, headers: nil, content_encoding: nil) ⇒ UploadFile
Create a new UploadFile instance
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/spikard/upload_file.rb', line 45 def initialize(filename, content, content_type: nil, size: nil, headers: nil, content_encoding: nil) @filename = filename @content_type = content_type || 'application/octet-stream' @headers = headers || {} # Decode content if base64 encoded @content = if content_encoding == 'base64' || base64_encoded?(content) Base64.decode64(content) else content end @size = size || @content.bytesize @io = StringIO.new(@content) end |
Instance Attribute Details
#content ⇒ String (readonly)
Get the raw content as a string
118 119 120 |
# File 'lib/spikard/upload_file.rb', line 118 def content @content end |
#content_type ⇒ String (readonly)
Returns MIME type of the uploaded file.
29 30 31 |
# File 'lib/spikard/upload_file.rb', line 29 def content_type @content_type end |
#filename ⇒ String (readonly)
Returns Original filename from the client.
26 27 28 |
# File 'lib/spikard/upload_file.rb', line 26 def filename @filename end |
#headers ⇒ Hash<String, String> (readonly)
Returns Additional headers associated with this file field.
35 36 37 |
# File 'lib/spikard/upload_file.rb', line 35 def headers @headers end |
#size ⇒ Integer (readonly)
Returns Size of the file in bytes.
32 33 34 |
# File 'lib/spikard/upload_file.rb', line 32 def size @size end |
Instance Method Details
#close ⇒ nil
Close the file (no-op for StringIO-based implementation)
104 105 106 |
# File 'lib/spikard/upload_file.rb', line 104 def close @io.close end |
#closed? ⇒ Boolean
Check if file is closed
111 112 113 |
# File 'lib/spikard/upload_file.rb', line 111 def closed? @io.closed? end |
#read(size = nil) ⇒ String
Read file contents
65 66 67 |
# File 'lib/spikard/upload_file.rb', line 65 def read(size = nil) @io.read(size) end |
#rewind ⇒ Integer
Rewind to the beginning of the file
97 98 99 |
# File 'lib/spikard/upload_file.rb', line 97 def rewind @io.rewind end |
#seek(offset, whence = IO::SEEK_SET) ⇒ Integer
Seek to a specific position in the file
82 83 84 |
# File 'lib/spikard/upload_file.rb', line 82 def seek(offset, whence = IO::SEEK_SET) @io.seek(offset, whence) end |
#tell ⇒ Integer Also known as: pos
Get current position in the file
89 90 91 |
# File 'lib/spikard/upload_file.rb', line 89 def tell @io.tell end |
#text(encoding: 'UTF-8') ⇒ String
Read file contents as text
73 74 75 |
# File 'lib/spikard/upload_file.rb', line 73 def text(encoding: 'UTF-8') @content.force_encoding(encoding) end |