diff --git a/.gitignore b/.gitignore index 080a328..a7bb8d2 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ htmlcov *.egg-info/ build dist +*.swp diff --git a/django_webdav_storage/listing.py b/django_webdav_storage/listing.py index a0ae49d..74f617f 100644 --- a/django_webdav_storage/listing.py +++ b/django_webdav_storage/listing.py @@ -1,7 +1,6 @@ import re - -NGINX_AUTOINDEX_RE = re.compile(b'\') +NGINX_AUTOINDEX_RE = re.compile(r'\') def nginx_autoindex(storage, path): @@ -9,11 +8,11 @@ def nginx_autoindex(storage, path): response = storage.webdav('GET', path) - for link in NGINX_AUTOINDEX_RE.findall(response.content): - if link == b'../': + for link in NGINX_AUTOINDEX_RE.findall(response.content.decode('utf-8')): + if link == '../': continue - if link.endswith(b'/'): + if link.endswith('/'): directories.append(link[:-1]) else: files.append(link) diff --git a/django_webdav_storage/storage.py b/django_webdav_storage/storage.py index 88e64cf..9fefaab 100644 --- a/django_webdav_storage/storage.py +++ b/django_webdav_storage/storage.py @@ -1,9 +1,10 @@ # coding: utf-8 - from __future__ import unicode_literals from __future__ import print_function from __future__ import absolute_import from __future__ import division +from tempfile import NamedTemporaryFile + from django.core.files.storage import Storage as StorageBase from django.core.files.base import ContentFile from django.conf import settings @@ -114,6 +115,14 @@ def exists(self, name): else: return True + def path(self, name): + try: + with NamedTemporaryFile(delete=False) as tf: + tf.write(self.webdav('GET', name).content) + return tf.name + except requests.exceptions.HTTPError: + pass + def size(self, name): try: return int(self.webdav('HEAD', name).headers['content-length']) diff --git a/django_webdav_storage/tests/tests_list_dir.py b/django_webdav_storage/tests/tests_list_dir.py index a77a1ff..8913578 100644 --- a/django_webdav_storage/tests/tests_list_dir.py +++ b/django_webdav_storage/tests/tests_list_dir.py @@ -28,11 +28,11 @@ def test_listdir_works(self): self.assertSetEqual( {f for f in files}, - {b'file.img', b'hello.pdf'} + {'file.img', 'hello.pdf'} ) self.assertSetEqual( {d for d in dirs}, - {b'hello'}, + {'hello'}, ) def test_listdir_not_found(self): diff --git a/django_webdav_storage/tests/tests_path.py b/django_webdav_storage/tests/tests_path.py new file mode 100644 index 0000000..09cd608 --- /dev/null +++ b/django_webdav_storage/tests/tests_path.py @@ -0,0 +1,31 @@ +# coding: utf-8 +from __future__ import unicode_literals +from __future__ import print_function +from __future__ import absolute_import +from __future__ import division +import os + +from django_webdav_storage.tests import TestBase, LAZY_FOX + + +class TestPathMethod(TestBase): + """ + Tests for `path` storage method + """ + def test_path_non_exists(self): + self.assertIsNone( + self.storage.path(self.session_id + '_non_exists.txt') + ) + + def test_path_exists(self): + with self.existing_file('exists.txt') as f: + path = self.storage.path(f.filename) + self.assertTrue(os.path.exists(path)) + self.storage.delete(f.filename) + self.assertIsNone(self.storage.path(f.filename)) + + def test_path_file_content(self): + with self.existing_file('exists.txt') as f: + path = self.storage.path(f.filename) + with open(path) as tmp: + self.assertEqual(tmp.read(), LAZY_FOX)