Skip to content

Commit

Permalink
Move extension helper functions also to extensions module
Browse files Browse the repository at this point in the history
modified:   src/flask_uploads/extensions.py
modified:   src/flask_uploads/flask_uploads.py
  • Loading branch information
jugmac00 committed Jun 30, 2020
1 parent f9bb591 commit 8a60e85
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
30 changes: 30 additions & 0 deletions src/flask_uploads/extensions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

"""Extension presets and extension configuration."""
import os

# This contains archive and compression formats (.gz, .bz2, .zip, .tar,
# .tgz, .txz, and .7z).
Expand Down Expand Up @@ -93,3 +94,32 @@ def __init__(self, items):

def __contains__(self, item):
return item not in self.items


def extension(filename):
ext = os.path.splitext(filename)[1]
if ext.startswith('.'):
# os.path.splitext retains . separator
ext = ext[1:]
return ext


def lowercase_ext(filename):
"""
This is a helper used by UploadSet.save to provide lowercase extensions for
all processed files, to compare with configured extensions in the same
case.
.. versionchanged:: 0.1.4
Filenames without extensions are no longer lowercased, only the
extension is returned in lowercase, if an extension exists.
:param filename: The filename to ensure has a lowercase extension.
"""
if '.' in filename:
main, ext = os.path.splitext(filename)
return main + ext.lower()
# For consistency with os.path.splitext,
# do not treat a filename without an extension as an extension.
# That is, do not return filename.lower().
return filename
31 changes: 2 additions & 29 deletions src/flask_uploads/flask_uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,8 @@

from .exceptions import UploadNotAllowed
from .extensions import DEFAULTS


def extension(filename):
ext = os.path.splitext(filename)[1]
if ext.startswith('.'):
# os.path.splitext retains . separator
ext = ext[1:]
return ext


def lowercase_ext(filename):
"""
This is a helper used by UploadSet.save to provide lowercase extensions for
all processed files, to compare with configured extensions in the same
case.
.. versionchanged:: 0.1.4
Filenames without extensions are no longer lowercased, only the
extension is returned in lowercase, if an extension exists.
:param filename: The filename to ensure has a lowercase extension.
"""
if '.' in filename:
main, ext = os.path.splitext(filename)
return main + ext.lower()
# For consistency with os.path.splitext,
# do not treat a filename without an extension as an extension.
# That is, do not return filename.lower().
return filename
from .extensions import extension
from .extensions import lowercase_ext


def addslash(url):
Expand Down

0 comments on commit 8a60e85

Please sign in to comment.