class Pdfmd

Attributes

filename[RW]
logfile[RW]
logstatus[RW]

Public Class Methods

new(filename) click to toggle source
# File lib/pdfmd.rb, line 41
def initialize(filename)

  # Default Logfile location and logging enabled
  if !@logfile or @logfile.empty?
    @logfile = Dir.pwd.chomp('/') + '/.pdfmd.log'
  end
  @log      = true

  # Defining the loglevel
  @loglevel = 'info'
  self.log('debug','---')
  self.log('info',"Starting with file '#{filename}'.")
  @filename  = filename
  @hieradata = queryHiera('pdfmd::config')
  if ! filename.empty?
    read_metatags(@filename)
  end

end

Public Instance Methods

check_metatags(metatags = []) click to toggle source

Check all or certain metatags If there is no content for a tag, return false

# File lib/pdfmd.rb, line 94
def check_metatags(metatags = [])

  if metatags.is_a?(String)
    metatags = metatags.split
  elsif !metatags.is_a?(Array)
    self.log('error', 'Array or string parameter expected for parameter of check_metatags.')
    exit 1
  end

  metatags.each do |value|
    if @@metadata[value].to_s.empty?
      false
    end
  end

end
metadata() click to toggle source

Make Metadata available to the outside

# File lib/pdfmd.rb, line 63
def metadata
  @@metadata
end
readUserInput(textstring = 'Enter value: ') click to toggle source

Read user input

# File lib/pdfmd.rb, line 200
def readUserInput(textstring = 'Enter value: ')

  self.log('info','Waiting for user input.')
  if textstring.match(/password/i)
    print textstring
    STDIN.noecho(&:gets).chomp + "\n"
  else
    ask textstring
  end

end
read_metatags(filename) click to toggle source

Read metatags from @metadata froma file into @@metadata

# File lib/pdfmd.rb, line 113
def read_metatags(filename)

  # Setup the metatags
  commandparameter = '-Warning'
  @@default_tags.each do |key|
    @@metadata[key] = ''
    commandparameter = commandparameter + " -#{key}"
  end

  if not File.file?(filename)
    self.log('error', "Cannot access file '#{filename}'.")
    puts "Cannot access file for reading metatags '#{filename}'. Abort"
    abort
  end

  metastrings = `exiftool #{commandparameter} '#{filename}'`.split("\n")

  # Assume an error (to enter the loop)
  metaPasswordError = true

  # Repeat password request to user until a valid password has been provided.
  # This loop can surely be made prettier.
  while metaPasswordError

    metaPasswordError = false
    metastrings.each do |metatag|
      if metatag.match(/warning.*password protected/i)
        self.log('info',"File '#{filename}' is password protected.")
        metaPasswordError = true
      end
    end

    # Leave this loop if there is no error in accessing the document
    if !metaPasswordError
      break
    end

    triedHieraPassword ||= false
    triedManualPassword ||= 0
    # Try a hiera password first, request otherwise from the user
    if documentPassword = self.determineValidSetting(nil, 'default:password') and
      !triedHieraPassword

      self.log('debug','Using default password from hiera.')
      @@documentPassword = documentPassword
      triedHieraPassword = true

    else

      # Message output if default password was not working
      if triedHieraPassword and triedManualPassword == 0
        self.log('warn','Default password from hiera is invalid.')
      end

      # Exit loop if there were more than three manual password inputs
      if triedManualPassword == 3
        self.log('error',"More than three password attempts on file '#{filename}'. Abort.")
        exit 1
      end

      # Request password from user
      self.log('info', 'Requesting password from user.')
      @@documentPassword = readUserInput('Document password : ').chomp
      triedManualPassword = 1 + triedManualPassword
      puts ''
    end

    metastrings = `exiftool -password '#{@@documentPassword}' #{commandparameter} '#{filename}'`.split("\n")

  end


  # NB: Maybe the output format should be changed here to catch keywords
  # matching the split string ('   : '). Exiftool has a format output option as well.
  self.log('debug', "Reading metadata from file '#{filename}'.")
  metastrings.each do |key|
    value = key.split('    : ')
    metatag = value[0].downcase.gsub(/ /,'')
    if @@metadata.has_key?( metatag )
      @@metadata[ metatag ] = value[1]
    end
  end

end