From a45eb8c4d22a8b57bc50107cea187bdd36715eb5 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Tue, 4 Sep 2018 14:04:44 +0100
Subject: [PATCH] Add a url method to the storage class to handle custom
 domains with auth

---
 opentech/storage_backends.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/opentech/storage_backends.py b/opentech/storage_backends.py
index ead023566..2acf70c95 100644
--- a/opentech/storage_backends.py
+++ b/opentech/storage_backends.py
@@ -1,4 +1,7 @@
+from urllib import parse
+
 from django.conf import settings
+from django.utils.encoding import filepath_to_uri
 from storages.backends.s3boto3 import S3Boto3Storage
 
 
@@ -25,3 +28,15 @@ class PrivateMediaStorage(S3Boto3Storage):
     file_overwrite = False
     querystring_auth = True
     url_protocol = 'https:'
+
+    def url(self, name, parameters=None, expire=None):
+        url = super().url(name, parameters, expire)
+
+        if hasattr(settings, 'AWS_PRIVATE_CUSTOM_DOMAIN'):
+            # Django storage doesn't handle custom domains with auth strings
+            custom_domain = settings.AWS_PRIVATE_CUSTOM_DOMAIN
+            parts = list(url.split(url))
+            parts[0:3] = self.url_protocol, custom_domain, filepath_to_uri(name)
+            return parse.urlunsplit(parts)
+
+        return url
-- 
GitLab