Class: Ramaze::Helper::Upload::UploadedFile
- Inherits:
-
Object
- Object
- Ramaze::Helper::Upload::UploadedFile
- Includes:
- Traited
- Defined in:
- lib/ramaze/helper/upload.rb
Overview
This class represents an uploaded file.
Instance Attribute Summary collapse
-
#filename ⇒ String
Suggested file name.
-
#type ⇒ String
readonly
MIME-type.
Instance Method Summary collapse
-
#initialize(filename, type, tempfile, options) ⇒ Ramaze::Helper::Upload::UploadedFile
constructor
Initializes a new Ramaze::Helper::Upload::UploadedFile object.
-
#path ⇒ String|nil
Returns the path of the Ramaze::Helper::Upload::UploadedFile object.
-
#save(path = nil, options = {}) ⇒ Object
Saves the Ramaze::Helper::Upload::UploadedFile.
-
#saved? ⇒ Boolean
Returns whether the Ramaze::Helper::Upload::UploadedFile has been saved or not.
-
#unlink_tempfile ⇒ Object
Deletes the temporary file associated with this Ramaze::Helper::Upload::UploadedFile immediately.
Constructor Details
#initialize(filename, type, tempfile, options) ⇒ Ramaze::Helper::Upload::UploadedFile
Initializes a new Ramaze::Helper::Upload::UploadedFile object.
378 379 380 381 382 383 384 385 |
# File 'lib/ramaze/helper/upload.rb', line 378 def initialize(filename, type, tempfile, ) @filename = File.basename(filename) @type = type @tempfile = tempfile @realfile = nil trait :options => end |
Instance Attribute Details
#filename ⇒ String
Suggested file name
359 360 361 |
# File 'lib/ramaze/helper/upload.rb', line 359 def filename @filename end |
#type ⇒ String (readonly)
MIME-type
363 364 365 |
# File 'lib/ramaze/helper/upload.rb', line 363 def type @type end |
Instance Method Details
#path ⇒ String|nil
Returns the path of the Ramaze::Helper::Upload::UploadedFile object. The method will always return nil before save has been called on the Ramaze::Helper::Upload::UploadedFile object.
407 408 409 |
# File 'lib/ramaze/helper/upload.rb', line 407 def path return self.saved? ? @realfile.path : nil end |
#save(path = nil, options = {}) ⇒ Object
Saves the Ramaze::Helper::Upload::UploadedFile.
If path
is not set, the method checks whether there exists default options for the path and tries to use that instead.
If you need to override any options set in the controller (using upload_options) you can set the corresponding option in options
to override the behavior for this particular Ramaze::Helper::Upload::UploadedFile object.
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/ramaze/helper/upload.rb', line 431 def save(path = nil, = {}) # Merge options opts = trait[:options].merge() unless path # No path was provided, use info stored elsewhere to try to build # the path unless opts[:default_upload_dir] raise StandardError.new('Unable to save file, no dirname given') end unless @filename raise StandardError.new('Unable to save file, no filename given') end # Check to see if a proc or a string was used for the # default_upload_dir parameter. If it was a proc, call the proc and # use the result as the directory part of the path. If a string was # used, use the string directly as the directory part of the path. dn = opts[:default_upload_dir] if dn.respond_to?(:call) dn = dn.call end path = File.join(dn, @filename) end path = File.(path) # Abort if file altready exists and overwrites are not allowed if File.exists?(path) and !opts[:allow_overwrite] raise StandardError.new('Unable to overwrite existing file') end # Confirm that we can read source file unless File.readable?(@tempfile.path) raise StandardError.new('Unable to read temporary file') end # Confirm that we can write to the destination file unless (File.exists?(path) and File.writable?(path)) \ or (File.exists?(File.dirname(path)) \ and File.writable?(File.dirname(path))) raise StandardError.new( "Unable to save file to #{path}. Path is not writable" ) end # If supported, use IO,copy_stream. If not, require fileutils # and use the same method from there if IO.respond_to?(:copy_stream) IO.copy_stream(@tempfile, path) else require 'fileutils' File.open(@tempfile.path, 'rb') do |src| File.open(path, 'wb') do |dest| FileUtils.copy_stream(src, dest) end end end # Update the realfile property, indicating that the file has been # saved @realfile = File.new(path) # But no need to keep it open @realfile.close # If the unlink_tempfile option is set to true, delete the temporary # file created by Rack unlink_tempfile if opts[:unlink_tempfile] end |
#saved? ⇒ Boolean
Returns whether the Ramaze::Helper::Upload::UploadedFile has been saved or not.
510 511 512 |
# File 'lib/ramaze/helper/upload.rb', line 510 def saved? return !@realfile.nil? end |
#unlink_tempfile ⇒ Object
Deletes the temporary file associated with this Ramaze::Helper::Upload::UploadedFile immediately.
518 519 520 521 |
# File 'lib/ramaze/helper/upload.rb', line 518 def unlink_tempfile File.unlink(@tempfile.path) @tempfile = nil end |