Browse files

Add a PreviewStore for the preview image.

Enables us to store it on S3.
  • Loading branch information...
1 parent 03e952d commit c877042fbd911a90fb81106d88e94b303004ac95 @jonmagic jonmagic committed Oct 21, 2011
Showing with 27 additions and 4 deletions.
  1. +1 −0 lib/document.rb
  2. +1 −0 lib/pdf_archive.rb
  3. +3 −1 lib/pdf_uploader.rb
  4. +15 −0 lib/preview_store.rb
  5. +1 −1 lib/process_pdf.rb
  6. +2 −1 spec/pdf_uploader_spec.rb
  7. +4 −1 spec/process_pdf_spec.rb
View
1 lib/document.rb
@@ -4,5 +4,6 @@ class Document
key :page_contents, Array
mount_uploader :pdf, PdfUploader
+ mount_uploader :preview, PreviewStore
searches :pdf_filename, :page_contents
end
View
1 lib/pdf_archive.rb
@@ -48,6 +48,7 @@ def self.root
# require pdf uploader, document model, and process pdf job
require 'pdf_uploader'
+require 'preview_store'
require 'document'
require 'process_pdf'
View
4 lib/pdf_uploader.rb
@@ -23,6 +23,8 @@ def grim
def create_preview
cache_stored_file! unless cached?
- grim[0].save(File.join(store_dir, 'preview.jpg'))
+ output_path = File.join(cache_dir, 'preview.jpg')
+ grim[0].save(output_path)
+ return output_path
end
end
View
15 lib/preview_store.rb
@@ -0,0 +1,15 @@
+class PreviewStore < CarrierWave::Uploader::Base
+ def cache_dir
+ "#{PdfArchive.root}/tmp/cache/#{model.id}"
+ end
+
+ def store_dir
+ if PdfArchive.environment == 'test'
+ "#{PdfArchive.root}/tmp/documents/#{model.id}"
+ elsif PdfArchive.environment == 'production'
+ "documents/#{model.id}"
+ else
+ "#{PdfArchive.root}/public/documents/#{model.id}"
+ end
+ end
+end
View
2 lib/process_pdf.rb
@@ -4,7 +4,7 @@ def self.perform(document_id)
pdf = document.pdf
if pdf.grim.count > 0
- pdf.create_preview
+ document.preview = File.open(pdf.create_preview)
pdf.grim.each do |page|
document.page_contents << page.text
View
3 spec/pdf_uploader_spec.rb
@@ -23,7 +23,8 @@
describe "#create_preview" do
it "creates a preview image" do
- document.pdf.create_preview
+ document.preview = File.open(document.pdf.create_preview)
+ document.save!
File.exists?(File.join(tmp_dir, 'documents', document.id, 'preview.jpg')).should be_true
end
end
View
5 spec/process_pdf_spec.rb
@@ -5,7 +5,10 @@
describe ".perform" do
it "calls create_preview on pdf" do
- PdfUploader.any_instance.should_receive(:create_preview)
+ document.pdf.cache_stored_file!
+ output_path = File.join(document.pdf.cache_dir, 'preview.jpg')
+ document.pdf.grim[0].save(output_path)
+ PdfUploader.any_instance.should_receive(:create_preview).and_return(output_path)
ProcessPdf.perform(document.id)
end

0 comments on commit c877042

Please sign in to comment.