diff --git a/pillarsdk/utils.py b/pillarsdk/utils.py index 396bd97..d588ba0 100644 --- a/pillarsdk/utils.py +++ b/pillarsdk/utils.py @@ -145,3 +145,17 @@ def download_to_file(url, filename, chunk_size=10 * 1024): open(filename, 'wb') as outfile: for block in req.iter_content(chunk_size=chunk_size): outfile.write(block) + + +def sanitize_filename(file_name): + """Sanitize the filename. + + Returns a new filename with only filename-safe characters. + """ + + # Removes quotes and other unsafe characters. + # This is easier than keeping good characters and trying to be unicode-friendly. + badchars = set('''!#$%&*()[]{}'"/\\<>''') + safe_name = ''.join(c for c in file_name + if ord(c) > 31 and c not in badchars) + return safe_name.strip(' .') diff --git a/tests/test_utils.py b/tests/test_utils.py index 766ff21..a09cabd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -69,3 +69,11 @@ class PillarUtilsTests(unittest.TestCase): as_json = json.dumps(resource, cls=utils.PillarJSONEncoder, sort_keys=True) self.assertEqual('{"datetime": "2016-03-22 12:35:16"}', as_json) + + def test_sanitize_filename(self): + + self.assertEqual('abc.def', utils.sanitize_filename('abc.def')) + self.assertEqual('abc.def', utils.sanitize_filename('. . abc.def . . ')) + self.assertEqual('abc......def', utils.sanitize_filename('././abc../..///..def')) + self.assertEqual('Pad Thai is ผัดไทย', utils.sanitize_filename('Pad Thai is ผัดไทย')) + self.assertEqual(u'Pad Thai is ผัดไทย', utils.sanitize_filename(u'Pad Thai is ผัดไทย'))