From eeda367e703b23ae63e23ac08275bc3c2f958f6c Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 26 May 2015 12:32:52 +1000 Subject: [PATCH] FIX: should be able to serve optimized image from local if its ... local... --- app/controllers/user_avatars_controller.rb | 2 +- app/models/optimized_image.rb | 4 ++++ spec/models/optimized_image_spec.rb | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rb index b5e5f953aa5..1ecdb2be3a8 100644 --- a/app/controllers/user_avatars_controller.rb +++ b/app/controllers/user_avatars_controller.rb @@ -67,7 +67,7 @@ class UserAvatarsController < ApplicationController original = Discourse.store.path_for(upload) if Discourse.store.external? || File.exists?(original) if optimized = get_optimized_image(upload, size) - if Discourse.store.external? + unless optimized.local? expires_in 1.day, public: true return redirect_to optimized.url end diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb index b2a758e76ed..227b8ffcfd5 100644 --- a/app/models/optimized_image.rb +++ b/app/models/optimized_image.rb @@ -83,6 +83,10 @@ class OptimizedImage < ActiveRecord::Base end end + def local? + !(url =~ /^(https?:)?\/\//) + end + def self.resize_instructions(from, to, dimensions, opts={}) # NOTE: ORDER is important! %W{ diff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rb index 9df31c7e3bd..b3a8da52d51 100644 --- a/spec/models/optimized_image_spec.rb +++ b/spec/models/optimized_image_spec.rb @@ -5,6 +5,21 @@ describe OptimizedImage do let(:upload) { build(:upload) } before { upload.id = 42 } + describe ".local?" do + + def local(url) + OptimizedImage.new(url: url).local? + end + + it "correctly detects local vs remote" do + expect(local("//hello")).to eq(false) + expect(local("http://hello")).to eq(false) + expect(local("https://hello")).to eq(false) + expect(local("https://hello")).to eq(false) + expect(local("/hello")).to eq(true) + end + end + describe ".create_for" do context "when using an internal store" do