Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
An action to calculate aspect ratios.
Browse files Browse the repository at this point in the history
  • Loading branch information
knowtheory committed Jan 24, 2016
1 parent 47d1418 commit c2c86b2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
53 changes: 53 additions & 0 deletions app/actions/calculate_aspect_ratios.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require File.join(File.dirname(__FILE__), 'support', 'document_action')

class CalculateAspectRatios < DocumentAction

def process
errors = []
document = Document.find(input)
@page_aspect_ratios = (1..document.page_count).map do |page_number|
path = document.page_image_path(page_number, 'small')
filename = File.basename(path)
begin
File.open(filename, 'wb'){ |file| file << asset_store.read(path) }
cmd = %(gm identify #{filename} | egrep -o "[[:digit:]]+x[[:digit:]]+")
width, height = `#{cmd}`.split("x").map(&:to_f)
width / height
rescue Exception => e
params = options.merge({page_number: page_number})
errors.push(params)
LifecycleMailer.exception_notification(e,params).deliver_now
0
end
end

save_page_aspect_ratios!
errors
end

def save_page_aspect_ratios!
ids = document.pages.order(:page_number).pluck(:id)

query_template = <<-QUERY
UPDATE pages
SET aspect_ratio = input.aspect_ratio
FROM (SELECT unnest(ARRAY[?]) as id, unnest(ARRAY[?]) as aspect_ratio) AS input
WHERE pages.id = input.id
QUERY
query = Page.send(:replace_bind_variables, query_template, [ids, @page_aspect_ratios])
Page.connection.execute(query)
end

end

__END__
operator = Account.lookup(email)
Document.find_each do |document|
job = document.processing_jobs.new(
action: 'calculate_aspect_ratios',
title: 'calculate_aspect_ratios',
options: {id: document.id},
account_id: operator.id
)
job.queue
end
1 change: 1 addition & 0 deletions config/cloud_crowd/express/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- api_document_import
- large_document_import
- reindex_everything
- calculate_aspect_ratios

# The following settings allow you to control the number of workers that can run
# on a given node, to prevent the node from becoming overloaded. 'max_workers'
Expand Down
1 change: 1 addition & 0 deletions config/cloud_crowd/production/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- backup_database
- api_document_import
- reindex_everything
- calculate_aspect_ratios

# The following settings allow you to control the number of workers that can run
# on a given node, to prevent the node from becoming overloaded. 'max_workers'
Expand Down

0 comments on commit c2c86b2

Please sign in to comment.