Class: Cloudflare::UploadedFile
- Inherits:
-
Object
- Object
- Cloudflare::UploadedFile
- Defined in:
- lib/cloudflare_workers/multipart.rb
Overview
Struct-ish wrapper for an uploaded file part. Identical shape to the ‘:type, :name, :tempfile, :head` Hash Rack’s parser returns, plus extras for the Workers use case.
Instance Attribute Summary collapse
-
#bytes_binstr ⇒ Object
readonly
Returns the value of attribute bytes_binstr.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#[](key) ⇒ Object
‘#[]` so `file` works on the UploadedFile itself (some gems use the Hash shape, some grab the file object — support both access patterns to reduce downstream surprises).
- #close ⇒ Object
-
#initialize(filename:, content_type:, name:, head: '', bytes_binstr: '') ⇒ UploadedFile
constructor
A new instance of UploadedFile.
-
#read ⇒ Object
Read the full byte string.
- #rewind ⇒ Object
-
#size ⇒ Object
(also: #bytesize)
Byte length of the part (not the JS string length — they’re the same here because we use latin1 1-byte-per-char encoding).
-
#to_blob ⇒ Object
Convert to a JS Blob for fetch/Response bodies.
-
#to_h ⇒ Object
(also: #to_hash)
Rack-friendly Hash view.
-
#to_uint8_array ⇒ Object
Convert the latin1 byte-string to a real JS Uint8Array.
-
#type ⇒ Object
Convenience accessor matching the CRuby Rack shape.
Constructor Details
#initialize(filename:, content_type:, name:, head: '', bytes_binstr: '') ⇒ UploadedFile
Returns a new instance of UploadedFile.
46 47 48 49 50 51 52 |
# File 'lib/cloudflare_workers/multipart.rb', line 46 def initialize(filename:, content_type:, name:, head: '', bytes_binstr: '') @filename = filename @content_type = content_type || 'application/octet-stream' @name = name @head = head @bytes_binstr = bytes_binstr || '' end |
Instance Attribute Details
#bytes_binstr ⇒ Object (readonly)
Returns the value of attribute bytes_binstr.
44 45 46 |
# File 'lib/cloudflare_workers/multipart.rb', line 44 def bytes_binstr @bytes_binstr end |
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
44 45 46 |
# File 'lib/cloudflare_workers/multipart.rb', line 44 def content_type @content_type end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
44 45 46 |
# File 'lib/cloudflare_workers/multipart.rb', line 44 def filename @filename end |
#head ⇒ Object (readonly)
Returns the value of attribute head.
44 45 46 |
# File 'lib/cloudflare_workers/multipart.rb', line 44 def head @head end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
44 45 46 |
# File 'lib/cloudflare_workers/multipart.rb', line 44 def name @name end |
Instance Method Details
#[](key) ⇒ Object
‘#[]` so `file` works on the UploadedFile itself (some gems use the Hash shape, some grab the file object —support both access patterns to reduce downstream surprises).
114 115 116 |
# File 'lib/cloudflare_workers/multipart.rb', line 114 def [](key) to_h[key.to_sym] end |
#close ⇒ Object
75 76 77 |
# File 'lib/cloudflare_workers/multipart.rb', line 75 def close self end |
#read ⇒ Object
Read the full byte string. Mirrors Tempfile#read.
67 68 69 |
# File 'lib/cloudflare_workers/multipart.rb', line 67 def read @bytes_binstr end |
#rewind ⇒ Object
71 72 73 |
# File 'lib/cloudflare_workers/multipart.rb', line 71 def rewind self end |
#size ⇒ Object Also known as: bytesize
Byte length of the part (not the JS string length — they’re the same here because we use latin1 1-byte-per-char encoding).
56 57 58 |
# File 'lib/cloudflare_workers/multipart.rb', line 56 def size @bytes_binstr.length end |
#to_blob ⇒ Object
Convert to a JS Blob for fetch/Response bodies.
92 93 94 95 96 |
# File 'lib/cloudflare_workers/multipart.rb', line 92 def to_blob u8 = to_uint8_array ct = @content_type `new Blob([#{u8}], { type: #{ct} })` end |
#to_h ⇒ Object Also known as: to_hash
Rack-friendly Hash view. Match the exact shape Rack::Multipart produces so gems that do ‘params[:filename]` keep working.
100 101 102 103 104 105 106 107 108 |
# File 'lib/cloudflare_workers/multipart.rb', line 100 def to_h { filename: @filename, type: @content_type, name: @name, head: @head, tempfile: self } end |
#to_uint8_array ⇒ Object
Convert the latin1 byte-string to a real JS Uint8Array. Used to feed raw bytes to ‘env.BUCKET.put`, `globalThis.fetch(body: …)`, `Blob`, etc. without re-encoding through UTF-8.
NOTE: single-line backtick x-string so Opal emits it as an expression (multi-line x-strings compile to raw statements and would silently return ‘undefined`). Same gotcha documented elsewhere in this codebase (see lib/cloudflare_workers.rb).
87 88 89 |
# File 'lib/cloudflare_workers/multipart.rb', line 87 def to_uint8_array `(function(s) { var len = s.length; var out = new Uint8Array(len); for (var i = 0; i < len; i++) { out[i] = s.charCodeAt(i) & 0xff; } return out; })(#{@bytes_binstr})` end |
#type ⇒ Object
Convenience accessor matching the CRuby Rack shape.
62 63 64 |
# File 'lib/cloudflare_workers/multipart.rb', line 62 def type @content_type end |