FIX: uploading an image as a site setting

FIX: uploading an image as a site setting

When uploading an image as a site setting, we need to return the “raw” URL, otherwise when saving the site setting, the upload won’t be looked up properly.

Follow-up-to: f11363d446

diff --git a/app/serializers/upload_serializer.rb b/app/serializers/upload_serializer.rb
index 118bb9c..43b8952 100644
--- a/app/serializers/upload_serializer.rb
+++ b/app/serializers/upload_serializer.rb
@@ -16,7 +16,6 @@ class UploadSerializer < ApplicationSerializer
              :human_filesize
 
   def url
-    return Discourse.store.cdn_url(object.url) if !object.secure || !SiteSetting.secure_media?
-    UrlHelper.cook_url(object.url, secure: object.secure)
+    object.for_site_setting ? object.url : UrlHelper.cook_url(object.url, secure: SiteSetting.secure_media? && object.secure)
   end
 end
diff --git a/spec/requests/uploads_controller_spec.rb b/spec/requests/uploads_controller_spec.rb
index fdd6714..69687b6 100644
--- a/spec/requests/uploads_controller_spec.rb
+++ b/spec/requests/uploads_controller_spec.rb
@@ -42,6 +42,13 @@ describe UploadsController do
         expect(Jobs::CreateAvatarThumbnails.jobs.size).to eq(1)
       end
 
+      it 'returns "raw" url for site settings' do
+        set_cdn_url "https://awesome.com"
+        post "/uploads.json", params: { file: logo, type: "site_setting", for_site_setting: "true" }
+        expect(response.status).to eq 200
+        expect(response.parsed_body["url"]).to start_with("/uploads/default/")
+      end
+
       it 'returns cdn url' do
         set_cdn_url "https://awesome.com"
         post "/uploads.json", params: { file: logo, type: "composer" }
diff --git a/spec/serializers/upload_serializer_spec.rb b/spec/serializers/upload_serializer_spec.rb
index b0f5e64..785f4f2 100644
--- a/spec/serializers/upload_serializer_spec.rb
+++ b/spec/serializers/upload_serializer_spec.rb
@@ -22,8 +22,8 @@ RSpec.describe UploadSerializer do
 
     context "when secure media is disabled" do
       it "just returns the normal URL, otherwise S3 errors are encountered" do
-        json_data = JSON.load(subject.to_json)
-        expect(json_data['url']).to eq(upload.url)
+        UrlHelper.expects(:cook_url).with(upload.url, secure: false)
+        subject.to_json
       end
     end
 

GitHub sha: f43c0a5d

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/changing-logo-image-fails/156564/4