[Groonga-commit] ranguba/chupa-text-decomposer-pdf at 91fd6e5 [master] Support encrypted PDF

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Feb 17 22:14:31 JST 2014


Kouhei Sutou	2014-02-17 22:14:31 +0900 (Mon, 17 Feb 2014)

  New Revision: 91fd6e5f3aeff6ed4cd1c8229232cf475c1925c0
  https://github.com/ranguba/chupa-text-decomposer-pdf/commit/91fd6e5f3aeff6ed4cd1c8229232cf475c1925c0

  Message:
    Support encrypted PDF

  Added files:
    test/fixture/encrypted.odt
    test/fixture/encrypted.pdf
  Modified files:
    lib/chupa-text/decomposers/pdf.rb
    test/test-pdf.rb

  Modified: lib/chupa-text/decomposers/pdf.rb (+23 -2)
===================================================================
--- lib/chupa-text/decomposers/pdf.rb    2014-02-16 23:45:42 +0900 (94c864f)
+++ lib/chupa-text/decomposers/pdf.rb    2014-02-17 22:14:31 +0900 (9e55e87)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2013-2014  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -29,7 +29,7 @@ module ChupaText
       end
 
       def decompose(data)
-        document = Poppler::Document.new(data.body)
+        document = create_document(data)
         text = ""
         document.each do |page|
           page_text = page.get_text
@@ -50,6 +50,27 @@ module ChupaText
       end
 
       private
+      def create_document(data)
+        begin
+          Poppler::Document.new(data.body, password(data))
+        rescue GLib::Error => error
+          case error.code
+          when Poppler::Error::ENCRYPTED.to_i
+            raise ChupaText::EncryptedError.new(data)
+          else
+            raise ChupaText::InvalidDataError.new(data, error.message)
+          end
+        end
+      end
+
+      def password(data)
+        password = @options[:password]
+        if password.respond_to?(:call)
+          password = password.call(data)
+        end
+        password
+      end
+
       def add_attribute(text_data, document, name)
         value = document.send(name)
         return if value.nil?

  Added: test/fixture/encrypted.odt (+0 -0) 100644
===================================================================
(Binary files differ)

  Added: test/fixture/encrypted.pdf (+0 -0) 100644
===================================================================
(Binary files differ)

  Modified: test/test-pdf.rb (+36 -7)
===================================================================
--- test/test-pdf.rb    2014-02-16 23:45:42 +0900 (1b71db4)
+++ test/test-pdf.rb    2014-02-17 22:14:31 +0900 (8a50651)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2013-2014  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -18,10 +18,14 @@ require "pathname"
 
 class TestPDF < Test::Unit::TestCase
   def setup
-    @decomposer = ChupaText::Decomposers::PDF.new({})
+    @options = {}
   end
 
   private
+  def decomposer
+    ChupaText::Decomposers::PDF.new(@options)
+  end
+
   def fixture_path(*components)
     base_path = Pathname(__FILE__).dirname + "fixture"
     base_path.join(*components)
@@ -37,11 +41,11 @@ class TestPDF < Test::Unit::TestCase
       end
 
       def test_pdf
-        assert_true(@decomposer.target?(create_data("index.pdf")))
+        assert_true(decomposer.target?(create_data("index.pdf")))
       end
 
       def test_html
-        assert_false(@decomposer.target?(create_data("index.html")))
+        assert_false(decomposer.target?(create_data("index.html")))
       end
     end
 
@@ -53,11 +57,11 @@ class TestPDF < Test::Unit::TestCase
       end
 
       def test_pdf
-        assert_true(@decomposer.target?(create_data("application/pdf")))
+        assert_true(decomposer.target?(create_data("application/pdf")))
       end
 
       def test_html
-        assert_false(@decomposer.target?(create_data("text/html")))
+        assert_false(decomposer.target?(create_data("text/html")))
       end
     end
   end
@@ -69,7 +73,7 @@ class TestPDF < Test::Unit::TestCase
       data.mime_type = "text/pdf"
 
       decomposed = []
-      @decomposer.decompose(data) do |decomposed_data|
+      decomposer.decompose(data) do |decomposed_data|
         decomposed << decomposed_data
       end
       decomposed
@@ -133,5 +137,30 @@ class TestPDF < Test::Unit::TestCase
         super(fixture_path("multi-pages.pdf"))
       end
     end
+
+    sub_test_case("encrypted") do
+      def test_with_password
+        @options = {:password => "encrypted"}
+        assert_equal(["Password is 'encrypted'."],
+                     decompose.collect(&:body))
+      end
+
+      def test_with_password_block
+        @options = {:password => lambda {|data| "encrypted"}}
+        assert_equal(["Password is 'encrypted'."],
+                     decompose.collect(&:body))
+      end
+
+      def test_without_password
+        assert_raise(ChupaText::EncryptedError) do
+          decompose
+        end
+      end
+
+      private
+      def decompose
+        super(fixture_path("encrypted.pdf"))
+      end
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
다운로드 



More information about the Groonga-commit mailing list
Back to archive index