Skip to content

Commit ff15abc

Browse files
committed
Support ZipExtFile - fixes kimetrica#2
Signed-off-by: Roger Hunwicks <roger@tonic-solutions.com>
1 parent d4602e3 commit ff15abc

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

binary_database_files/storage.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Custom storage backend that stores files in the database to facilitate scaling."""
33
from __future__ import unicode_literals, division
44
import os
5+
from io import UnsupportedOperation
56

67
import six
78

@@ -72,12 +73,13 @@ def _open(self, name, mode='rb'):
7273
def _save(self, name, content):
7374
"""Save file with filename `name` and given content to the database."""
7475
full_path = self.path(name)
76+
# ZipExtFile advertises seek() but can raise UnsupportedOperation
7577
try:
76-
size = content.size
77-
except AttributeError:
78-
size = os.path.getsize(full_path)
79-
content.seek(0)
78+
content.seek(0)
79+
except UnsupportedOperation:
80+
pass
8081
content = content.read()
82+
size = len(content)
8183
f = models.File.objects.create(
8284
content=content,
8385
size=size,
9.42 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a text file containing UTF-8 text: áéíóúüñÑ

binary_database_files/tests/tests.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# -*- coding: utf-8 -*-
22
import os
33
import shutil
4+
from io import BytesIO
5+
from zipfile import ZipFile
46

57
import six
68

9+
from django.conf import settings
710
from django.core import files
8-
from django.test import TestCase
11+
from django.core.files import File as DjangoFile
912
from django.core.files.storage import default_storage
1013
from django.core.management import call_command
11-
from django.conf import settings
14+
from django.test import TestCase
1215

1316
from binary_database_files.models import File
1417
from binary_database_files.tests.models import Thing
@@ -96,6 +99,19 @@ def test_adding_file(self):
9699
self.assertEqual(default_storage.exists('i/special/test.txt'), False)
97100
self.assertEqual(os.path.isfile(test_fqfn), False)
98101

102+
def test_adding_extzipfile(self):
103+
# ZipExtFile advertises seek() but can raise UnsupportedOperation
104+
with ZipFile(os.path.join(DIR, 'fixtures/test.zip')) as testzip:
105+
for filename in testzip.namelist():
106+
with testzip.open(filename) as testfile:
107+
o = Thing()
108+
o.upload = DjangoFile(testfile)
109+
o.save()
110+
with testzip.open(filename) as testfile:
111+
uploaded = File.objects.get(name='i/special/' + filename)
112+
self.assertEqual(uploaded.size, testzip.getinfo(filename).file_size)
113+
self.assertEqual(BytesIO(uploaded.content).read(), testfile.read())
114+
99115
def test_hash(self):
100116
verbose = 1
101117

0 commit comments

Comments
 (0)