Permalink
Browse files

Namespace everything under Imagery module and lib/imagery

  • Loading branch information...
1 parent 013c543 commit bddd1395892bdfad940832eada8b8d5e75709a67 @josh josh committed with Dec 17, 2009
Showing with 483 additions and 471 deletions.
  1. +11 −9 config.ru
  2. +0 −33 image_server.rb
  3. +0 −68 lib/image.rb
  4. +0 −47 lib/image_variant_generator.rb
  5. +12 −0 lib/imagery.rb
  6. +69 −0 lib/imagery/image.rb
  7. +47 −0 lib/imagery/image_variant_generator.rb
  8. 0 lib/{ → imagery}/logger_ext.rb
  9. +41 −0 lib/imagery/middleware/accel_redirect.rb
  10. +21 −0 lib/imagery/middleware/cache_purge.rb
  11. +17 −0 lib/imagery/middleware/favicon_filter.rb
  12. +32 −0 lib/imagery/middleware/logged_request.rb
  13. +24 −0 lib/imagery/middleware/remote_proxy.rb
  14. +13 −0 lib/imagery/middleware/server_name.rb
  15. +32 −0 lib/imagery/send_file.rb
  16. +22 −0 lib/imagery/server.rb
  17. +55 −0 lib/imagery/svg_generator.rb
  18. +23 −0 lib/imagery/transformations.rb
  19. +35 −0 lib/imagery/transformations/borders.rb
  20. +8 −8 lib/{ → imagery}/transformations/sizes.rb
  21. +8 −9 lib/{ → imagery}/transformations/transform.rb
  22. 0 lib/{ → imagery}/vendor/SyslogLogger-1.4.0/History.txt
  23. 0 lib/{ → imagery}/vendor/SyslogLogger-1.4.0/Manifest.txt
  24. 0 lib/{ → imagery}/vendor/SyslogLogger-1.4.0/README.txt
  25. 0 lib/{ → imagery}/vendor/SyslogLogger-1.4.0/Rakefile
  26. 0 lib/{ → imagery}/vendor/SyslogLogger-1.4.0/lib/syslog_logger.rb
  27. 0 lib/{ → imagery}/vendor/SyslogLogger-1.4.0/test/test_syslog_logger.rb
  28. +0 −40 lib/middleware/accel_redirect.rb
  29. +0 −20 lib/middleware/cache_purge.rb
  30. +0 −16 lib/middleware/favicon_filter.rb
  31. +0 −31 lib/middleware/logged_request.rb
  32. +0 −23 lib/middleware/remote_proxy.rb
  33. +0 −12 lib/middleware/server_name.rb
  34. +0 −31 lib/send_file.rb
  35. +0 −56 lib/svg_generator.rb
  36. +0 −21 lib/transformations.rb
  37. +0 −35 lib/transformations/borders.rb
  38. +2 −1 test/helper.rb
  39. +6 −6 test/test_image_variant_generator.rb
  40. +1 −1 test/test_remote_proxy.rb
  41. +4 −4 test/test_svg_generation.rb
