mounted_on should also be used for redirections + refactoring

did committed May 15, 2015
commit c8a9d8be3c1e7a1d03e290d3c722f4e1a5739cf7
Showing 4 changed files with 13 additions and 8 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb +2 -0
@@ @@ -18,6 +18,8 @@ module Locomotive
CONTENT_ENTRY_ENGINE_CLASS_NAME = /^Locomotive::ContentEntry(.*)$/o
+ IsHTTP = /^https?:\/\//o
+
class << self
attr_writer :configuration
end
locomotive/steam/middlewares/helpers.rb b/lib/locomotive/steam/middlewares/helpers.rb +9 -2
@@ @@ -18,8 +18,11 @@ module Locomotive::Steam
end
def redirect_to(location, type = 301)
- self.log "Redirected to #{location}".blue
- @next_response = [type, { 'Content-Type' => 'text/html', 'Location' => location }, []]
+ _location = mounted_on && (location =~ Steam::IsHTTP).nil? ? "#{mounted_on}#{location}" : location
+
+ self.log "Redirected to #{_location}".blue
+
+ @next_response = [type, { 'Content-Type' => 'text/html', 'Location' => _location }, []]
end
def modify_path(path = nil, &block)
@@ @@ -34,6 +37,10 @@ module Locomotive::Steam
path
end
+ def mounted_on
+ request.env['steam.mounted_on']
+ end
+
def log(msg, offset = 2)
Locomotive::Common::Logger.info (' ' * offset) + msg
end
locomotive/steam/services/asset_host_service.rb b/lib/locomotive/steam/services/asset_host_service.rb +1 -3
@@ @@ -3,8 +3,6 @@ module Locomotive
class AssetHostService
- IsHTTP = /^https?:\/\//o
-
attr_reader :request, :site, :host
def initialize(request, site, host)
@@ @@ -16,7 +14,7 @@ module Locomotive
def compute(source, timestamp = nil)
return source if source.nil?
- return add_timestamp_suffix(source, timestamp) if source =~ IsHTTP
+ return add_timestamp_suffix(source, timestamp) if source =~ Steam::IsHTTP
url = self.host ? URI.join(host, source).to_s : source
locomotive/steam/services/image_resizer_service.rb b/lib/locomotive/steam/services/image_resizer_service.rb +1 -3
@@ @@ -3,8 +3,6 @@ module Locomotive
class ImageResizerService < Struct.new(:resizer, :asset_path)
- IsHTTP = /^https?:\/\//o
-
def resize(source, geometry)
return nil if disabled? || geometry.blank?
@@ @@ -25,7 +23,7 @@ module Locomotive
def fetch_file(source)
url_or_path = get_url_or_path(source)
- if url_or_path =~ IsHTTP
+ if url_or_path =~ Steam::IsHTTP
resizer.fetch_url(url_or_path)
else
path = url_or_path.sub(/(\?.*)$/, '')