docsplit-paperclip-processor 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +14 -0
- data/docsplit-paperclip-processor.gemspec +2 -0
- data/fixtures/not_a_docx.docx +0 -0
- data/fixtures/portable_document_format.pdf +0 -0
- data/fixtures/test.bin +0 -0
- data/fixtures/twopage.pdf +0 -0
- data/fixtures/word_xml.docx +0 -0
- data/lib/docsplit-paperclip-processor.rb +10 -10
- data/lib/docsplit-paperclip-processor/version.rb +1 -1
- data/spec/docsplit-paperclip-processor_spec.rb +21 -0
- data/spec/docsplit_image_spec.rb +55 -0
- data/spec/docsplit_pdf_spec.rb +61 -0
- data/spec/spec_helper.rb +7 -0
- metadata +94 -62
data/CHANGELOG
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
New in 0.2.0:
|
2
|
+
|
3
|
+
API CHANGE: Rails.logger no longer called
|
4
|
+
Bug Fix: Was calling PaperclipError, now calls Paperclip::Error
|
5
|
+
Test Coverage: Specs added for DocsplitProcessor, DocsplitPdf, DocsplitImage.
|
6
|
+
Feature: Added docsplit as a dependency
|
7
|
+
Feature: Added rspec as a development dependency
|
8
|
+
|
9
|
+
New in 0.1.2:
|
10
|
+
Feature: Using FileMagic to determine MIME type
|
11
|
+
|
12
|
+
New in 0.1.1:
|
13
|
+
Feature: Passing PDF on if PDF conversion is attempted on a PDF
|
14
|
+
Feature: Added Docsplit.extract_image processor
|
@@ -16,6 +16,8 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
s.add_dependency "paperclip", "~> 2.4"
|
18
18
|
s.add_dependency 'ruby-filemagic'
|
19
|
+
s.add_dependency 'docsplit'
|
20
|
+
s.add_development_dependency 'rspec'
|
19
21
|
|
20
22
|
s.files = `git ls-files`.split("\n")
|
21
23
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
Binary file
|
Binary file
|
data/fixtures/test.bin
ADDED
Binary file
|
Binary file
|
Binary file
|
@@ -12,6 +12,10 @@ module Paperclip
|
|
12
12
|
@attachment = attachment
|
13
13
|
@basename = File.basename(@file.path, '.*')
|
14
14
|
end
|
15
|
+
|
16
|
+
def src_path
|
17
|
+
File.expand_path(@src.path)
|
18
|
+
end
|
15
19
|
end
|
16
20
|
|
17
21
|
class DocsplitChaining < Processor
|
@@ -31,26 +35,24 @@ module Paperclip
|
|
31
35
|
class DocsplitPdf < DocsplitProcessor
|
32
36
|
def make
|
33
37
|
begin
|
34
|
-
src_path = File.expand_path(@src.path)
|
35
38
|
dst_dir = Dir.tmpdir
|
36
39
|
dst_path = File.join(dst_dir, "#{@basename}.pdf")
|
37
40
|
|
38
|
-
if pdf_format?
|
41
|
+
if pdf_format?
|
39
42
|
dst_path = File.join(dst_dir, "_#{@basename}.pdf")
|
40
43
|
FileUtils.copy_file(src_path, dst_path)
|
41
44
|
else
|
42
45
|
Docsplit.extract_pdf(src_path, :output => dst_dir)
|
43
46
|
end
|
44
47
|
rescue Exception => e
|
45
|
-
|
46
|
-
raise PaperclipError, "There was an error converting #{@basename} to pdf"
|
48
|
+
raise Paperclip::Error, "There was an error converting #{@basename} to pdf"
|
47
49
|
end
|
48
50
|
File.open(dst_path)
|
49
51
|
end
|
50
52
|
|
51
|
-
def pdf_format?
|
53
|
+
def pdf_format?
|
52
54
|
file_magic = FileMagic.new
|
53
|
-
type = file_magic.file(
|
55
|
+
type = file_magic.file(src_path)
|
54
56
|
file_magic.close
|
55
57
|
type =~ /pdf/i
|
56
58
|
end
|
@@ -59,17 +61,15 @@ module Paperclip
|
|
59
61
|
class DocsplitImage < DocsplitProcessor
|
60
62
|
def make
|
61
63
|
begin
|
62
|
-
src_path = File.expand_path(@src.path)
|
63
64
|
dst_path = Dir.tmpdir
|
64
65
|
pages = options[:pages] || [1]
|
65
66
|
options = @options.merge(:output => dst_path)
|
66
67
|
|
67
68
|
Docsplit.extract_images(src_path, options)
|
68
69
|
rescue Exception => e
|
69
|
-
|
70
|
-
raise PaperclipError, "There was an error extracting images from #{@basename}"
|
70
|
+
raise Paperclip::Error, "There was an error extracting images from #{@basename}"
|
71
71
|
end
|
72
72
|
File.open(File.join(dst_path, "#{@basename}_#{pages.first}.#{@options[:format]}"))
|
73
73
|
end
|
74
74
|
end
|
75
|
-
end
|
75
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Paperclip::DocsplitProcessor do
|
4
|
+
it "has an accessible file attribute" do
|
5
|
+
d = Paperclip::DocsplitProcessor.new(File.open("./fixtures/word_xml.docx"))
|
6
|
+
d.src.path.should eq(File.open("./fixtures/word_xml.docx").path)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "has an accessible options hash attribute" do
|
10
|
+
options = {:option_1 => 1, :option_2 => 2}
|
11
|
+
d = Paperclip::DocsplitProcessor.new(File.open("./fixtures/word_xml.docx"), options)
|
12
|
+
d.options.should eq(options)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "has an accessible Paperclip::Attachment attribute" do
|
16
|
+
attachment = Paperclip::Attachment.new(nil, nil)
|
17
|
+
|
18
|
+
d = Paperclip::DocsplitProcessor.new(File.open("./fixtures/word_xml.docx"), {}, attachment)
|
19
|
+
d.attachment.should eq(attachment)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Paperclip::DocsplitImage do
|
4
|
+
def pdf_jpg_images
|
5
|
+
Dir.entries(Dir.tmpdir).reject{ |x| !(x =~ /twopage_\d.jpg/) }
|
6
|
+
end
|
7
|
+
|
8
|
+
context "with a valid pdf file attachment" do
|
9
|
+
before(:all) do
|
10
|
+
pdf_jpg_images.each do |tempfile|
|
11
|
+
File.delete(File.join(Dir.tmpdir, tempfile))
|
12
|
+
end
|
13
|
+
|
14
|
+
@file = File.open("./fixtures/twopage.pdf")
|
15
|
+
@processor = Paperclip::DocsplitImage.new(@file, {:format => :jpg, :size => "50x50"})
|
16
|
+
@output = @processor.make
|
17
|
+
end
|
18
|
+
|
19
|
+
after(:all) do
|
20
|
+
@file.close
|
21
|
+
end
|
22
|
+
|
23
|
+
it "#make generates an image for each page of the document" do
|
24
|
+
pdf_jpg_images.count.should eq(2)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "#make generates images at the specified resolution" do
|
28
|
+
cmd = %Q[identify -format "%wx%h" "#{@output.path}"]
|
29
|
+
`#{cmd}`.chomp.should eq("39x50")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "#make generates images in the specified format" do
|
33
|
+
pdf_jpg_images.each do |output_file|
|
34
|
+
FileMagic.new.file(File.join(Dir.tmpdir, output_file)).should =~ /jpeg/i
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "#make returns the image of the first page" do
|
39
|
+
File.basename(@output).should eq('twopage_1.jpg')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when processing fails" do
|
44
|
+
it "#make raises an error if the processing was unsuccessful" do
|
45
|
+
@file = File.open("./fixtures/twopage.pdf")
|
46
|
+
Dir.stub!(:tmpdir).and_return(:raise)
|
47
|
+
|
48
|
+
lambda {
|
49
|
+
Paperclip::DocsplitImage.new(@file, {:format => :jpg}).make
|
50
|
+
}.should raise_error(Paperclip::Error)
|
51
|
+
|
52
|
+
@file.close
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Paperclip::DocsplitPdf do
|
4
|
+
def make_output
|
5
|
+
Paperclip::DocsplitPdf.new(@file).make
|
6
|
+
end
|
7
|
+
|
8
|
+
context "with a Microsoft Word .docx file" do
|
9
|
+
before(:all) do
|
10
|
+
@file = File.open("./fixtures/word_xml.docx")
|
11
|
+
@output = make_output
|
12
|
+
end
|
13
|
+
|
14
|
+
after(:all) do
|
15
|
+
@file.close
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "produces a file with a .pdf extension" do
|
19
|
+
it "names the converted document with a .pdf extension" do
|
20
|
+
File.extname(@output.path).should eq(".pdf")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "produces PDF-format file" do
|
24
|
+
FileMagic.new.file(@output.path).should =~ /pdf/i
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "passes the document on if it is already a PDF" do
|
30
|
+
@file = File.open("./fixtures/portable_document_format.pdf")
|
31
|
+
|
32
|
+
lambda {
|
33
|
+
FileMagic.new.file(make_output.path).should =~ /pdf/i
|
34
|
+
}.should_not raise_error
|
35
|
+
|
36
|
+
@file.close
|
37
|
+
end
|
38
|
+
|
39
|
+
it "raises an error if PDF conversion was unsuccessful" do
|
40
|
+
@file = File.open("./fixtures/test.bin")
|
41
|
+
Dir.stub!(:tmpdir).and_return(:raise)
|
42
|
+
|
43
|
+
lambda {
|
44
|
+
make_output
|
45
|
+
}.should raise_error(Paperclip::Error)
|
46
|
+
|
47
|
+
@file.close
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#pdf_format?" do
|
51
|
+
it "identifies a PDF document as a PDF" do
|
52
|
+
processor = Paperclip::DocsplitPdf.new(File.open("./fixtures/portable_document_format.pdf"))
|
53
|
+
processor.pdf_format?.should be_true
|
54
|
+
end
|
55
|
+
|
56
|
+
it "identifies a non-PDF document as not a PDF" do
|
57
|
+
processor = Paperclip::DocsplitPdf.new(File.open("./fixtures/word_xml.docx"))
|
58
|
+
processor.pdf_format?.should be_false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,100 +1,132 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: docsplit-paperclip-processor
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 2
|
10
|
-
version: 0.1.2
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Tien Le
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-07-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: paperclip
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
18
|
+
requirements:
|
26
19
|
- - ~>
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 2
|
31
|
-
- 4
|
32
|
-
version: "2.4"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.4'
|
33
22
|
type: :runtime
|
34
|
-
|
35
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.4'
|
30
|
+
- !ruby/object:Gem::Dependency
|
36
31
|
name: ruby-filemagic
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
37
39
|
prerelease: false
|
38
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
41
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: docsplit
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
47
54
|
type: :runtime
|
48
|
-
|
49
|
-
|
50
|
-
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: This gem is simple Paperclip processor which uses Docsplit to convert
|
79
|
+
uploaded files to pdf, or extract information/thumbnails from them
|
80
|
+
email:
|
51
81
|
- tienlx@gmail.com
|
52
82
|
executables: []
|
53
|
-
|
54
83
|
extensions: []
|
55
|
-
|
56
84
|
extra_rdoc_files: []
|
57
|
-
|
58
|
-
files:
|
85
|
+
files:
|
59
86
|
- .gitignore
|
87
|
+
- CHANGELOG
|
60
88
|
- Gemfile
|
61
89
|
- README.md
|
62
90
|
- Rakefile
|
63
91
|
- docsplit-paperclip-processor.gemspec
|
92
|
+
- fixtures/not_a_docx.docx
|
93
|
+
- fixtures/portable_document_format.pdf
|
94
|
+
- fixtures/test.bin
|
95
|
+
- fixtures/twopage.pdf
|
96
|
+
- fixtures/word_xml.docx
|
64
97
|
- lib/docsplit-paperclip-processor.rb
|
65
98
|
- lib/docsplit-paperclip-processor/version.rb
|
99
|
+
- spec/docsplit-paperclip-processor_spec.rb
|
100
|
+
- spec/docsplit_image_spec.rb
|
101
|
+
- spec/docsplit_pdf_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
66
103
|
homepage: https://github.com/tienle/docsplit-paperclip-processor
|
67
104
|
licenses: []
|
68
|
-
|
69
105
|
post_install_message:
|
70
106
|
rdoc_options: []
|
71
|
-
|
72
|
-
require_paths:
|
107
|
+
require_paths:
|
73
108
|
- lib
|
74
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
110
|
none: false
|
76
|
-
requirements:
|
77
|
-
- -
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
|
80
|
-
|
81
|
-
- 0
|
82
|
-
version: "0"
|
83
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
116
|
none: false
|
85
|
-
requirements:
|
86
|
-
- -
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
|
89
|
-
segments:
|
90
|
-
- 0
|
91
|
-
version: "0"
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
92
121
|
requirements: []
|
93
|
-
|
94
122
|
rubyforge_project: docsplit-paperclip-processor
|
95
|
-
rubygems_version: 1.8.
|
123
|
+
rubygems_version: 1.8.24
|
96
124
|
signing_key:
|
97
125
|
specification_version: 3
|
98
126
|
summary: A Paperclip processor for Docsplit
|
99
|
-
test_files:
|
100
|
-
|
127
|
+
test_files:
|
128
|
+
- spec/docsplit-paperclip-processor_spec.rb
|
129
|
+
- spec/docsplit_image_spec.rb
|
130
|
+
- spec/docsplit_pdf_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
has_rdoc:
|