Class: Cloudflare::UploadedFile

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_binstrObject (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_typeObject (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

#filenameObject (readonly)

Returns the value of attribute filename.



44
45
46
# File 'lib/cloudflare_workers/multipart.rb', line 44

def filename
  @filename
end

#headObject (readonly)

Returns the value of attribute head.



44
45
46
# File 'lib/cloudflare_workers/multipart.rb', line 44

def head
  @head
end

#nameObject (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

#closeObject



75
76
77
# File 'lib/cloudflare_workers/multipart.rb', line 75

def close
  self
end

#readObject

Read the full byte string. Mirrors Tempfile#read.



67
68
69
# File 'lib/cloudflare_workers/multipart.rb', line 67

def read
  @bytes_binstr
end

#rewindObject



71
72
73
# File 'lib/cloudflare_workers/multipart.rb', line 71

def rewind
  self
end

#sizeObject 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_blobObject

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_hObject 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_arrayObject

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

#typeObject

Convenience accessor matching the CRuby Rack shape.



62
63
64
# File 'lib/cloudflare_workers/multipart.rb', line 62

def type
  @content_type
end