Class: AlForm::UploadedFile

Inherits:
Object
  • Object
show all
Defined in:
lib/al_form/multipart.rb

Overview

テンポラリファイル生成

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ UploadedFile

constructor



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/al_form/multipart.rb', line 166

def initialize( obj )
  @file = nil
  100.times do
    # make filename
    @path = File.join( AlFile.dirname, "al_tmp#{$$}_#{rand(99999999)}" )
    begin
      @file = File.open( @path, File::RDWR|File::CREAT|File::EXCL, 0600 )
      break
    rescue =>ex
      # next loop
    end
  end

  if ! @file
    raise "Can't create temporary file. Fix an AL_TEMPDIR parameter in al_config.rb file, or AlFile.dirname setting."
  end

  ObjectSpace.define_finalizer( obj, UploadedFile::remove_uploaded_file( @path.dup ) )
end

Instance Attribute Details

#fileObject (readonly)

ファイルオブジェクト



145
146
147
# File 'lib/al_form/multipart.rb', line 145

def file
  @file
end

#pathObject (readonly)

ファイルパス



142
143
144
# File 'lib/al_form/multipart.rb', line 142

def path
  @path
end

Class Method Details

.remove_uploaded_file(fname) ⇒ Object

アップロードファイル消去用ファイナライザ

(note) テンポラリファイル消去をAlFormオブジェクトのファイナライザにして UploadedFileオブジェクトの参照がなくなってもファイル自体の存在は、 AlFormオブジェクトの生存期間と合致させる。



156
157
158
159
160
# File 'lib/al_form/multipart.rb', line 156

def self.remove_uploaded_file( fname )
  proc {
    File.unlink( fname ) rescue 0
  }
end