From 05d6940dae292dafb637372e915b9da6b758f843 Mon Sep 17 00:00:00 2001 From: Ilya Boltnev Date: Fri, 26 Jul 2019 19:45:59 +0300 Subject: [PATCH 1/4] add storage path method --- .gitignore | 1 + django_webdav_storage/storage.py | 11 +++++++- django_webdav_storage/tests/tests_path.py | 32 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 django_webdav_storage/tests/tests_path.py 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/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_path.py b/django_webdav_storage/tests/tests_path.py new file mode 100644 index 0000000..e30c1d5 --- /dev/null +++ b/django_webdav_storage/tests/tests_path.py @@ -0,0 +1,32 @@ +# 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) + From 56baa48112fb30fe4109813b3cc281bdd04d32b3 Mon Sep 17 00:00:00 2001 From: Ilya Boltnev Date: Fri, 26 Jul 2019 19:52:16 +0300 Subject: [PATCH 2/4] blank line fix --- django_webdav_storage/tests/tests_path.py | 1 - 1 file changed, 1 deletion(-) diff --git a/django_webdav_storage/tests/tests_path.py b/django_webdav_storage/tests/tests_path.py index e30c1d5..09cd608 100644 --- a/django_webdav_storage/tests/tests_path.py +++ b/django_webdav_storage/tests/tests_path.py @@ -29,4 +29,3 @@ def test_path_file_content(self): path = self.storage.path(f.filename) with open(path) as tmp: self.assertEqual(tmp.read(), LAZY_FOX) - From 656e71a41b0c0d2c8d7daafe67bcdb0cfde96955 Mon Sep 17 00:00:00 2001 From: Ilya Boltnev Date: Fri, 26 Jul 2019 19:57:31 +0300 Subject: [PATCH 3/4] fix regex --- django_webdav_storage/listing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_webdav_storage/listing.py b/django_webdav_storage/listing.py index a0ae49d..37f6e58 100644 --- a/django_webdav_storage/listing.py +++ b/django_webdav_storage/listing.py @@ -1,7 +1,7 @@ import re -NGINX_AUTOINDEX_RE = re.compile(b'\') +NGINX_AUTOINDEX_RE = re.compile(r'\') def nginx_autoindex(storage, path): From d975dc2e665c916d563db9ec3393eecf84a998d2 Mon Sep 17 00:00:00 2001 From: Ilya Boltnev Date: Sat, 27 Jul 2019 15:37:00 +0300 Subject: [PATCH 4/4] fixing bytes and chars mix --- django_webdav_storage/listing.py | 7 +++---- django_webdav_storage/tests/tests_list_dir.py | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/django_webdav_storage/listing.py b/django_webdav_storage/listing.py index 37f6e58..74f617f 100644 --- a/django_webdav_storage/listing.py +++ b/django_webdav_storage/listing.py @@ -1,6 +1,5 @@ import re - NGINX_AUTOINDEX_RE = re.compile(r'\') @@ -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/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):