Class: Jimmu::UploadedFile

Inherits:
Struct
  • Object
show all
Defined in:
lib/jimmu.rb

Overview

Represents one uploaded file from a multipart/form-data request. Data is held in memory as a binary String; call #save to write it to disk.

file = params[:image]
file.filename       # "photo.png"
file.content_type   # "image/png"
file.size           # 48213
file.save("public/uploads/photo.png")

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type

Returns:

  • (Object)

    the current value of content_type



396
397
398
# File 'lib/jimmu.rb', line 396

def content_type
  @content_type
end

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



396
397
398
# File 'lib/jimmu.rb', line 396

def data
  @data
end

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



396
397
398
# File 'lib/jimmu.rb', line 396

def filename
  @filename
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


408
409
410
# File 'lib/jimmu.rb', line 408

def empty?
  data.nil? || data.empty?
end

#save(path) ⇒ Object



401
402
403
404
405
406
# File 'lib/jimmu.rb', line 401

def save(path)
  dir = File.dirname(path)
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
  File.open(path, 'wb') { |f| f.write(data) }
  path
end

#sizeObject



397
398
399
# File 'lib/jimmu.rb', line 397

def size
  data.bytesize
end