lib/email-fetch-and-process.rb
# Class tp encapsulate a fetch and handle job.
Class tp encapsulate a fetch and handle job.
Class tp encapsulate a fetch and handle job .
def handle_parts(parts, job, msg_ids)
    parts.each do |part|
      if !part.parts.empty?
        handle_parts(part.parts, job, msg_ids)
      else
        begin
          name = if job.filename.to_s.empty?
                   part.header[:content_disposition].filename || part.filename rescue part.filename
                 else
                   job.filename
                 end
          name = Mail::Encodings.decode_encode(name, :decode) if name rescue name
        rescue StandardError => e
          puts e, e.backtrace.inspect
          next
        end
        next unless name
        @body_index += 1
        attachment = part
        final_destination = job.destination || @destination
        attachment_path = File.expand_path(File.join(final_destination, name))
        attachment_path = nil unless attachment_path =~ /^#{final_destination}/
        next unless final_destination != attachment_path
        if attachment && attachment_path
          file_path = File.join([final_destination, job.subdirectory, name].compact)
          atch = attachment.body.to_s
          FileUtils.mkdir_p File.dirname(file_path) unless FileTest.exist? File.dirname(file_path)
          File.open(file_path, 'wb+') do |fh|
            fh.write atch.respond_to?(:each) ? atch.join : atch
          end
          sha_new = `/usr/bin/shasum "#{file_path}"`.split(/\s+/).first
          sha_old = nil
          FileTest.exist?("#{file_path}.sha") &&
            File.open("#{file_path}.sha", 'r') { |fh| sha_old = fh.read.chomp }
          if sha_new != sha_old
            command_to_run = job.action.gsub(/FILEPATH/, Shellwords.escape(file_path)).
              gsub(/DESTINATION/, Shellwords.escape(final_destination))
            system(command_to_run) &&
              File.open("#{file_path}.sha", 'w+') { |fh| fh.write sha_new }
          end
        end
      end
    end
  end
def handle_parts ( parts , job , msg_ids ) parts . each do | part | if ! part . parts . empty? handle_parts ( part . parts , job , msg_ids ) else begin name = if job . filename . to_s . empty? part . header [ :content_disposition ] . filename || part . filename rescue part . filename else job . filename end name = Mail :: Encodings . decode_encode ( name , :decode ) if name rescue name rescue StandardError => e puts e , e . backtrace . inspect next end next unless name @body_index += 1 attachment = part final_destination = job . destination || @destination attachment_path = File . expand_path ( File . join ( final_destination , name ) ) attachment_path = nil unless attachment_path =~ / ^ #{ final_destination } / next unless final_destination != attachment_path if attachment && attachment_path file_path = File . join ( [ final_destination , job . subdirectory , name ] . compact ) atch = attachment . body . to_s FileUtils . mkdir_p File . dirname ( file_path ) unless FileTest . exist? File . dirname ( file_path ) File . open ( file_path , 'wb+' ) do | fh | fh . write atch . respond_to? ( :each ) ? atch . join : atch end sha_new = ` /usr/bin/shasum " #{ file_path } " ` . split ( / \s + / ) . first sha_old = nil FileTest . exist? ( "#{file_path}.sha" ) && File . open ( "#{file_path}.sha" , 'r' ) { | fh | sha_old = fh . read . chomp } if sha_new != sha_old command_to_run = job . action . gsub ( / FILEPATH / , Shellwords . escape ( file_path ) ) . gsub ( / DESTINATION / , Shellwords . escape ( final_destination ) ) system ( command_to_run ) && File . open ( "#{file_path}.sha" , 'w+' ) { | fh | fh . write sha_new } end end end end end
Class tp encapsulate a fetch and handle job.
Class tp encapsulate a fetch and handle job .
parts
<not_specific>
job
<not_specific>
msg_ids
<not_specific>