Fix day filter
[cds-indico.git] / indico / util / fs.py
blob0de9a8b78c24c83bfcd356815f1dc6c9f54bc68e
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 import errno
18 import os
20 from indico.util.string import unicode_to_ascii, to_unicode
21 from werkzeug.utils import secure_filename as _secure_filename
24 def silentremove(filename):
25 try:
26 os.remove(filename)
27 except OSError as e:
28 if e.errno != errno.ENOENT:
29 raise
32 def secure_filename(filename, fallback):
33 """Returns a secure version of a filename.
35 This removes possibly dangerous characters and also converts the
36 filename to plain ASCII for maximum compatibility.
38 :param filename: A filename
39 :param fallback: The filename to use if there were no safe chars
40 in the original filename.
41 """
42 if not filename:
43 return fallback
44 return _secure_filename(unicode_to_ascii(to_unicode(filename))) or fallback