FIX: return cdn url for uploads if available.

FIX: return cdn url for uploads if available.

Currently it is displaying non-cdn urls in the composer preview.

diff --git a/app/serializers/upload_serializer.rb b/app/serializers/upload_serializer.rb
index a04300b..118bb9c 100644
--- a/app/serializers/upload_serializer.rb
+++ b/app/serializers/upload_serializer.rb
@@ -16,7 +16,7 @@ class UploadSerializer < ApplicationSerializer
              :human_filesize
 
   def url
-    return object.url if !object.secure || !SiteSetting.secure_media?
+    return Discourse.store.cdn_url(object.url) if !object.secure || !SiteSetting.secure_media?
     UrlHelper.cook_url(object.url, secure: object.secure)
   end
 end
diff --git a/spec/requests/uploads_controller_spec.rb b/spec/requests/uploads_controller_spec.rb
index 0be31c1..fdd6714 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 cdn url' do
+        set_cdn_url "https://awesome.com"
+        post "/uploads.json", params: { file: logo, type: "composer" }
+        expect(response.status).to eq 200
+        expect(response.parsed_body["url"]).to start_with("https://awesome.com/uploads/default/")
+      end
+
       it 'is successful with an attachment' do
         SiteSetting.authorized_extensions = "*"
 

GitHub sha: f11363d4

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

https://meta.discourse.org/t/s3-cdn-url-ignored-when-uploading-into-posts/54898/14

Why not use UrlHelper.cook_url(object.url, secure: SiteSetting.secure_media? && object.secure) here?

Not sure. I will check the possibility :+1:

1 Like