### Home Classes Methods

### In Files

- lib/ramaze/helper/upload.rb

### Parent

Object

### Methods

- ::new
- #filename=
- #path
- #save
- #saved?
- #unlink_tempfile

### Included Modules

- Ramaze::Traited

### Class/Module Index

Quicksearch

- Ramaze
- Ramaze::App
- Ramaze::AppGraph
- Ramaze::Bin
- Ramaze::Bin::Console
- Ramaze::Bin::Create
- Ramaze::Bin::Helper
- Ramaze::Bin::Restart
- Ramaze::Bin::Runner
- Ramaze::Bin::Start
- Ramaze::Bin::Status
- Ramaze::Bin::Stop
- Ramaze::Cache
- Ramaze::Cache::LRU
- Ramaze::Cache::LocalMemCache
- Ramaze::Cache::MemCache
- Ramaze::Cache::Redis
- Ramaze::Cache::Sequel
- Ramaze::Controller
- Ramaze::CoreExtensions
- Ramaze::CoreExtensions::Array
- Ramaze::CoreExtensions::Binding
- Ramaze::CoreExtensions::Kernel
- Ramaze::CoreExtensions::Numeric
- Ramaze::CoreExtensions::Object
- Ramaze::CoreExtensions::Proc
- Ramaze::CoreExtensions::String
- Ramaze::Current
- Ramaze::DefaultController
- Ramaze::Dictionary
- Ramaze::Fiber
- Ramaze::Files
- Ramaze::GemSetup
- Ramaze::Gestalt
- Ramaze::Helper
- Ramaze::Helper::Auth
- Ramaze::Helper::Bench
- Ramaze::Helper::BlueForm
- Ramaze::Helper::BlueForm::Form
- Ramaze::Helper::CSRF
- Ramaze::Helper::Cache
- Ramaze::Helper::Cache::SingletonMethods
- Ramaze::Helper::Disqus
- Ramaze::Helper::Email
- Ramaze::Helper::Erector
- Ramaze::Helper::Flash
- Ramaze::Helper::Formatting
- Ramaze::Helper::Gestalt
- Ramaze::Helper::Gravatar
- Ramaze::Helper::Identity
- Ramaze::Helper::Layout
- Ramaze::Helper::Layout::SingletonMethods
- Ramaze::Helper::Link
- Ramaze::Helper::Localize
- Ramaze::Helper::Localize::Dictionary
- Ramaze::Helper::Localize::Parser
- Ramaze::Helper::Markaby
- Ramaze::Helper::Maruku
- Ramaze::Helper::Paginate
- Ramaze::Helper::Paginate::Paginator
- Ramaze::Helper::Paginate::Paginator::ArrayPager
- Ramaze::Helper::Paginate::Paginator::DataMapperPager
- Ramaze::Helper::Remarkably
- Ramaze::Helper::RequestAccessor
- Ramaze::Helper::SendFile
- Ramaze::Helper::SimpleCaptcha
- Ramaze::Helper::Stack
- Ramaze::Helper::Thread
- Ramaze::Helper::Ultraviolet
- Ramaze::Helper::Upload
- Ramaze::Helper::Upload::ClassMethods
- Ramaze::Helper::Upload::UploadedFile
- Ramaze::Helper::UserHelper
- Ramaze::Helper::UserHelper::Wrapper
- Ramaze::Helper::XHTML
- Ramaze::LRUHash
- Ramaze::Logger
- Ramaze::Logger::Analogger
- Ramaze::Logger::Growl
- Ramaze::Logger::Informer
- Ramaze::Logger::Knotify
- Ramaze::Logger::LogHub
- Ramaze::Logger::Logger
- Ramaze::Logger::RotatingInformer
- Ramaze::Logger::Syslog
- Ramaze::Logger::Xosd
- Ramaze::Logging
- Ramaze::MiddlewareCompiler
- Ramaze::Plugin
- Ramaze::Reloader
- Ramaze::Reloader::Hooks
- Ramaze::Reloader::WatchInotify
- Ramaze::Reloader::WatchStat
- Ramaze::Request
- Ramaze::Response
- Ramaze::SpecLogger
- Ramaze::Struct
- Ramaze::View
- Ramaze::View::Erector
- Ramaze::View::Erubis
- Ramaze::View::Ezamar
- Ramaze::View::Gestalt
- Ramaze::View::Haml
- Ramaze::View::Liquid
- Ramaze::View::Liquid::Tag
- Ramaze::View::Lokar
- Ramaze::View::Mustache
- Ramaze::View::Mustache::RamazeContext
- Ramaze::View::Nagoro
- Ramaze::View::Remarkably
- Ramaze::View::Sass
- Ramaze::View::Slippers
- Ramaze::View::Tagz
- Ramaze::View::Tagz::Methods
- Ramaze::View::Tenjin
- Nagoro
- Nagoro::Pipe
- Nagoro::Pipe::RenderPartial
- Bacon
- Bacon::PrettyOutput
- Erector
- Erector::Widget
- Rack
- Rack::RouteExceptions
- Tenjin
- Tenjin::Template
- BlankSlate
- Controller
- Fiber
- FiberError
- MainController
- Object
- OrderedSet
- Thread

No matching classes.

# Ramaze::Helper::Upload::UploadedFile

This class represents an uploaded file.

@author Lars Olsson @since 18-08-2011

### Attributes

filename[R]

Suggested file name @return [String]

type[R]

MIME-type @return [String]

### Public Class Methods

new(filename, type, tempfile, options) click to toggle source

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 save @see Ramaze::Helper::Upload::ClassMethods#upload_options

    # File lib/ramaze/helper/upload.rb, line 378
    def initialize(filename, type, tempfile, options)
      @filename = File.basename(filename)
      @type     = type
      @tempfile = tempfile
      @realfile = nil

      trait :options => options
    end

### Public Instance Methods

filename=(name) click to toggle source

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
    def filename=(name)
      @filename = File.basename(name)
    end

path() click to toggle source

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
    def path
      return self.saved? ? @realfile.path : nil
    end

save(path = nil, options = {}) click to toggle source

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 initialize @see Ramaze::Helper::Upload::ClassMethods#upload_options

    # File lib/ramaze/helper/upload.rb, line 431
    def save(path = nil, options = {})
      # Merge options
      opts = trait[:options].merge(options)

      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.expand_path(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)

      # If the unlink_tempfile option is set to true, delete the temporary
      # file created by Rack
      unlink_tempfile if opts[:unlink_tempfile]
    end

saved?() click to toggle source

Returns whether the Ramaze::Helper::Upload::UploadedFile has been saved or not.

@return [Boolean]

    # File lib/ramaze/helper/upload.rb, line 508
    def saved?
      return !@realfile.nil?
    end

unlink_tempfile() click to toggle source

Deletes the temporary file associated with this Ramaze::Helper::Upload::UploadedFile immediately.

    # File lib/ramaze/helper/upload.rb, line 516
    def unlink_tempfile
      File.unlink(@tempfile.path)
      @tempfile = nil
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.