Permalink
Browse files

Save to S3. Watermarking in the background.

  • Loading branch information...
1 parent 251c4cd commit eea24fa352f708fe8d2e8ad75e8325822dafbfd8 @wasabhi wasabhi committed Aug 6, 2012
Showing with 101 additions and 11 deletions.
  1. +4 −0 .env.example
  2. +2 −0 Gemfile
  3. +24 −0 Gemfile.lock
  4. +48 −0 lib/watermark.rb
  5. +23 −11 resque-example-app.rb
  6. BIN watermark.png
View
@@ -1,2 +1,6 @@
RACK_ENV=development
REDISTOGO_URL=redis://localhost:6379/
+AWS_S3_BUCKET_ORIGINALS={{ insert bucket name here }}
+AWS_S3_BUCKET_WATERMARKED={{ insert bucket name here }}
+AWS_ACCESS_KEY_ID=
+AWS_SECRET_ACCESS_KEY=
View
@@ -3,6 +3,8 @@ source :rubygems
gem 'rake'
gem 'sinatra'
gem 'resque'
+gem 'fog'
+gem 'mini_magick'
group :development, :test do
gem 'pry'
View
@@ -1,10 +1,30 @@
GEM
remote: http://rubygems.org/
specs:
+ builder (3.0.0)
coderay (1.0.7)
diff-lcs (1.1.3)
+ excon (0.15.5)
+ fog (1.5.0)
+ builder
+ excon (~> 0.14)
+ formatador (~> 0.2.0)
+ mime-types
+ multi_json (~> 1.0)
+ net-scp (~> 1.0.4)
+ net-ssh (>= 2.1.3)
+ nokogiri (~> 1.5.0)
+ ruby-hmac
+ formatador (0.2.3)
method_source (0.8)
+ mime-types (1.19)
+ mini_magick (3.4)
+ subexec (~> 0.2.1)
multi_json (1.3.6)
+ net-scp (1.0.4)
+ net-ssh (>= 1.99.1)
+ net-ssh (2.5.2)
+ nokogiri (1.5.5)
pry (0.9.10)
coderay (~> 1.0.5)
method_source (~> 0.8)
@@ -31,11 +51,13 @@ GEM
rspec-expectations (2.11.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.1)
+ ruby-hmac (0.4.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
slop (3.3.2)
+ subexec (0.2.2)
tilt (1.3.3)
vegas (0.1.11)
rack (>= 1.0.0)
@@ -44,6 +66,8 @@ PLATFORMS
ruby
DEPENDENCIES
+ fog
+ mini_magick
pry
rack-test
rake
View
@@ -0,0 +1,48 @@
+class Watermark
+ attr_reader :originals_directory, :watermarked_directory, :connection, :original_file
+ @queue = :watermark
+
+ def initialize(file_token)
+ @connection = Fog::Storage.new({
+ :provider => 'AWS',
+ :aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
+ :aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
+ })
+
+ @originals_directory = connection.directories.get(ENV['AWS_S3_BUCKET_ORIGINALS'])
+ @watermarked_directory = connection.directories.get(ENV['AWS_S3_BUCKET_WATERMARKED'])
+
+ @original_file = @originals_directory.files.get(file_token["key"])
+ end
+
+ def self.perform(file_token)
+ (new file_token).apply_watermark
+ end
+
+ def apply_watermark
+ Dir.mktmpdir do |tmpdir|
+ tmpfile = File.join(tmpdir, @original_file.key)
+
+ File.open(tmpfile, 'w') { |f| f.write(@original_file.body) }
+ image = MiniMagick::Image.open(tmpfile)
+
+ result = image.composite(MiniMagick::Image.open("watermark.png", "jpg")) do |c|
+ c.dissolve "15"
+ c.gravity "center"
+ end
+
+ watermarked_local_file = "#{tmpdir}/watermarked_#{@original_file.key}"
+ result.write(watermarked_local_file)
+
+ save_watermarked_file(watermarked_local_file)
+ end
+ end
+
+ def save_watermarked_file(watermarked_local_file)
+ watermarked_file_token = @watermarked_directory.files.create(
+ :key => @original_file.key,
+ :body => File.open(watermarked_local_file),
+ :public => true
+ )
+ end
+end
View
@@ -1,20 +1,32 @@
require 'bundler/setup'
Bundler.require(:default)
-
-module PopString
- @queue = :puts
-
- def self.perform(str)
- puts "Received #{str}"
- end
-end
+require './lib/watermark'
configure do
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
-get '/pop/:str' do
- Resque.enqueue(PopString, params['str'])
- puts "Popping #{params['str']} onto the queue..."
+post '/upload' do
+ unless params['file'][:tempfile].nil?
+ tmpfile = params['file'][:tempfile]
+ name = params['file'][:filename]
+ file_token = send_to_s3(tmpfile, name)
+ Resque.enqueue(Watermark, file_token)
+ end
+end
+
+def send_to_s3(tmpfile, name)
+ connection = Fog::Storage.new({
+ :provider => 'AWS',
+ :aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
+ :aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
+ })
+
+ directory = connection.directories.get(ENV['AWS_S3_BUCKET_ORIGINALS'])
+ file_token = directory.files.create(
+ :key => name,
+ :body => File.open(tmpfile),
+ :public => true
+ )
end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eea24fa

Please sign in to comment.