Skip to content

Commit

Permalink
Use absolute URL for images
Browse files Browse the repository at this point in the history
  • Loading branch information
KalobTaulien authored and gasman committed Mar 26, 2020
1 parent 1f63f0d commit 19e940d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
~~~~~~~~~~~~~~~~~~

* Fix error when exporting empty non-required ChooserBlocks (Kalob Taulinen, Jacob Topp-Mugglestone)
* Ensure that file URLs are always absolute by falling back on BASE_URL when MEDIA_URL is local (Kalob Taulinen)


0.2 (17.03.2020)
Expand Down
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'test-media')
MEDIA_URL = 'http://example.com/media/'
MEDIA_URL = 'http://media.example.com/media/'

SECRET_KEY = 'not needed'

Expand Down
27 changes: 24 additions & 3 deletions tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.core.files import File
from django.core.files.images import ImageFile
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.test import TestCase, override_settings
from wagtail.core.models import Page, Collection
from wagtail.images.models import Image
from wagtail.documents.models import Document
Expand Down Expand Up @@ -364,6 +364,27 @@ def test_image(self):
self.assertEqual(response.status_code, 200)
data = json.loads(response.content)

self.assertEqual(len(data['objects']), 1)
obj = data['objects'][0]
self.assertEqual(obj['fields']['file']['download_url'], 'http://media.example.com/media/original_images/wagtail.jpg')
self.assertEqual(obj['fields']['file']['size'], 1160)
self.assertEqual(obj['fields']['file']['hash'], '45c5db99aea04378498883b008ee07528f5ae416')

@override_settings(MEDIA_URL='/media/')
def test_image_with_local_media_url(self):
"""File URLs should use BASE_URL to form an absolute URL if MEDIA_URL is relative"""
with open(os.path.join(FIXTURES_DIR, 'wagtail.jpg'), 'rb') as f:
image = Image.objects.create(
title="Wagtail",
file=ImageFile(f, name='wagtail.jpg')
)

response = self.get({
'wagtailimages.image': [image.pk]
})
self.assertEqual(response.status_code, 200)
data = json.loads(response.content)

self.assertEqual(len(data['objects']), 1)
obj = data['objects'][0]
self.assertEqual(obj['fields']['file']['download_url'], 'http://example.com/media/original_images/wagtail.jpg')
Expand All @@ -385,7 +406,7 @@ def test_document(self):

self.assertEqual(len(data['objects']), 1)
obj = data['objects'][0]
self.assertEqual(obj['fields']['file']['download_url'], 'http://example.com/media/documents/document.txt')
self.assertEqual(obj['fields']['file']['download_url'], 'http://media.example.com/media/documents/document.txt')
self.assertEqual(obj['fields']['file']['size'], 33)
self.assertEqual(obj['fields']['file']['hash'], '9b90daf19b6e1e8a4852c64f9ea7fec5bcc5f7fb')

Expand All @@ -403,7 +424,7 @@ def test_custom_model_with_file_field(self):

self.assertEqual(len(data['objects']), 1)
obj = data['objects'][0]
self.assertEqual(obj['fields']['image']['download_url'], 'http://example.com/media/avatars/wagtail.jpg')
self.assertEqual(obj['fields']['image']['download_url'], 'http://media.example.com/media/avatars/wagtail.jpg')
self.assertEqual(obj['fields']['image']['size'], 1160)
self.assertEqual(obj['fields']['image']['hash'], '45c5db99aea04378498883b008ee07528f5ae416')

Expand Down
15 changes: 12 additions & 3 deletions wagtail_transfer/field_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import pathlib
from urllib.parse import urlparse

from django.conf import settings
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.db.models.fields.reverse_related import ManyToOneRel
from django.utils.encoding import is_protected_type
from taggit.managers import TaggableManager
from wagtail.core.fields import RichTextField, StreamField

from .files import get_file_size, get_file_hash, File
from .files import get_file_size, get_file_hash, File, FileTransferError
from .models import get_base_model
from .richtext import get_reference_handler
from .streamfield import get_object_references, update_object_ids
Expand Down Expand Up @@ -161,8 +162,13 @@ def update_object_references(self, value, destination_ids_by_source):

class FileAdapter(FieldAdapter):
def serialize(self, instance):
url = self.field.value_from_object(instance).url
if settings.MEDIA_URL.startswith('/'):
# Using a relative media url. ie. /media/
# Prepend the BASE_URL to turn this into an absolute URL
url = settings.BASE_URL.rstrip('/') + url
return {
'download_url': self.field.value_from_object(instance).url,
'download_url': url,
'size': get_file_size(self.field, instance),
'hash': get_file_hash(self.field, instance),
}
Expand All @@ -184,7 +190,10 @@ def populate_field(self, instance, value, context):
local_filename = self.field.generate_filename(instance, name)

_file = File(local_filename, value['size'], value['hash'], value['download_url'])
imported_file = _file.transfer()
try:
imported_file = _file.transfer()
except FileTransferError:
return None
context.imported_files_by_source_url[_file.source_url] = imported_file

value = imported_file.file.name
Expand Down
2 changes: 1 addition & 1 deletion wagtail_transfer/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def transfer(self):
response = requests.get(self.source_url)

if response.status_code != 200:
raise FileTransferError # TODO
raise FileTransferError("Non-200 response from image URL")

return ImportedFile.objects.create(
file=ContentFile(response.content, name=self.local_filename),
Expand Down

0 comments on commit 19e940d

Please sign in to comment.