Class: Pdfmd

Inherits:
Object
  • Object
show all
Includes:
Pdfmdmethods
Defined in:
lib/pdfmd.rb

Constant Summary collapse

@@default_tags =
['createdate', 'author', 'title', 'subject', 'keywords']
@@documentPassword =

Default document password

''
@@metadata =

Document metadata, read from the document

Hash.new
@@hieradata =

Hiera configuration data

Hash.new
@@edit_separator =

Field seperator for edit tags

'='

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pdfmdmethods

#determineValidSetting, #log, #queryHiera

Constructor Details

#initialize(filename) ⇒ Pdfmd

Returns a new instance of Pdfmd.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



16
17
18
# File 'lib/pdfmd.rb', line 16

def filename
  @filename
end

#logfileObject

Returns the value of attribute logfile.



16
17
18
# File 'lib/pdfmd.rb', line 16

def logfile
  @logfile
end

#logstatusObject

Returns the value of attribute logstatus.



16
17
18
# File 'lib/pdfmd.rb', line 16

def logstatus
  @logstatus
end

Instance Method Details

#check_metatags(metatags = []) ⇒ Object

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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

#metadataObject

Make Metadata available to the outside



63
64
65
# File 'lib/pdfmd.rb', line 63

def 
  @@metadata
end

#read_metatags(filename) ⇒ Object

Read metatags from @metadata froma file into @@metadata



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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

#readUserInput(textstring = 'Enter value: ') ⇒ Object

Read user input



200
201
202
203
204
205
206
207
208
209
210
# 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