Skip to content

Commit cd2ad9b

Browse files
committed
Be consistent with os.path in lowercase_ext.
1 parent 02580ef commit cd2ad9b

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

flaskext/uploads.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,24 @@ def extension(filename):
8080

8181

8282
def lowercase_ext(filename):
83+
"""
84+
This is a helper used by UploadSet.save to provide lowercase extensions for
85+
all processed files, to compare with configured extensions in the same
86+
case.
87+
88+
.. versionchanged:: 0.1.4
89+
Filenames without extensions are no longer lowercased, only the
90+
extension is returned in lowercase, if an extension exists.
91+
92+
:param filename: The filename to ensure has a lowercase extension.
93+
"""
8394
if '.' in filename:
8495
main, ext = os.path.splitext(filename)
8596
return main + ext.lower()
86-
else:
87-
return filename.lower()
97+
# For consistency with os.path.splitext,
98+
# do not treat a filename without an extension as an extension.
99+
# That is, do not return filename.lower().
100+
return filename
88101

89102

90103
def addslash(url):

tests/test-uploads.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import os.path
1616
from flask import Flask, url_for
1717
from flaskext.uploads import (UploadSet, UploadConfiguration, extension,
18-
TestingFileStorage, patch_request_class, configure_uploads, addslash,
19-
ALL, AllExcept)
18+
lowercase_ext, TestingFileStorage, patch_request_class, configure_uploads,
19+
addslash, ALL, AllExcept)
2020

2121

2222
class TestMiscellaneous(object):
@@ -34,6 +34,16 @@ def test_extension(self):
3434
assert extension('archive.tar.gz') == 'gz'
3535
assert extension('audio.m4a') == 'm4a'
3636

37+
def test_lowercase_ext(self):
38+
assert lowercase_ext('foo.txt') == 'foo.txt'
39+
assert lowercase_ext('FOO.TXT') == 'FOO.txt'
40+
assert lowercase_ext('foo') == 'foo'
41+
assert lowercase_ext('FOO') == 'FOO'
42+
assert lowercase_ext('archive.tar.gz') == 'archive.tar.gz'
43+
assert lowercase_ext('ARCHIVE.TAR.GZ') == 'ARCHIVE.TAR.gz'
44+
assert lowercase_ext('audio.m4a') == 'audio.m4a'
45+
assert lowercase_ext('AUDIO.M4A') == 'AUDIO.m4a'
46+
3747
def test_addslash(self):
3848
assert (addslash('http://localhost:4000') ==
3949
'http://localhost:4000/')

0 commit comments

Comments
 (0)