Class: Plugins::PDFinfo

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, Cinch::Helpers, Cinch::Plugin
Defined in:
lib/Zeta/plugins/pdfinfo.rb

Constant Summary collapse

DEFAULT_ALLOWED =

Default list of URL regexps to ignore.

[/\.pdf$/i].freeze
FILE_SIZE_LIMIT =
4000000

Instance Method Summary collapse

Methods included from Cinch::Plugin

#check?, #log2chan

Instance Method Details

#execute(msg, url) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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