lib/rocket_job/dirmon_entry.rb in rocketjob-1.2.0 vs lib/rocket_job/dirmon_entry.rb in rocketjob-1.2.1

- old
+ new

@@ -211,34 +211,42 @@ cattr_accessor :default_archive_directory @@default_archive_directory = '_archive'.freeze # Returns [Pathname] the archive_directory if set, otherwise the default_archive_directory - def archive_pathname - Pathname.new(archive_directory || self.class.default_archive_directory) + # Creates the archive directory if one is set + def archive_pathname(file_pathname) + if archive_directory + path = Pathname.new(archive_directory) + path.mkpath unless path.exist? + path.realpath + else + file_pathname.dirname.join(self.class.default_archive_directory).realdirpath + end end # Passes each filename [Pathname] found that matches the pattern into the supplied block def each(&block) logger.tagged("DirmonEntry:#{id}") do - Pathname.glob(pattern).each do |pathname| + # Case insensitive filename matching + Pathname.glob(pattern, File::FNM_CASEFOLD).each do |pathname| next if pathname.directory? pathname = pathname.realpath file_name = pathname.to_s # Skip archive directories - next if file_name.start_with?(archive_pathname.realpath.to_s) + next if file_name.include?(self.class.default_archive_directory) # Security check? - if (@@whitelist_paths.size > 0) && @@whitelist_paths.none? { |whitepath| file_name.start_with?(whitepath) } - logger.warn "Ignoring file: #{file_name} since it is not in any of the whitelisted paths: #{whitelist_paths.join(', ')}" + if (whitelist_paths.size > 0) && whitelist_paths.none? { |whitepath| file_name.start_with?(whitepath) } + logger.error "Skipping file: #{file_name} since it is not in any of the whitelisted paths: #{whitelist_paths.join(', ')}" next end # File must be writable so it can be removed after processing unless pathname.writable? - logger.warn "Ignoring file: #{file_name} since it is not writable by the current user. Must be able to delete/move the file after queueing the job" + logger.error "Skipping file: #{file_name} since it is not writable by the current user. Must be able to delete/move the file after queueing the job" next end block.call(pathname) end end @@ -281,10 +289,15 @@ end end protected + # Instance method to return whitelist paths + def whitelist_paths + @@whitelist_paths + end + # Upload the file to the job def upload_file(job, pathname) if job.respond_to?(:file_store_upload) # Allow the job to determine what to do with the file # Pass the pathname as a string, not a Pathname (IO) instance @@ -314,10 +327,10 @@ # Returns [String] the fully qualified archived file name # # Note: # - Works across partitions when the file and the archive are on different partitions def archive_file(job, pathname) - target_path = archive_pathname + target_path = archive_pathname(pathname) target_path.mkpath target_file_name = target_path.join("#{job.id}_#{pathname.basename}") # In case the file is being moved across partitions FileUtils.move(pathname.to_s, target_file_name.to_s) target_file_name.to_s