Class: AlForm::UploadedFile
- Inherits:
-
Object
- Object
- AlForm::UploadedFile
- Defined in:
- lib/al_form/multipart.rb
Overview
テンポラリファイル生成
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
ファイルオブジェクト.
-
#path ⇒ Object
readonly
ファイルパス.
Class Method Summary collapse
-
.remove_uploaded_file(fname) ⇒ Object
アップロードファイル消去用ファイナライザ.
Instance Method Summary collapse
-
#initialize(obj) ⇒ UploadedFile
constructor
constructor.
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
#file ⇒ Object (readonly)
ファイルオブジェクト
145 146 147 |
# File 'lib/al_form/multipart.rb', line 145 def file @file end |
#path ⇒ Object (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 |