doc-thumbnailer 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem 'rubyzip'
7
+ gem 'quick_magick'
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :test, :development do
11
+ gem "rspec", "~> 2.10.0"
12
+ gem "rdoc", "~> 3.12"
13
+ gem "bundler", ">= 1.0.0"
14
+ gem "jeweler", "~> 1.8.4"
15
+ gem "simplecov", ">= 0"
16
+ gem "pry"
17
+ end
18
+
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ coderay (1.0.7)
5
+ diff-lcs (1.1.3)
6
+ git (1.2.5)
7
+ jeweler (1.8.4)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rdoc
12
+ json (1.7.3)
13
+ method_source (0.7.1)
14
+ multi_json (1.3.6)
15
+ pry (0.9.9.6)
16
+ coderay (~> 1.0.5)
17
+ method_source (~> 0.7.1)
18
+ slop (>= 2.4.4, < 3)
19
+ quick_magick (0.8.0)
20
+ rake (0.9.2.2)
21
+ rdoc (3.12)
22
+ json (~> 1.4)
23
+ rspec (2.10.0)
24
+ rspec-core (~> 2.10.0)
25
+ rspec-expectations (~> 2.10.0)
26
+ rspec-mocks (~> 2.10.0)
27
+ rspec-core (2.10.1)
28
+ rspec-expectations (2.10.0)
29
+ diff-lcs (~> 1.1.3)
30
+ rspec-mocks (2.10.1)
31
+ rubyzip (0.9.9)
32
+ simplecov (0.6.4)
33
+ multi_json (~> 1.0)
34
+ simplecov-html (~> 0.5.3)
35
+ simplecov-html (0.5.3)
36
+ slop (2.4.4)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ bundler (>= 1.0.0)
43
+ jeweler (~> 1.8.4)
44
+ pry
45
+ quick_magick
46
+ rdoc (~> 3.12)
47
+ rspec (~> 2.10.0)
48
+ rubyzip
49
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Pedro Henrique Linhares
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = thumbnailer
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to thumbnailer
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Pedro Henrique Linhares. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "doc-thumbnailer"
18
+ gem.homepage = "http://github.com/pedrolinhares/thumbnailer"
19
+ gem.license = "MIT"
20
+ gem.summary = "Generate thumbnails for document files"
21
+ gem.description = "Generate thumbnails for document files"
22
+ gem.email = "pedrolmota@gmail.com"
23
+ gem.authors = ["Pedro Henrique Linhares"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "thumbnailer #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,58 @@
1
+ require 'zip/zip'
2
+ require 'quick_magick'
3
+ require 'base64'
4
+ require 'tempfile'
5
+
6
+ module Thumbnailer
7
+ class << self
8
+ def create(file_path, mimetype, params = {})
9
+ Maker.new(file_path, mimetype, params).thumbnail
10
+ end
11
+ end
12
+
13
+ class Maker
14
+ def initialize(file_path, mimetype, params = {})
15
+ params = {width: 181, height: 256, string: false}.merge params
16
+ @file_path = file_path
17
+ @mimetype = mimetype
18
+ @width = params[:width]
19
+ @height = params[:height]
20
+ @return_string = params[:string]
21
+ end
22
+
23
+ def thumbnail
24
+ return odt_thumbnail if @mimetype =~ /opendocument.text/
25
+ return pdf_thumbnail if @mimetype =~ /pdf/
26
+ end
27
+
28
+ private
29
+
30
+ def odt_thumbnail
31
+ odt = Zip::ZipFile.open(@file_path)
32
+ thumb = odt.read("Thumbnails/thumbnail.png")
33
+
34
+ return Base64.encode64(thumb) if @return_string
35
+
36
+ thumb_file = File.new("thumbnail#{Time.now.to_i}.png", 'w')
37
+ thumb_file.write(thumb)
38
+ thumb_file.close
39
+ thumb_file.path
40
+ end
41
+
42
+ def pdf_thumbnail
43
+ pdf = QuickMagick::Image.read(@file_path) { |image| image.density = 300 }
44
+ pdf[0].resize "#{@width}x#{@height}"
45
+ thumb_name = "thumbnail#{Time.now.to_i}.png"
46
+
47
+ pdf[0].save thumb_name
48
+
49
+ if @return_string
50
+ string = Base64.encode64(File.open(thumb_name).read)
51
+ File.delete thumb_name
52
+ return string
53
+ end
54
+
55
+ thumb_name
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Thumbnailer::Maker do
4
+ describe "Types of files" do
5
+ it "accepts odt files" do
6
+ maker = Thumbnailer::Maker.new(File.expand_path(File.dirname(__FILE__) + "/resources/file.odt"),
7
+ "application/vnd.oasis.opendocument.text")
8
+ maker.should_receive(:odt_thumbnail)
9
+ maker.thumbnail
10
+ end
11
+
12
+ it "accepts pdf files" do
13
+ maker = Thumbnailer::Maker.new(File.expand_path(File.dirname(__FILE__) + "/resources/file.pdf"),
14
+ "application/pdf")
15
+ maker.should_receive(:pdf_thumbnail)
16
+ maker.thumbnail
17
+ end
18
+ end
19
+
20
+ it "returns a saved thumbnail with png extension" do
21
+ thumb_path = Thumbnailer::Maker.new(File.expand_path(File.dirname(__FILE__) + "/resources/file.odt"),
22
+ "application/vnd.oasis.opendocument.text").thumbnail
23
+ thumb_path =~ /.png/
24
+ end
25
+
26
+ it "returns a png thumbnail from a pdf" do
27
+ thumb_path = Thumbnailer::Maker.new(File.expand_path(File.dirname(__FILE__) + "/resources/file.pdf"),
28
+ "application/pdf").thumbnail
29
+
30
+ #TODO - Make it work..
31
+ #FileUtils.compare_file(thumb_path,
32
+ # File.expand_path(File.dirname(__FILE__) + "/resources/desired_pdf.png")).should be_true
33
+ end
34
+
35
+ it "returns a png thumbnail from a odt" do
36
+ thumb_path = Thumbnailer::Maker.new(File.expand_path(File.dirname(__FILE__) + "/resources/file.odt"),
37
+ "application/vnd.oasis.opendocument.text").thumbnail
38
+
39
+ FileUtils.compare_file(thumb_path,
40
+ File.expand_path(File.dirname(__FILE__) + "/resources/desired_odt.png")).should be_true
41
+ end
42
+
43
+ it 'with option string = true returns the base64 thumbnail string' do
44
+ thumb_string = Thumbnailer::Maker.new(File.expand_path(File.dirname(__FILE__) + "/resources/file.odt"),
45
+ "application/vnd.oasis.opendocument.text", string: true).thumbnail
46
+
47
+ #compare the resulting image
48
+ desired_thumb = File.open(File.expand_path(File.dirname(__FILE__) + "/resources/desired_odt.png"))
49
+ thumb_string.should == Base64.encode64(desired_thumb.read)
50
+ end
51
+ end
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'pry'
5
+ require 'thumbnailer'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Thumbnailer do
4
+ it "create should send thumbnail message to Maker" do
5
+ Thumbnailer::Maker.any_instance.should_receive(:thumbnail)
6
+ Thumbnailer.create(File.expand_path(File.dirname(__FILE__) + "/resources/file.odt"),
7
+ "application/vnd.oasis.opendocument.text")
8
+ end
9
+ end
@@ -0,0 +1,77 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "thumbnailer"
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Pedro Henrique Linhares"]
12
+ s.date = "2012-07-09"
13
+ s.description = "Generate thumbnails for document files"
14
+ s.email = "pedrolmota@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/thumbnailer.rb",
29
+ "spec/maker_spec.rb",
30
+ "spec/resources/desired_odt.png",
31
+ "spec/resources/desired_pdf.png",
32
+ "spec/resources/file.odt",
33
+ "spec/resources/file.pdf",
34
+ "spec/spec_helper.rb",
35
+ "spec/thumbnailer_spec.rb",
36
+ "thumbnailer.gemspec"
37
+ ]
38
+ s.homepage = "http://github.com/pedrolinhares/thumbnailer"
39
+ s.licenses = ["MIT"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = "1.8.17"
42
+ s.summary = "Generate thumbnails for document files"
43
+
44
+ if s.respond_to? :specification_version then
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<rubyzip>, [">= 0"])
49
+ s.add_runtime_dependency(%q<quick_magick>, [">= 0"])
50
+ s.add_development_dependency(%q<rspec>, ["~> 2.10.0"])
51
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
52
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
53
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
54
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
55
+ s.add_development_dependency(%q<pry>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<rubyzip>, [">= 0"])
58
+ s.add_dependency(%q<quick_magick>, [">= 0"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.10.0"])
60
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
61
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
62
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
63
+ s.add_dependency(%q<simplecov>, [">= 0"])
64
+ s.add_dependency(%q<pry>, [">= 0"])
65
+ end
66
+ else
67
+ s.add_dependency(%q<rubyzip>, [">= 0"])
68
+ s.add_dependency(%q<quick_magick>, [">= 0"])
69
+ s.add_dependency(%q<rspec>, ["~> 2.10.0"])
70
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
71
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
72
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
73
+ s.add_dependency(%q<simplecov>, [">= 0"])
74
+ s.add_dependency(%q<pry>, [">= 0"])
75
+ end
76
+ end
77
+
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doc-thumbnailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pedro Henrique Linhares
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rubyzip
16
+ requirement: &78868660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *78868660
25
+ - !ruby/object:Gem::Dependency
26
+ name: quick_magick
27
+ requirement: &78868330 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *78868330
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &78868050 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.10.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *78868050
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdoc
49
+ requirement: &78867700 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *78867700
58
+ - !ruby/object:Gem::Dependency
59
+ name: bundler
60
+ requirement: &78867320 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: 1.0.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *78867320
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: &78970570 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.8.4
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *78970570
80
+ - !ruby/object:Gem::Dependency
81
+ name: simplecov
82
+ requirement: &78968380 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *78968380
91
+ - !ruby/object:Gem::Dependency
92
+ name: pry
93
+ requirement: &78967560 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *78967560
102
+ description: Generate thumbnails for document files
103
+ email: pedrolmota@gmail.com
104
+ executables: []
105
+ extensions: []
106
+ extra_rdoc_files:
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ files:
110
+ - .document
111
+ - .rspec
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE.txt
115
+ - README.rdoc
116
+ - Rakefile
117
+ - VERSION
118
+ - lib/thumbnailer.rb
119
+ - spec/maker_spec.rb
120
+ - spec/resources/desired_odt.png
121
+ - spec/resources/desired_pdf.png
122
+ - spec/resources/file.odt
123
+ - spec/resources/file.pdf
124
+ - spec/spec_helper.rb
125
+ - spec/thumbnailer_spec.rb
126
+ - thumbnailer.gemspec
127
+ homepage: http://github.com/pedrolinhares/thumbnailer
128
+ licenses:
129
+ - MIT
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ segments:
141
+ - 0
142
+ hash: -707460863
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 1.8.17
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: Generate thumbnails for document files
155
+ test_files: []