Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
*.pyc
*.eggs/
*.egg-info
.idea
mezzanine-git/
2 changes: 1 addition & 1 deletion filebrowser_safe/locale/cs/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ msgstr[1] "%(counter) výsledky"
#: templates/filebrowser/include/toolbar.html:9
#, python-format
msgid "%(full_result_count)s total"
msgstr "%(full_result_count) celkem"
msgstr "%(full_result_count)s celkem"

#: templates/filebrowser/include/search.html:5
msgid "Clear Restrictions"
Expand Down
48 changes: 40 additions & 8 deletions filebrowser_safe/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# coding: utf-8

# PYTHON IMPORTS
import itertools
import os
import shutil
import posixpath

# DJANGO IMPORTS
from django.core.files.move import file_move_safe
Expand Down Expand Up @@ -112,15 +114,19 @@ def makedirs(self, name):

def rmtree(self, name):
name = self._normalize_name(self._clean_name(name))
dirlist = self.listdir(self._encode_name(name))
for item in dirlist:
item.delete()
directories, files = self.listdir(self._encode_name(name))

for key in files:
self.delete('/'.join([name, key]))

for dirname in directories:
self.rmtree('/'.join([name, dirname]))


class GoogleStorageMixin(StorageMixin):

def isfile(self, name):
return self.exists(name)
return bool(name) and self.exists(name)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by relying only on exists(), the method isfile() inappropriately considered the bucket root to be a file.


def isdir(self, name):
# That's some inefficient implementation...
Expand All @@ -132,10 +138,10 @@ def isdir(self, name):
if self.isfile(name):
return False

name = self._normalize_name(self._clean_name(name))
dirlist = self.bucket.list(self._encode_name(name))
(dirs, files) = self.listdir(name)
Comment thread
jesteria marked this conversation as resolved.
Outdated
dirlist = itertools.chain(dirs, files)

# Check whether the iterator is empty
# Check for contents
for item in dirlist:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the dirlist returned by listdir() is a tuple of (dirs, files), so the iteration check was previously not testing anything about the listing -- (it would always return True).

return True
return False
Expand Down Expand Up @@ -163,6 +169,32 @@ def makedirs(self, name):

def rmtree(self, name):
name = self._normalize_name(self._clean_name(name))
dirlist = self.bucket.list(self._encode_name(name))
dirlist = self.listdir(self._encode_name(name))
for item in dirlist:
item.delete()

def _clean_name(self, name):
"""
Cleans the name so that Windows style paths work
"""
return clean_name(name)


def clean_name(name):
"""
Cleans the name so that Windows style paths work
"""
# Normalize Windows style paths
clean_name = posixpath.normpath(name).replace('\\', '/')

# os.path.normpath() can strip trailing slashes so we implement
# a workaround here.
if name.endswith('/') and not clean_name.endswith('/'):
# Add a trailing slash as it was stripped.
clean_name = clean_name + '/'

# Given an empty string, os.path.normpath() will return ., which we don't want
if clean_name == '.':
clean_name = ''

return clean_name