Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allow use of remote storage #8

Merged
merged 2 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 14 additions & 18 deletions exiffield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,18 @@ def get_exif(file_: FieldFile) -> str:

if not file_._committed:
# pipe file content to exiftool
file_._file.seek(0)

process = subprocess.run(
[exiftool_path, '-j', '-l', '-'],
check=True,
input=file_._file.read(),
stdout=subprocess.PIPE,
)
return process.stdout
fo = file_._file
fo.seek(0)
else:
# pass physical file to exiftool
file_path = file_.path
encoded_json = subprocess.check_output(
[exiftool_path, '-j', '-l', file_path],
)
return encoded_json.decode('utf8')
fo = file_.open()

process = subprocess.run(
[exiftool_path, '-j', '-l', '-'],
check=True,
input=fo.read(),
stdout=subprocess.PIPE,
)
return process.stdout


class ExifField(JSONField):
Expand Down Expand Up @@ -219,7 +215,7 @@ def update_exif(
# check whether extraction of the exif is required
exif_data = getattr(instance, self.name, None) or {}
has_exif = bool(exif_data)
filename = Path(file_.path).name
filename = Path(file_.name).name
exif_for_filename = exif_data.get('FileName', {}).get('val', '')
file_changed = exif_for_filename != filename or not file_._committed

Expand All @@ -230,7 +226,7 @@ def update_exif(
try:
exif_json = get_exif(file_)
except Exception:
logger.exception('Could not read metainformation from file: %s', file_.path)
logger.exception('Could not read metainformation from file: %s', file_.name)
return

try:
Expand All @@ -244,7 +240,7 @@ def update_exif(
# the storage.
# In the worst case the exif is extracted twice...
exif_data['FileName'] = {
'desc': 'Filename',
'desc': 'File Name',
'val': filename,
}
setattr(instance, self.name, exif_data)
Expand Down
45 changes: 45 additions & 0 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.core.files.uploadedfile import SimpleUploadedFile

from exiffield import fields
Expand Down Expand Up @@ -70,6 +71,39 @@ def img(request, uncommitted_img):
return img


@pytest.fixture
def remotestorage(mocker):
"""
Return a patched FileSystemStorage that does not support path()
"""
storage = FileSystemStorage()

def remote_open(name, mode):
media_image_path = Path(settings.MEDIA_ROOT) / IMAGE_NAME
return open(media_image_path, mode)

def remote_path():
raise NotImplementedError("Remote storage does not implement path()")

mocker.patch.object(storage, 'path', remote_path)
mocker.patch.object(storage, 'open', remote_open)
yield storage


@pytest.fixture
def img_remotestorage(remotestorage, img):
"""
Return a committed and uncommitted image instance using remote storage
"""
temp_storage = img.image.storage

img.image.storage = remotestorage

yield img

img.image.storage = temp_storage


@pytest.mark.django_db
def test_unsupported_file():
image_path = DIR / IMAGE_NAME
Expand Down Expand Up @@ -166,6 +200,17 @@ def test_do_not_extract_exif_without_file(mocker):
assert img.exif == {}


@pytest.mark.django_db
def test_extract_remote_backend(mocker, img_remotestorage):
img = img_remotestorage

exif_field = img._meta.get_field('exif')
mocker.spy(fields, 'get_exif')

exif_field.update_exif(img)
assert fields.get_exif.call_count == 1


@pytest.mark.django_db
def test_extract_exif_if_missing(mocker, img):
img.save() # store image and extract exif
Expand Down