Class: Spikard::UploadFile

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

Examples:

app.post('/upload') do |body|
  file = body[:file]  # UploadFile instance
  content = file.read
  {
    filename: file.filename,
    size: file.size,
    content_type: file.content_type,
    description: body[:description]
  }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, content, content_type: nil, size: nil, headers: nil, content_encoding: nil) ⇒ UploadFile

Create a new UploadFile instance

Parameters:

  • filename (String)

    Original filename from the client

  • content (String)

    File contents (may be base64 encoded)

  • content_type (String, nil) (defaults to: nil)

    MIME type (defaults to “application/octet-stream”)

  • size (Integer, nil) (defaults to: nil)

    File size in bytes (computed from content if not provided)

  • headers (Hash<String, String>, nil) (defaults to: nil)

    Additional headers from the multipart field

  • content_encoding (String, nil) (defaults to: nil)

    Encoding type (e.g., “base64”)



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

#contentString (readonly)

Get the raw content as a string

Returns:

  • (String)

    Raw file content



118
119
120
# File 'lib/spikard/upload_file.rb', line 118

def content
  @content
end

#content_typeString (readonly)

Returns MIME type of the uploaded file.

Returns:

  • (String)

    MIME type of the uploaded file



29
30
31
# File 'lib/spikard/upload_file.rb', line 29

def content_type
  @content_type
end

#filenameString (readonly)

Returns Original filename from the client.

Returns:

  • (String)

    Original filename from the client



26
27
28
# File 'lib/spikard/upload_file.rb', line 26

def filename
  @filename
end

#headersHash<String, String> (readonly)

Returns Additional headers associated with this file field.

Returns:

  • (Hash<String, String>)

    Additional headers associated with this file field



35
36
37
# File 'lib/spikard/upload_file.rb', line 35

def headers
  @headers
end

#sizeInteger (readonly)

Returns Size of the file in bytes.

Returns:

  • (Integer)

    Size of the file in bytes



32
33
34
# File 'lib/spikard/upload_file.rb', line 32

def size
  @size
end

Instance Method Details

#closenil

Close the file (no-op for StringIO-based implementation)

Returns:

  • (nil)


104
105
106
# File 'lib/spikard/upload_file.rb', line 104

def close
  @io.close
end

#closed?Boolean

Check if file is closed

Returns:

  • (Boolean)


111
112
113
# File 'lib/spikard/upload_file.rb', line 111

def closed?
  @io.closed?
end

#read(size = nil) ⇒ String

Read file contents

Parameters:

  • size (Integer, nil) (defaults to: nil)

    Number of bytes to read (nil for all remaining)

Returns:

  • (String)

    File contents



65
66
67
# File 'lib/spikard/upload_file.rb', line 65

def read(size = nil)
  @io.read(size)
end

#rewindInteger

Rewind to the beginning of the file

Returns:

  • (Integer)

    Always returns 0



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

Parameters:

  • offset (Integer)

    Byte offset

  • whence (Integer) (defaults to: IO::SEEK_SET)

    Position reference (IO::SEEK_SET, IO::SEEK_CUR, IO::SEEK_END)

Returns:

  • (Integer)

    New position



82
83
84
# File 'lib/spikard/upload_file.rb', line 82

def seek(offset, whence = IO::SEEK_SET)
  @io.seek(offset, whence)
end

#tellInteger Also known as: pos

Get current position in the file

Returns:

  • (Integer)

    Current byte offset



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

Parameters:

  • encoding (String) (defaults to: 'UTF-8')

    Character encoding (defaults to UTF-8)

Returns:

  • (String)

    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