class Plugins::PDFinfo

Constants

DEFAULT_ALLOWED

Default list of URL regexps to ignore.

FILE_SIZE_LIMIT

Public Instance Methods

execute(msg, url) click to toggle source
# File lib/Zeta/plugins/pdfinfo.rb, line 26
def execute(msg, url)

  allowedlist = DEFAULT_ALLOWED.dup
  allowedlist.concat(config[:blacklist]) if config[:blacklist]

  return unless allowedlist.any? { |entry| url =~ entry }
  debug "URL matched: #{url}"

  # Grab header and check filesize
  # one line to make request
  head = Faraday.head url

  # example with headers
  file_size = head.headers['Content-Length']
  humanize_size = Humanize::Byte.new(file_size.to_i).to_k.to_s.to_i.round(2)
  if file_size.to_i > FILE_SIZE_LIMIT
    return msg.reply("PDF → Unable to parse. file too big #{humanize_size}kb")
  end


  # Get file and parse metadata
  open(url, "rb") do |io|
    reader = PDF::Reader.new(io)
    creator = reader.info[:Creator] || 'Anon'
    producer = reader.info[:Producer] || 'Anon'
    creation = reader.info[:CreationDate] || 'now'
    modification = reader.info[:ModDate] || 'now'
    title = reader.info[:Title] || nil
    display = title ? title : "Title: None <> Creator: #{creator} <> Producer: #{producer} <> Creation: #{creation}"
    msg.reply "PDF (#{humanize_size}kb) → #{display}"
  end


rescue => e
  error "#{e.class.name}: #{e.message}"
end