Skip to content
Browse files

Add more processors

- docsplit_image for extracting images from file
- docsplit_chaining for chaining the result from previous styles
i
  • Loading branch information...
1 parent 859cc80 commit aa728ce3250da4a8bc77818a815f86763f88e5df @tienle committed Mar 14, 2012
Showing with 35 additions and 5 deletions.
  1. +35 −5 lib/docsplit-paperclip-processor.rb
View
40 lib/docsplit-paperclip-processor.rb
@@ -1,7 +1,6 @@
require "paperclip"
module Paperclip
- class DocsplitPdf < Processor
-
+ class DocsplitProcessor < Processor
attr_accessor :src, :options
def initialize(file, options = {}, attachment = nil)
@@ -10,19 +9,50 @@ def initialize(file, options = {}, attachment = nil)
@options = options
@basename = File.basename(@file.path, '.*')
end
+ end
+
+ class DocsplitChaining < Processor
+ attr_accessor :options, :attachment
+
+ def initialize(file, options = {}, attachment = nil)
+ @options = options
+ @attachment = attachment
+ end
+
+ def make
+ attachment.to_file(options[:from_style] || :original)
+ end
+ end
+ class DocsplitPdf < DocsplitProcessor
def make
begin
src_path = File.expand_path(@src.path)
dst_path = Dir.tmpdir
- escaped_src, escaped_dst = [src_path, dst_path].map &Docsplit::ESCAPE
- Docsplit.extract_pdf(escaped_src, :output => escaped_dst)
- rescue Cocaine::CommandLineError => e
+ Docsplit.extract_pdf(src_path, :output => dst_path)
+ rescue Exception => e
Rails.logger.error e.message
raise PaperclipError, "There was an error converting #{basename} to pdf"
end
File.open(File.join(dst_path, "#{@basename}.pdf"))
end
end
+
+ class DocsplitImage < DocsplitProcessor
+ def make
+ begin
+ src_path = File.expand_path(@src.path)
+ dst_path = Dir.tmpdir
+ pages = options[:pages] || [1]
+ options = @options.merge(:output => dst_path)
+
+ Docsplit.extract_images(src_path, options)
+ rescue Exception => e
+ Rails.logger.error e.message
+ raise PaperclipError, "There was an error extracting images from #{basename}"
+ end
+ File.open(File.join(dst_path, "#{@basename}_#{pages.first}.#{@options[:format]}"))
+ end
+ end
end

0 comments on commit aa728ce

Please sign in to comment.
Something went wrong with that request. Please try again.