View
@@ -3,7 +3,9 @@
require 'rubygems'
require 'rack/cache'
require 'rack/contrib'
-require 'image_server'
+
+$: << File.join(File.dirname(__FILE__), 'lib')
+require 'imagery'
require 'config/env'
@@ -20,22 +22,22 @@ end
if ENV['NGINX_ACCEL_REDIRECTS']
STDERR.puts 'Using accel redirect (Shopify config).'
- require 'lib/middleware/accel_redirect'
- use AccelRedirect
+ require 'imagery/middleware/accel_redirect'
+ use Imagery::AccelRedirect
else
use Rack::Sendfile
end
use Rack::ShowExceptions
# 1. Forget about stupid favicons
-use FaviconFilter
+use Imagery::FaviconFilter
# 2. Log all other incoming requests
-use LoggedRequest
+use Imagery::LoggedRequest
# 3. Override server name into something non embarrasing
-use ServerName
+use Imagery::ServerName
# 4. Content type needs to be present, default to attachment
use Rack::ContentType, "application/octet-stream"
@@ -46,10 +48,10 @@ use Rack::Cache,
:entitystore => ENV['ENTITY_STORE']
# 6. handle PURGE requests
-use CachePurge
+use Imagery::CachePurge
# 7. See if files already exist on remote host, if so handle them directly
-use RemoteProxy
+use Imagery::RemoteProxy
# 8. Otherwise run the image server and produce the missing images
-run ImageServer.new
+run Imagery::Server.new
View
@@ -1,33 +0,0 @@
-require 'lib/send_file'
-require 'lib/middleware/cache_purge'
-require 'lib/middleware/logged_request'
-require 'lib/middleware/remote_proxy'
-require 'lib/middleware/server_name'
-require 'lib/middleware/favicon_filter'
-require 'lib/logger_ext'
-require 'lib/transformations'
-require 'lib/svg_generator'
-require 'lib/image_variant_generator'
-require 'lib/image'
-
-class ImageServer
- include SendFile
-
- NotFound = [404, {'Content-Type' => 'text/html'}, ['<h1>File not Found</h1>']].freeze
-
- def call(env)
- Logger.current.info 'Attempting to generate missing file...'
-
- [SvgGenerator, ImageVariantGenerator].each do |generator|
- if image = generator.from_url(env['imagery.origin_host'], env['PATH_INFO'] + (env['QUERY_STRING'].empty? ? '' : "?#{env['QUERY_STRING']}"))
-
- return send_file(image)
- end
- end
-
- Logger.current.info 'No generator available'
-
- NotFound
- end
-
-end
View
@@ -1,68 +0,0 @@
-require 'RMagick'
-require 'fileutils'
-require 'patron'
-
-class Image
- attr_accessor :content
- attr_reader :headers
- attr_reader :status
- attr_reader :server
-
- def initialize(server, path)
- @server = server
- @path = path
- download(path)
- end
-
- def found?
- @status == 200
- end
-
- def basename
- File.basename(@path)
- end
-
- def basename_no_ext
- File.basename(@path, ext)
- end
-
- def ext
- File.extname(@path)
- end
-
- def dirname
- File.dirname(@path)
- end
-
- private
-
- def session
- @@session ||= begin
- sess = Patron::Session.new
- sess.timeout = 10
- sess.headers['User-Agent'] = 'imagery/1.0'
- sess
- end
- end
-
- def download(path_info)
- session.base_url = "http://#{server}"
-
- response = Logger.current.info_with_time "Loading http://#{server}#{path_info}" do
- session.get(path_info)
- end
-
- @path = path_info.split('?')[0]
- @headers = response.headers
- @status = response.status
-
- if found?
- self.content = response.body
- true
- else
- Logger.current.error "Not found"
- false
- end
- end
-end
-
@@ -1,47 +0,0 @@
-require 'RMagick'
-require 'fileutils'
-
-class ImageVariantGenerator
- VARIANT_DELIMITER = '_'
- SupportedImageTypes = ['.gif', '.jpg', '.jpeg', '.png', '.bmp']
-
- attr_accessor :content
- attr_accessor :content_type
-
- def self.variant_parser
- @variant_parser ||= /(.*)\_(#{Transformations.list.join('|')})(#{SupportedImageTypes.join('|')})/i
- end
-
- def self.from_url(server, path)
- return nil unless path =~ variant_parser
-
- remote_path = "#{$1}#{$3}"
-
- file = Image.new(server, remote_path)
- if file.found?
- transform_content(file, $2)
- file
- else
- nil
- end
- end
-
- def initialize(image)
- @image = image
- end
-
- def self.transform_content(image, variant)
- img = Magick::Image.from_blob(image.content).first
- transformation = Transformations[variant]
-
- Logger.current.info_with_time "Transforming image to #{variant}" do
- raise ArgumentError, "#{variant} is not a known transformation. (#{Transformations.list.join(', ')})" if transformation.nil?
- img = transformation.call(img)
- raise ArgumentError, "Creating variant #{variant} for #{path} produced an error. Please return a Magick::Image" if img.nil?
- image.content = img.to_blob
- end
- true
- end
-
-end
-
View
@@ -0,0 +1,12 @@
+require 'imagery/send_file'
+require 'imagery/middleware/cache_purge'
+require 'imagery/middleware/logged_request'
+require 'imagery/middleware/remote_proxy'
+require 'imagery/middleware/server_name'
+require 'imagery/middleware/favicon_filter'
+require 'imagery/logger_ext'
+require 'imagery/transformations'
+require 'imagery/svg_generator'
+require 'imagery/image_variant_generator'
+require 'imagery/image'
+require 'imagery/server'
View
@@ -0,0 +1,69 @@
+require 'RMagick'
+require 'fileutils'
+require 'patron'
+
+module Imagery
+ class Image
+ attr_accessor :content
+ attr_reader :headers
+ attr_reader :status
+ attr_reader :server
+
+ def initialize(server, path)
+ @server = server
+ @path = path
+ download(path)
+ end
+
+ def found?
+ @status == 200
+ end
+
+ def basename
+ File.basename(@path)
+ end
+
+ def basename_no_ext
+ File.basename(@path, ext)
+ end
+
+ def ext
+ File.extname(@path)
+ end
+
+ def dirname
+ File.dirname(@path)
+ end
+
+ private
+
+ def session
+ @@session ||= begin
+ sess = Patron::Session.new
+ sess.timeout = 10
+ sess.headers['User-Agent'] = 'imagery/1.0'
+ sess
+ end
+ end
+
+ def download(path_info)
+ session.base_url = "http://#{server}"
+
+ response = Logger.current.info_with_time "Loading http://#{server}#{path_info}" do
+ session.get(path_info)
+ end
+
+ @path = path_info.split('?')[0]
+ @headers = response.headers
+ @status = response.status
+
+ if found?
+ self.content = response.body
+ true
+ else
+ Logger.current.error "Not found"
+ false
+ end
+ end
+ end
+end
@@ -0,0 +1,47 @@
+require 'RMagick'
+require 'fileutils'
+
+module Imagery
+ class ImageVariantGenerator
+ VARIANT_DELIMITER = '_'
+ SupportedImageTypes = ['.gif', '.jpg', '.jpeg', '.png', '.bmp']
+
+ attr_accessor :content
+ attr_accessor :content_type
+
+ def self.variant_parser
+ @variant_parser ||= /(.*)\_(#{Transformations.list.join('|')})(#{SupportedImageTypes.join('|')})/i
+ end
+
+ def self.from_url(server, path)
+ return nil unless path =~ variant_parser
+
+ remote_path = "#{$1}#{$3}"
+
+ file = Image.new(server, remote_path)
+ if file.found?
+ transform_content(file, $2)
+ file
+ else
+ nil
+ end
+ end
+
+ def initialize(image)
+ @image = image
+ end
+
+ def self.transform_content(image, variant)
+ img = Magick::Image.from_blob(image.content).first
+ transformation = Transformations[variant]
+
+ Logger.current.info_with_time "Transforming image to #{variant}" do
+ raise ArgumentError, "#{variant} is not a known transformation. (#{Transformations.list.join(', ')})" if transformation.nil?
+ img = transformation.call(img)
+ raise ArgumentError, "Creating variant #{variant} for #{path} produced an error. Please return a Magick::Image" if img.nil?
+ image.content = img.to_blob
+ end
+ true
+ end
+ end
+end
File renamed without changes.
@@ -0,0 +1,41 @@
+require 'rack/file'
+
+class File #:nodoc:
+ alias :to_path :path
+end
+
+# To make this work you have to add:
+#
+# location /cache/ {
+ # internal;
+ # alias /mnt/data/cache/rack/body;
+# }
+#
+# to nginx config
+
+
+module Imagery
+ class AccelRedirect
+ F = ::File
+
+ def initialize(app, variation=nil)
+ @app = app
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ if body.respond_to?(:to_path)
+
+ path = body.to_path
+ url = path.sub(/^#{ENV['CACHE_LOCATION']}/i, '/cache')
+
+ Logger.current.info " => sending #{url} through nginx"
+
+ headers['Content-Length'] = '0'
+ headers['X-Accel-Redirect'] = url
+ body = []
+ end
+ [status, headers, body]
+ end
+ end
+end
@@ -0,0 +1,21 @@
+module Imagery
+ class CachePurge
+ Success = [200, {'Content-Type' => 'text/plain'}, ['OK']]
+
+ def initialize(app)
+ @app = app
+ end
+
+ # PURGE /s/files/1/0001/4168/files/thumbs/pic_thumb.jpg?12428536032 HTTP/1.0
+
+ def call(env)
+ # Rack cache automatically invalidates resource if the verbs are not GET/POST so
+ # we don't actually have to do anything. Simply don't delegate those to the backend
+ if env['REQUEST_METHOD'] == 'PURGE'
+ Success
+ else
+ @app.call(env)
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit bddd139

Please sign in to comment.