This class represents an uploaded file.
@author Lars Olsson @since 18-08-2011
Initializes a new Ramaze::Helper::Upload::UploadedFile object.
@param [String] filename Suggested file name @param [String] type MIME-type @param [File] tempfile temporary file @param [Hash] options Options for uploaded files. Options supported
match those available to Ramaze::Helper::Upload::ClassMethods#upload_options
@return [Ramaze::Helper::Upload::UploadedFile] A new
Ramaze::Helper::Upload::UploadedFile object
@see # @see Ramaze::Helper::Upload::ClassMethods#upload_options
# File lib/ramaze/helper/upload.rb, line 378 378: def initialize(filename, type, tempfile, options) 379: @filename = File.basename(filename) 380: @type = type 381: @tempfile = tempfile 382: @realfile = nil 383: 384: trait :options => options 385: end
Changes the suggested filename of this Ramaze::Helper::Upload::UploadedFile. name should be a string representing the filename (only the filename, not a complete path), but if you provide a complete path this method it will try to identify the filename and use that instead.
@param [String] The new suggested filename.
# File lib/ramaze/helper/upload.rb, line 396 396: def filename=(name) 397: @filename = File.basename(name) 398: end
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.
@return [String|nil]
# File lib/ramaze/helper/upload.rb, line 407 407: def path 408: return self.saved? ? @realfile.path : nil 409: end
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.
@param [String] path Path where the
Ramaze::Helper::Upload::UploadedFile will be saved
@param [Hash] options Options for uploaded files. Options supported
match those available to Ramaze::Helper::Upload::ClassMethods#upload_options
@raise [StandardError] Will be raised if the save operation fails. @see # @see Ramaze::Helper::Upload::ClassMethods#upload_options
# File lib/ramaze/helper/upload.rb, line 431 431: def save(path = nil, options = {}) 432: # Merge options 433: opts = trait[:options].merge(options) 434: 435: unless path 436: # No path was provided, use info stored elsewhere to try to build 437: # the path 438: unless opts[:default_upload_dir] 439: raise StandardError.new('Unable to save file, no dirname given') 440: end 441: 442: unless @filename 443: raise StandardError.new('Unable to save file, no filename given') 444: end 445: 446: # Check to see if a proc or a string was used for the 447: # default_upload_dir parameter. If it was a proc, call the proc and 448: # use the result as the directory part of the path. If a string was 449: # used, use the string directly as the directory part of the path. 450: dn = opts[:default_upload_dir] 451: 452: if dn.respond_to?(:call) 453: dn = dn.call 454: end 455: 456: path = File.join(dn, @filename) 457: end 458: 459: path = File.expand_path(path) 460: 461: # Abort if file altready exists and overwrites are not allowed 462: if File.exists?(path) and !opts[:allow_overwrite] 463: raise StandardError.new('Unable to overwrite existing file') 464: end 465: 466: # Confirm that we can read source file 467: unless File.readable?(@tempfile.path) 468: raise StandardError.new('Unable to read temporary file') 469: end 470: 471: # Confirm that we can write to the destination file 472: unless (File.exists?(path) and File.writable?(path)) or (File.exists?(File.dirname(path)) and File.writable?(File.dirname(path))) 473: raise StandardError.new( 474: "Unable to save file to #{path}. Path is not writable" 475: ) 476: end 477: 478: # If supported, use IO,copy_stream. If not, require fileutils 479: # and use the same method from there 480: if IO.respond_to?(:copy_stream) 481: IO.copy_stream(@tempfile, path) 482: else 483: require 'fileutils' 484: File.open(@tempfile.path, 'rb') do |src| 485: File.open(path, 'wb') do |dest| 486: FileUtils.copy_stream(src, dest) 487: end 488: end 489: end 490: 491: # Update the realfile property, indicating that the file has been 492: # saved 493: @realfile = File.new(path) 494: 495: # If the unlink_tempfile option is set to true, delete the temporary 496: # file created by Rack 497: unlink_tempfile if opts[:unlink_tempfile] 498: end
Returns whether the Ramaze::Helper::Upload::UploadedFile has been saved or not.
@return [Boolean]
# File lib/ramaze/helper/upload.rb, line 508 508: def saved? 509: return !@realfile.nil? 510: end
Deletes the temporary file associated with this Ramaze::Helper::Upload::UploadedFile immediately.
# File lib/ramaze/helper/upload.rb, line 516 516: def unlink_tempfile 517: File.unlink(@tempfile.path) 518: @tempfile = nil 519: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.