Class: Downspout::Tmpfile
- Inherits:
-
File
- Object
- File
- Downspout::Tmpfile
- Defined in:
- lib/downspout/tmp_file.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = nil) ⇒ Tmpfile
constructor
accepts an options hash which can include either or both of :name and :prefix then creates a Tempfile with the optionally given name in a unique sub-folder of the configured directory, optionally named with the prefix string.
Constructor Details
#initialize(options = nil) ⇒ Tmpfile
accepts an options hash which can include either or both of :name and :prefix then creates a Tempfile with the optionally given name in a unique sub-folder of the configured directory, optionally named with the prefix string. The unique folder name includes the prefix, a sortable date, the PID of the download process, and a randomly generated sequence of characters.
=> "/tmp/downloads/my-app-20110203-59488-1run8k2-0/desired-file-name.txt"
call-seq: Downspout::Tmpfile.new( :name => ‘desired-file-name.txt’, :prefix => ‘my-app’ )
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/downspout/tmp_file.rb', line 19 def initialize( = nil ) # make sure the configured directory exists FileUtils.mkdir_p( Downspout::Config.tmp_dir ) defaults = {:prefix => Downspout::Config.default_prefix, :name => "downloaded_file.tmp"} # overwrite defaults with given options defaults.merge!( ) unless .nil? # create a unique file path from the given options unique_path = File.join( Downspout::Config.tmp_dir, tmp_dir_name( defaults[:prefix] ), defaults[:name] ) # make sure the unique directory exists $logger.debug("downspout | tmpfile | initialize | Creating unique directory : #{File.dirname(unique_path)}") FileUtils.mkdir_p( File.dirname( unique_path ) ) raise "MakeDir Error" unless File.exist?( File.dirname( unique_path ) ) super( unique_path, File::CREAT, 0644 ) end |
Class Method Details
.clean_dir(dir_path, delay = 30) ⇒ Object
:nodoc:
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/downspout/tmp_file.rb', line 40 def self.clean_dir( dir_path, delay=30 ) #:nodoc: # remove files older than DELAY (in minutes) from configured folder delay = 30 unless (delay.class == Fixnum) t0 = Time.now - ( delay * 60 ) return false unless File.exist?( dir_path ) the_dir = Dir.new( dir_path ) $logger.debug( "downspout | tmpfile | clean_dir | start | Entries : #{the_dir.entries.size}" ) the_dir.entries.each do |item| next if item == "." next if item == ".." $logger.debug( "downspout | tmpfile | clean_dir | sub item : #{item}" ) item_path = File.join( dir_path, item ) tx = File.mtime( item_path ).utc # skip files with modtime changed less than sixty minutes ago next unless (tx < t0) if File.directory?( item_path ) then $logger.debug( "downspout | tmpfile | clean_dir | Removing Directory : #{item}/*" ) clean_dir( item_path, delay ) begin FileUtils.rmdir( item_path ) rescue Exception => e $logger.debug( "downspout | tmpfile | clean_dir | Exception : #{e}" ) return false end else $logger.debug( "downspout | tmpfile | clean_dir | Removing Item : #{item}" ) FileUtils.rm( item_path ) end end $logger.debug( "downspout | tmpfile | clean_download_dir | finish | Entries : #{the_dir.entries.size}" ) return true end |