mirror of https://github.com/django/django.git
Made FileSystemStorage accept both text and byte streams
Thanks Alexey Boriskin for his help on the patch.
This commit is contained in:
parent
b5240d25c1
commit
4e70ad11d2
|
@ -195,10 +195,17 @@ class FileSystemStorage(Storage):
|
||||||
fd = os.open(full_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_BINARY', 0))
|
fd = os.open(full_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_BINARY', 0))
|
||||||
try:
|
try:
|
||||||
locks.lock(fd, locks.LOCK_EX)
|
locks.lock(fd, locks.LOCK_EX)
|
||||||
|
_file = None
|
||||||
for chunk in content.chunks():
|
for chunk in content.chunks():
|
||||||
os.write(fd, chunk)
|
if _file is None:
|
||||||
|
mode = 'wb' if isinstance(chunk, bytes) else 'wt'
|
||||||
|
_file = os.fdopen(fd, mode)
|
||||||
|
_file.write(chunk)
|
||||||
finally:
|
finally:
|
||||||
locks.unlock(fd)
|
locks.unlock(fd)
|
||||||
|
if _file is not None:
|
||||||
|
_file.close()
|
||||||
|
else:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno == errno.EEXIST:
|
if e.errno == errno.EEXIST:
|
||||||
|
|
|
@ -356,6 +356,17 @@ class FileStorageTests(unittest.TestCase):
|
||||||
finally:
|
finally:
|
||||||
os.remove = real_remove
|
os.remove = real_remove
|
||||||
|
|
||||||
|
def test_file_chunks_error(self):
|
||||||
|
"""
|
||||||
|
Test behaviour when file.chunks() is raising an error
|
||||||
|
"""
|
||||||
|
f1 = ContentFile('chunks fails')
|
||||||
|
def failing_chunks():
|
||||||
|
raise IOError
|
||||||
|
f1.chunks = failing_chunks
|
||||||
|
with self.assertRaises(IOError):
|
||||||
|
self.storage.save('error.file', f1)
|
||||||
|
|
||||||
|
|
||||||
class CustomStorage(FileSystemStorage):
|
class CustomStorage(FileSystemStorage):
|
||||||
def get_available_name(self, name):
|
def get_available_name(self, name):
|
||||||
|
|
Loading…
Reference in New Issue