From b1ca64487ab123245a79ab8d53c65bd099ede303 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Tue, 25 Jun 2019 01:19:58 +0530 Subject: [PATCH] FIX: multisite upload urls must have either db name or the word 'short-url'. --- app/models/post.rb | 4 +++- spec/multisite/post_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 spec/multisite/post_spec.rb diff --git a/app/models/post.rb b/app/models/post.rb index 84f2af64702..0fcf0f802f8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -886,8 +886,9 @@ class Post < ActiveRecord::Base end def each_upload_url(fragments: nil, include_local_upload: true) + current_db = RailsMultisite::ConnectionManagement.current_db upload_patterns = [ - /\/uploads\/#{RailsMultisite::ConnectionManagement.current_db}\//, + /\/uploads\/#{current_db}\//, /\/original\//, /\/optimized\//, /\/uploads\/short-url\/[a-zA-Z0-9]+(\.[a-z0-9]+)?/ @@ -898,6 +899,7 @@ class Post < ActiveRecord::Base links.each do |src| next if src.blank? || upload_patterns.none? { |pattern| src.split("?")[0] =~ pattern } + next if Rails.configuration.multisite && src.exclude?(current_db) && src.exclude?("short-url") src = "#{SiteSetting.force_https ? "https" : "http"}:#{src}" if src.start_with?("//") next unless Discourse.store.has_been_uploaded?(src) || (include_local_upload && src =~ /\A\/[^\/]/i) diff --git a/spec/multisite/post_spec.rb b/spec/multisite/post_spec.rb new file mode 100644 index 00000000000..ef1112de8b6 --- /dev/null +++ b/spec/multisite/post_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Multisite Post', type: :multisite do + describe '#each_upload_url' do + let(:upload1) { Fabricate(:upload_s3) } + let(:upload2) { Fabricate(:upload_s3) } + let(:upload3) { Fabricate(:upload_s3) } + + it "correctly identifies all upload urls" do + SiteSetting.enable_s3_uploads = true + SiteSetting.s3_upload_bucket = "s3-upload-bucket" + SiteSetting.s3_access_key_id = "some key" + SiteSetting.s3_secret_access_key = "some secret key" + SiteSetting.s3_cdn_url = "https://cdn.s3.amazonaws.com" + + upload3.url.sub!(RailsMultisite::ConnectionManagement.current_db, "secondsite") + upload3.save! + + urls = [] + post = Fabricate(:post, raw: "A post with image and link upload.\n\n![](#{upload1.short_path})\n\nLink to upload\n![](#{upload3.url})") + post.each_upload_url { |src, _, _| urls << src.sub("http:", "") } + expect(urls).to eq([upload1.short_path, upload2.url]) + end + end +end