Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for absolute path in THUMBNAILS_DIR_NAME setting #132

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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/
9 changes: 7 additions & 2 deletions filebrowser_safe/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import unicode_literals

from django.conf import settings
from django.core.files.storage import default_storage
from future.builtins import super
# coding: utf-8

Expand Down Expand Up @@ -52,6 +55,8 @@ class RenameForm(forms.Form):

def __init__(self, path, file_extension, *args, **kwargs):
self.path = path
if re.match("^https?://", settings.MEDIA_ROOT):
self.path = re.sub("^" + settings.MEDIA_ROOT, "", self.path)
self.file_extension = file_extension
super(RenameForm, self).__init__(*args, **kwargs)

Expand All @@ -69,8 +74,8 @@ def clean_name(self):
not alnum_name_re.search(self.path)):
raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'))
# folder/file must not already exist.
if os.path.isdir(os.path.join(self.path, self.cleaned_data['name'])):
if default_storage.isdir(os.path.join(self.path, self.cleaned_data['name'])):
raise forms.ValidationError(_(u'The Folder already exists.'))
elif os.path.isfile(os.path.join(self.path, self.cleaned_data['name'] + self.file_extension)):
elif default_storage.isfile(os.path.join(self.path, self.cleaned_data['name'] + self.file_extension)):
raise forms.ValidationError(_(u'The File already exists.'))
return self.cleaned_data['name']
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
41 changes: 36 additions & 5 deletions filebrowser_safe/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# PYTHON IMPORTS
import os
import shutil
import posixpath

# DJANGO IMPORTS
from django.core.files.move import file_move_safe
Expand Down Expand Up @@ -112,9 +113,13 @@ 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):
Expand All @@ -133,7 +138,7 @@ def isdir(self, name):
return False

name = self._normalize_name(self._clean_name(name))
dirlist = self.bucket.list(self._encode_name(name))
dirlist = self.listdir(self._encode_name(name))

# Check whether the iterator is empty
for item in dirlist:
Expand Down Expand Up @@ -163,6 +168,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
6 changes: 5 additions & 1 deletion filebrowser_safe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def remove_thumbnails(file_path):
"""
from mezzanine.conf import settings
dir_name, file_name = os.path.split(file_path)
path = os.path.join(dir_name, settings.THUMBNAILS_DIR_NAME, file_name)
if os.path.isabs(settings.THUMBNAILS_DIR_NAME):
path = os.path.join(settings.THUMBNAILS_DIR_NAME, dir_name, file_name)
else:
path = os.path.join(dir_name, settings.THUMBNAILS_DIR_NAME, file_name)

try:
default_storage.rmtree(path)
except:
Expand Down