Skip to content

Instantly share code, notes, and snippets.

@xenda
Created June 7, 2012 20:39
Show Gist options
  • Save xenda/2891415 to your computer and use it in GitHub Desktop.
Save xenda/2891415 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'aws/s3'
module S3Config
bucket = 'assets.baymachine.com'
s3_key = '1B7JJ1RZXMZP7VQADY02'
s3_secret = '8UvZq1RtsyE72t0vq2U1FaaZStGXm9fj87uFub2b'
end
post "/convert_to" do
pdf_file_path = process_file(params[:tmp])
swf_file_path = convert_to_swf(pdf_file_path)
upload_to_amazons3(swf_file_path)
end
private
def process_file(file)
path = "temp#{Time.now.to_s}"
File.open(path) do |f|
f.write file
end
path
end
def convert_to_swf(pdf_file_path)
bin_file = "pdf2swf"
swf_file_path = pdf_file_path.gsub("pdf","swf")
result = `#{bin_file} #{pdf_file_path} -o #{swf_file_path} -G -z -j 10`
swf_file_path
end
def upload_to_amazons3(swf_file_path)
init_s3_connection
send_file(swf_file_path)
end
def init_s3_connection
params = { :access_key_id => S3Config::s3_key,
:secret_access_key => S3Config::s3_secret}
AWS::S3::Base.establish_connection!(params)
end
def send_file(file_path,bucket)
file_name = file_path.split("/").last
s3_path = "converted_swfs/#{file_name}"
file = AWS::S3::S3Object.store(s3_path, open(file_path), S3Config::bucket, :access => :public_read)
puts file.inspect
"http://#{S3Config::bucket}/#{s3_path}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment