lib/onebox/engine/pdf_onebox.rb in onebox-1.8.0 vs lib/onebox/engine/pdf_onebox.rb in onebox-1.8.1

- old
+ new

@@ -1,7 +1,5 @@ -require 'pdf-reader' - module Onebox module Engine class PdfOnebox include Engine include LayoutSupport @@ -11,26 +9,23 @@ private def data html_entities = HTMLEntities.new - pdf_response = get_pdf_response - raise "Unable to read pdf file: #{@url}" if pdf_response.nil? + pdf_info = get_pdf_info + raise "Unable to read pdf file: #{@url}" if pdf_info.nil? - pdf_data = pdf_response.info - result = { link: link } - result['title'] = unless pdf_data[:Title].blank? - html_entities.decode(Onebox::Helpers.truncate(pdf_data[:Title].force_encoding("UTF-8").scrub.strip, 80)) - else - "PDF File" - end - result['description'] = html_entities.decode(Onebox::Helpers.truncate(pdf_data[:Subject].force_encoding("UTF-8").scrub.strip, 250)) rescue nil - result['author'] = pdf_data[:Author] unless pdf_data[:Author].blank? + result = { link: link, + title: pdf_info[:name], + filesize: pdf_info[:filesize] + } result end - def get_pdf_response - PDF::Reader.new(open(@url, read_timeout: Onebox.options.timeout)) + def get_pdf_info + uri = URI.parse(@url) + size = Onebox::Helpers.fetch_content_length(@url) + return {filesize: Onebox::Helpers.pretty_filesize(size.to_i), name: File.basename(uri.path)} rescue nil end end end